|
1
|
|
|
<?php |
|
2
|
|
|
namespace library\cc |
|
3
|
|
|
{ |
|
4
|
|
|
|
|
5
|
|
|
use library\components\Component; |
|
6
|
|
|
use library\storage\JsonStorage; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Application |
|
10
|
|
|
* @package library\cc |
|
11
|
|
|
*/ |
|
12
|
|
|
class Application |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var \stdClass |
|
16
|
|
|
*/ |
|
17
|
|
|
private $config; |
|
18
|
|
|
/** |
|
19
|
|
|
* @var \library\storage\Storage $config |
|
20
|
|
|
*/ |
|
21
|
|
|
private $storage; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var Request |
|
25
|
|
|
*/ |
|
26
|
|
|
private $request; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $matchedSitemapItems = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $applicationComponents = array(); |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Application constructor. |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct() |
|
42
|
|
|
{ |
|
43
|
|
|
$this->config(); |
|
44
|
|
|
$this->storage(); |
|
45
|
|
|
|
|
46
|
|
|
$this->request = new Request(); |
|
47
|
|
|
|
|
48
|
|
|
$this->sitemapMatching($this->request); |
|
49
|
|
|
|
|
50
|
|
|
$this->getApplicationComponents(); |
|
51
|
|
|
|
|
52
|
|
|
$this->runApplicationComponents(); |
|
53
|
|
|
$this->runSitemapComponents(); |
|
54
|
|
|
|
|
55
|
|
|
$this->renderApplicationComponents(); |
|
56
|
|
|
$this->renderSitemapComponents(); |
|
57
|
|
|
|
|
58
|
|
|
dump($this); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Initialize the config |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \Exception |
|
65
|
|
|
*/ |
|
66
|
|
|
private function config() |
|
67
|
|
|
{ |
|
68
|
|
|
$configPath = __DIR__ . '/../../config.json'; |
|
69
|
|
|
if (realpath($configPath) !== false) { |
|
70
|
|
|
$json = file_get_contents($configPath); |
|
71
|
|
|
$this->config = json_decode($json); |
|
72
|
|
|
} else { |
|
73
|
|
|
throw new \Exception('Couldn\'t find config file in path ' . $configPath); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Initialize the storage |
|
79
|
|
|
*/ |
|
80
|
|
|
private function storage() |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->getStorageType() == 'json') { |
|
83
|
|
|
$this->storage = new JsonStorage($this->getStoragePath()); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Loop through sitemap items and see if one matches the requestUri. |
|
89
|
|
|
* If it does, add it tot the matchedSitemapItems array |
|
90
|
|
|
* |
|
91
|
|
|
* @param $request |
|
92
|
|
|
*/ |
|
93
|
|
|
private function sitemapMatching($request) |
|
94
|
|
|
{ |
|
95
|
|
|
$sitemap = $this->storage->getSitemap(); |
|
96
|
|
|
$relativeUri = '/' . $request::$relativeUri; |
|
97
|
|
|
|
|
98
|
|
|
foreach ($sitemap as $sitemapItem) { |
|
99
|
|
|
if ($sitemapItem->regex) { |
|
100
|
|
|
$matches = array(); |
|
101
|
|
|
if (preg_match_all($sitemapItem->url, $relativeUri, $matches)) { |
|
102
|
|
|
// Make a clone, so it doesnt add the matches to the original |
|
103
|
|
|
$matchedClone = clone $sitemapItem; |
|
104
|
|
|
$matchedClone->matches = $matches; |
|
105
|
|
|
$this->matchedSitemapItems[] = $matchedClone; |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
} else { |
|
109
|
|
|
if ($sitemapItem->url == $relativeUri) { |
|
110
|
|
|
$this->matchedSitemapItems[] = $sitemapItem; |
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Loop through all application components and run them |
|
119
|
|
|
* |
|
120
|
|
|
* @throws \Exception |
|
121
|
|
|
*/ |
|
122
|
|
|
private function runApplicationComponents() |
|
123
|
|
|
{ |
|
124
|
|
|
foreach ($this->applicationComponents as $key => $applicationComponent) { |
|
125
|
|
|
$class = $applicationComponent->component; |
|
126
|
|
|
$parameters = $applicationComponent->parameters; |
|
127
|
|
|
$this->applicationComponents[$key]->{'object'} = $this->getComponentObject($class, null, $parameters, null); |
|
|
|
|
|
|
128
|
|
|
$this->applicationComponents[$key]->{'object'}->run($this->storage); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Loop through all (matched) sitemap components and run them |
|
134
|
|
|
* |
|
135
|
|
|
* @throws \Exception |
|
136
|
|
|
*/ |
|
137
|
|
|
private function runSitemapComponents() |
|
138
|
|
|
{ |
|
139
|
|
|
foreach ($this->matchedSitemapItems as $key => $sitemapItem) { |
|
140
|
|
|
$class = $sitemapItem->component; |
|
141
|
|
|
$template = $sitemapItem->template; |
|
142
|
|
|
$parameters = $sitemapItem->parameters; |
|
143
|
|
|
|
|
144
|
|
|
$this->matchedSitemapItems[$key]->object = $this->getComponentObject($class, $template, $parameters, $sitemapItem); |
|
145
|
|
|
|
|
146
|
|
|
$this->matchedSitemapItems[$key]->object->run($this->storage); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param string $class |
|
152
|
|
|
* @param string $template |
|
153
|
|
|
* @param array $parameters |
|
154
|
|
|
* @param \stdClass $matchedSitemapItem |
|
155
|
|
|
* |
|
156
|
|
|
* @return mixed |
|
157
|
|
|
* @throws \Exception |
|
158
|
|
|
*/ |
|
159
|
|
|
private function getComponentObject($class='', $template='', $parameters=array(), $matchedSitemapItem) |
|
160
|
|
|
{ |
|
161
|
|
|
$libraryComponentName = '\\library\\components\\' . $class; |
|
162
|
|
|
$userComponentName = '\\components\\' . $class; |
|
163
|
|
|
|
|
164
|
|
|
if (\autoLoad($libraryComponentName, false)) { |
|
165
|
|
|
$component = new $libraryComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
|
166
|
|
|
} elseif (\autoLoad($userComponentName, false)) { |
|
167
|
|
|
$component = new $userComponentName($template, $this->request, $parameters, $matchedSitemapItem); |
|
168
|
|
|
} else { |
|
169
|
|
|
throw new \Exception('Could not load component ' . $class); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (!$component instanceof Component) { |
|
173
|
|
|
throw new \Exception('Component not of type Component. Must inherit \library\components\Component'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $component; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Loop through all application components and render them |
|
181
|
|
|
*/ |
|
182
|
|
|
private function renderApplicationComponents() |
|
183
|
|
|
{ |
|
184
|
|
|
foreach ($this->applicationComponents as $applicationComponent) { |
|
185
|
|
|
$applicationComponent->{'object'}->render(); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Loop through all (matched) sitemap components and render them |
|
191
|
|
|
*/ |
|
192
|
|
|
private function renderSitemapComponents() |
|
193
|
|
|
{ |
|
194
|
|
|
foreach ($this->matchedSitemapItems as $sitemapItem) { |
|
195
|
|
|
$this->setCachingHeaders(); |
|
196
|
|
|
$sitemapItem->object->render($this); |
|
197
|
|
|
ob_clean(); |
|
198
|
|
|
echo $sitemapItem->object->get(); |
|
199
|
|
|
ob_end_flush(); |
|
200
|
|
|
exit; |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
public function getAllApplicationComponentParameters() |
|
205
|
|
|
{ |
|
206
|
|
|
$allParameters = array(); |
|
207
|
|
|
foreach ($this->applicationComponents as $applicationComponent) { |
|
208
|
|
|
$parameters = $applicationComponent->{'object'}->getParameters(); |
|
209
|
|
|
$allParameters[] = $parameters; |
|
210
|
|
|
} |
|
211
|
|
|
return $allParameters; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
public function unlockApplicationComponentParameters() |
|
215
|
|
|
{ |
|
216
|
|
|
foreach ($this->applicationComponents as $applicationComponent) { |
|
217
|
|
|
$parameters = $applicationComponent->{'object'}->getParameters(); |
|
218
|
|
|
extract($parameters); |
|
219
|
|
|
} |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Set the default caching of pages to 2 days |
|
224
|
|
|
*/ |
|
225
|
|
|
public function setCachingHeaders() |
|
226
|
|
|
{ |
|
227
|
|
|
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60 * 24 * 2))); // 2 days |
|
228
|
|
|
header("Cache-Control: max-age=" . (60 * 60 * 24 * 2)); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* @return string |
|
233
|
|
|
*/ |
|
234
|
|
|
public function getStorageType() |
|
235
|
|
|
{ |
|
236
|
|
|
return $this->config->storageType; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return string |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getStoragePath() |
|
243
|
|
|
{ |
|
244
|
|
|
return $this->config->storagePath; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function getApplicationComponents() |
|
248
|
|
|
{ |
|
249
|
|
|
$this->applicationComponents = $this->storage->getApplicationComponents(); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
} |
|
253
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: