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, $this->config->filesDir); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getAllApplicationComponentParameters() |
108
|
|
|
{ |
109
|
|
|
$allParameters = array(); |
110
|
|
|
foreach ($this->applicationComponents as $applicationComponent) { |
111
|
|
|
$parameters = $applicationComponent->{'object'}->getParameters(); |
112
|
|
|
$allParameters[] = $parameters; |
113
|
|
|
} |
114
|
|
|
return $allParameters; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function unlockApplicationComponentParameters() |
118
|
|
|
{ |
119
|
|
|
foreach ($this->applicationComponents as $applicationComponent) { |
120
|
|
|
$parameters = $applicationComponent->{'object'}->getParameters(); |
121
|
|
|
extract($parameters, EXTR_OVERWRITE); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getTemplateDir() |
129
|
|
|
{ |
130
|
|
|
return $this->config->templateDir; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
public function getStorageDir() |
137
|
|
|
{ |
138
|
|
|
return $this->config->storageDir; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getApplicationComponents() |
142
|
|
|
{ |
143
|
|
|
$this->applicationComponents = $this->storage->getApplicationComponents()->getApplicationComponents(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @return string |
148
|
|
|
*/ |
149
|
|
|
public function getRootDir() |
150
|
|
|
{ |
151
|
|
|
return $this->config->rootDir; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
private function setExceptionHandler() |
155
|
|
|
{ |
156
|
|
|
$whoops = new Run; |
157
|
|
|
$whoops->pushHandler(new PrettyPageHandler); |
158
|
|
|
$whoops->register(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @throws \Exception |
163
|
|
|
*/ |
164
|
|
|
private function urlMatching() |
165
|
|
|
{ |
166
|
|
|
$urlMatcher = new UrlMatcher($this, $this->storage); |
167
|
|
|
$urlMatcher->redirectMatching($this->request); |
168
|
|
|
$urlMatcher->sitemapMatching($this->request); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function run() |
172
|
|
|
{ |
173
|
|
|
$applicationRunner = new ApplicationRunner($this->storage, $this->request); |
174
|
|
|
$applicationRunner->runApplicationComponents($this->applicationComponents); |
175
|
|
|
$applicationRunner->runSitemapComponents($this->matchedSitemapItems); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @throws \Exception |
180
|
|
|
*/ |
181
|
|
|
private function render() |
182
|
|
|
{ |
183
|
|
|
$applicationRenderer = new ApplicationRenderer($this, $this->storage, $this->request); |
184
|
|
|
$applicationRenderer->renderApplicationComponents($this->applicationComponents); |
185
|
|
|
$applicationRenderer->renderSitemapComponents($this->matchedSitemapItems); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
private function startServices() |
189
|
|
|
{ |
190
|
|
|
FileService::getInstance()->init($this->storage); |
191
|
|
|
ImageService::getInstance()->init($this->storage); |
192
|
|
|
ValuelistService::getInstance()->init($this->storage); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function addMatchedSitemapItem($matchedClone) |
196
|
|
|
{ |
197
|
|
|
$this->matchedSitemapItems[] = $matchedClone; |
198
|
|
|
} |
199
|
|
|
} |