Passed
Push — master ( 3a2d72...a747ff )
by Jens
02:50
created

Application::setHeaders()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nc 4
nop 0
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
1
<?php
2
3
namespace CloudControl\Cms\cc;
4
5
use CloudControl\Cms\cc\application\ApplicationRenderer;
6
use CloudControl\Cms\cc\application\ApplicationRunner;
7
use CloudControl\Cms\cc\application\UrlMatcher;
8
use CloudControl\Cms\services\FileService;
9
use CloudControl\Cms\services\ImageService;
10
use CloudControl\Cms\services\ValuelistService;
11
use CloudControl\Cms\storage\Cache;
12
use CloudControl\Cms\storage\Storage;
13
use Whoops\Handler\PrettyPageHandler;
14
use Whoops\Run;
15
16
class Application
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $rootDir;
22
    /**
23
     * @var string
24
     */
25
    protected $configPath;
26
    /**
27
     * @var \stdClass
28
     */
29
    private $config;
30
    /**
31
     * @var \CloudControl\Cms\storage\Storage
32
     */
33
    private $storage;
34
35
    /**
36
     * @var \CloudControl\Cms\cc\Request
37
     */
38
    private $request;
39
40
    /**
41
     * @var array
42
     */
43
    private $matchedSitemapItems = array();
44
45
    /**
46
     * @var array
47
     */
48
    private $applicationComponents = array();
49
50
51
    /**
52
     * Application constructor.
53
     * @param string $rootDir
54
     * @param string $configPath
55
     * @throws \Exception
56
     */
57
    public function __construct($rootDir, $configPath)
58
    {
59
        $this->rootDir = $rootDir;
60
        $this->configPath = $configPath;
61
62
        $this->config();
63
        $this->storage();
64
65
        Cache::getInstance()->setStoragePath($this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->storageDir);
66
67
        $this->request = new Request();
68
        ResponseHeaders::init();
69
70
        $this->setExceptionHandler();
71
72
        $this->startServices();
73
74
        $this->urlMatching();
75
76
        $this->getApplicationComponents();
77
        $this->run();
78
        $this->render();
79
    }
80
81
    /**
82
     * Initialize the config
83
     *
84
     * @throws \Exception
85
     */
86
    private function config()
87
    {
88
        if (realpath($this->configPath) !== false) {
89
            $json = file_get_contents($this->configPath);
90
            $this->config = json_decode($json);
91
            $this->config->rootDir = $this->rootDir;
92
        } else {
93
            throw new \RuntimeException('Framework not initialized yet. Consider running composer install');
94
        }
95
    }
96
97
    /**
98
     * Initialize the storage
99
     * @throws \Exception
100
     */
101
    private function storage()
102
    {
103
        $this->storage = new Storage($this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->storageDir,
104
            $this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->imagesDir,
105
            $this->config->rootDir . DIRECTORY_SEPARATOR . $this->config->filesDir);
106
    }
107
108
    public function getAllApplicationComponentParameters()
109
    {
110
        $allParameters = array();
111
        foreach ($this->applicationComponents as $applicationComponent) {
112
            $parameters = $applicationComponent->{'object'}->getParameters();
113
            $allParameters[] = $parameters;
114
        }
115
        return $allParameters;
116
    }
117
118
    public function unlockApplicationComponentParameters()
119
    {
120
        foreach ($this->applicationComponents as $applicationComponent) {
121
            $parameters = $applicationComponent->{'object'}->getParameters();
122
            extract($parameters, EXTR_OVERWRITE);
123
        }
124
    }
125
126
    /**
127
     * @return string
128
     */
129
    public function getTemplateDir()
130
    {
131
        return $this->config->templateDir;
132
    }
133
134
    /**
135
     * @return string
136
     */
137
    public function getStorageDir()
138
    {
139
        return $this->config->storageDir;
140
    }
141
142
    public function getApplicationComponents()
143
    {
144
        $this->applicationComponents = $this->storage->getApplicationComponents()->getApplicationComponents();
145
    }
146
147
    /**
148
     * @return string
149
     */
150
    public function getRootDir()
151
    {
152
        return $this->config->rootDir;
153
    }
154
155
    private function setExceptionHandler()
156
    {
157
        $whoops = new Run;
158
        $whoops->pushHandler(new PrettyPageHandler);
159
        $whoops->register();
160
    }
161
162
    /**
163
     * @throws \Exception
164
     */
165
    private function urlMatching()
166
    {
167
        $urlMatcher = new UrlMatcher($this, $this->storage);
168
        $urlMatcher->redirectMatching($this->request);
169
        $urlMatcher->sitemapMatching($this->request);
170
    }
171
172
    private function run()
173
    {
174
        $applicationRunner = new ApplicationRunner($this->storage, $this->request);
175
        $applicationRunner->runApplicationComponents($this->applicationComponents);
176
        $applicationRunner->runSitemapComponents($this->matchedSitemapItems);
177
    }
178
179
    /**
180
     * @throws \Exception
181
     */
182
    private function render()
183
    {
184
        $applicationRenderer = new ApplicationRenderer($this, $this->storage, $this->request);
185
        $applicationRenderer->renderApplicationComponents($this->applicationComponents);
186
        $applicationRenderer->renderSitemapComponents($this->matchedSitemapItems);
187
    }
188
189
    private function startServices()
190
    {
191
        FileService::getInstance()->init($this->storage);
192
        ImageService::getInstance()->init($this->storage);
193
        ValuelistService::getInstance()->init($this->storage);
194
    }
195
196
    public function addMatchedSitemapItem($matchedClone)
197
    {
198
        $this->matchedSitemapItems[] = $matchedClone;
199
    }
200
}