|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright 2018 Vladimir Jimenez |
|
5
|
|
|
* @license https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace allejo\stakx\Server; |
|
9
|
|
|
|
|
10
|
|
|
use allejo\stakx\Compiler; |
|
11
|
|
|
use allejo\stakx\Document\BasePageView; |
|
12
|
|
|
use allejo\stakx\Document\CollectableItem; |
|
13
|
|
|
use allejo\stakx\Document\DynamicPageView; |
|
14
|
|
|
use allejo\stakx\Document\ReadableDocument; |
|
15
|
|
|
use allejo\stakx\Document\RepeaterPageView; |
|
16
|
|
|
use allejo\stakx\Document\StaticPageView; |
|
17
|
|
|
use allejo\stakx\Document\TemplateReadyDocument; |
|
18
|
|
|
use allejo\stakx\Filesystem\FilesystemLoader as fs; |
|
19
|
|
|
use allejo\stakx\FrontMatter\ExpandedValue; |
|
20
|
|
|
use allejo\stakx\Service; |
|
21
|
|
|
use FastRoute\RouteCollector; |
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
23
|
|
|
use React\Http\Response; |
|
24
|
|
|
|
|
25
|
|
|
class Controller |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
private static $baseUrl = ''; |
|
28
|
|
|
private $lastModified = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @internal |
|
32
|
|
|
*/ |
|
33
|
|
|
private function __construct() |
|
34
|
|
|
{ |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Build a controller for handling a Static PageView's URL. |
|
39
|
|
|
* |
|
40
|
|
|
* @param StaticPageView $pageView |
|
41
|
|
|
* @param Compiler $compiler |
|
42
|
|
|
* |
|
43
|
|
|
* @return \Closure |
|
44
|
|
|
*/ |
|
45
|
|
|
private function staticPageViewAction(StaticPageView $pageView, Compiler $compiler) |
|
46
|
|
|
{ |
|
47
|
|
|
return function () use ($pageView, $compiler) { |
|
48
|
|
|
Service::setOption('currentTemplate', $pageView->getAbsoluteFilePath()); |
|
49
|
|
|
|
|
50
|
|
|
$compiler->getTemplateBridge()->clearTemplateCache(); |
|
51
|
|
|
|
|
52
|
|
|
if ($this->hasBeenTouched($pageView)) |
|
53
|
|
|
{ |
|
54
|
|
|
$pageView->readContent(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$mimeType = MimeDetector::getMimeType(fs::getExtension($pageView->getTargetFile())); |
|
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
return new Response( |
|
60
|
|
|
200, |
|
61
|
|
|
['Content-Type' => $mimeType], |
|
62
|
|
|
$compiler->renderStaticPageView($pageView) |
|
63
|
|
|
); |
|
64
|
|
|
}; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Build a controller for handling a Dynamic PageView's URL. |
|
69
|
|
|
* |
|
70
|
|
|
* @param DynamicPageView $pageView |
|
71
|
|
|
* @param Compiler $compiler |
|
72
|
|
|
* |
|
73
|
|
|
* @return \Closure |
|
74
|
|
|
*/ |
|
75
|
|
|
private function dynamicPageViewAction(DynamicPageView $pageView, Compiler $compiler) |
|
76
|
|
|
{ |
|
77
|
|
|
return function (ServerRequestInterface $request) use ($pageView, $compiler) { |
|
78
|
|
|
Service::setOption('currentTemplate', $pageView->getAbsoluteFilePath()); |
|
79
|
|
|
|
|
80
|
|
|
$compiler->getTemplateBridge()->clearTemplateCache(); |
|
81
|
|
|
|
|
82
|
|
|
$contentItem = self::getContentItem($pageView, $request->getUri()->getPath()); |
|
83
|
|
|
|
|
84
|
|
|
if ($contentItem === null) |
|
85
|
|
|
{ |
|
86
|
|
|
return WebServer::return404(); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($this->hasBeenTouched($pageView)) |
|
90
|
|
|
{ |
|
91
|
|
|
$pageView->readContent(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if ($this->hasBeenTouched($contentItem)) |
|
|
|
|
|
|
95
|
|
|
{ |
|
96
|
|
|
$contentItem->readContent(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return WebServer::return200($compiler->renderDynamicPageView($pageView, $contentItem)); |
|
|
|
|
|
|
100
|
|
|
}; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Build a controller for handling a Repeater PageView's URL. |
|
105
|
|
|
* |
|
106
|
|
|
* @param RepeaterPageView $pageView |
|
107
|
|
|
* @param Compiler $compiler |
|
108
|
|
|
* |
|
109
|
|
|
* @return \Closure |
|
110
|
|
|
*/ |
|
111
|
|
|
private function repeaterPageViewAction(RepeaterPageView $pageView, Compiler $compiler) |
|
112
|
|
|
{ |
|
113
|
|
|
return function (ServerRequestInterface $request) use ($pageView, $compiler) { |
|
114
|
|
|
Service::setOption('currentTemplate', $pageView->getAbsoluteFilePath()); |
|
115
|
|
|
|
|
116
|
|
|
$compiler->getTemplateBridge()->clearTemplateCache(); |
|
117
|
|
|
|
|
118
|
|
|
$expandedValue = self::getExpandedValue($pageView, $request->getUri()->getPath()); |
|
119
|
|
|
|
|
120
|
|
|
if ($expandedValue === null) |
|
121
|
|
|
{ |
|
122
|
|
|
return WebServer::return404(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if ($this->hasBeenTouched($pageView)) |
|
126
|
|
|
{ |
|
127
|
|
|
$pageView->readContent(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return WebServer::return200($compiler->renderRepeaterPageView($pageView, $expandedValue)); |
|
131
|
|
|
}; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Return the appropriate action based on a PageView's type. |
|
136
|
|
|
* |
|
137
|
|
|
* @param BasePageView|DynamicPageView|RepeaterPageView|StaticPageView $pageView |
|
138
|
|
|
* @param Compiler $compiler |
|
139
|
|
|
* |
|
140
|
|
|
* @return \Closure |
|
141
|
|
|
*/ |
|
142
|
|
|
private function createAction(BasePageView $pageView, Compiler $compiler) |
|
143
|
|
|
{ |
|
144
|
|
|
switch ($pageView->getType()) |
|
145
|
|
|
{ |
|
146
|
|
|
case BasePageView::STATIC_TYPE: |
|
147
|
|
|
return $this->staticPageViewAction($pageView, $compiler); |
|
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
case BasePageView::DYNAMIC_TYPE: |
|
150
|
|
|
return $this->dynamicPageViewAction($pageView, $compiler); |
|
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
case BasePageView::REPEATER_TYPE: |
|
153
|
|
|
return $this->repeaterPageViewAction($pageView, $compiler); |
|
|
|
|
|
|
154
|
|
|
|
|
155
|
|
|
default: |
|
156
|
|
|
return function () { |
|
157
|
|
|
$errMsg = 'This URL type has not yet been implemented.'; |
|
158
|
|
|
|
|
159
|
|
|
return new Response(501, ['Content-Type' => 'text/plain'], $errMsg); |
|
160
|
|
|
}; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Check to see if a file has been touched since we last read it. |
|
166
|
|
|
* |
|
167
|
|
|
* @param ReadableDocument $document |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool True if the file has been modified since it was last accessed |
|
170
|
|
|
*/ |
|
171
|
|
|
private function hasBeenTouched(ReadableDocument $document) |
|
172
|
|
|
{ |
|
173
|
|
|
$rPath = $document->getRelativeFilePath(); |
|
174
|
|
|
|
|
175
|
|
|
if (!isset($this->lastModified[$rPath])) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->lastModified[$rPath] = $document->getLastModified(); |
|
178
|
|
|
|
|
179
|
|
|
return true; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $document->getLastModified() > $this->lastModified[$rPath]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Create a FastRoute Dispatcher. |
|
187
|
|
|
* |
|
188
|
|
|
* @param RouteMapper $router |
|
189
|
|
|
* @param Compiler $compiler |
|
190
|
|
|
* |
|
191
|
|
|
* @return \FastRoute\Dispatcher |
|
192
|
|
|
*/ |
|
193
|
|
|
public static function create(RouteMapper $router, Compiler $compiler) |
|
194
|
|
|
{ |
|
195
|
|
|
self::$baseUrl = $router->getBaseUrl(); |
|
196
|
|
|
|
|
197
|
|
|
return \FastRoute\simpleDispatcher(function (RouteCollector $r) use ($router, $compiler) { |
|
198
|
|
|
$dispatcher = new Controller(); |
|
199
|
|
|
|
|
200
|
|
|
foreach ($router->getRouteMapping() as $route => $pageView) |
|
201
|
|
|
{ |
|
202
|
|
|
$r->get($route, $dispatcher->createAction($pageView, $compiler)); |
|
203
|
|
|
} |
|
204
|
|
|
}); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* Normalize a given URL. |
|
209
|
|
|
* |
|
210
|
|
|
* A normalized URL is one with `baseurl` stripped away from it. This is necessary because all permalinks in stakx |
|
211
|
|
|
* are handled without the base so it's necessary to be able to reference correct correct permalinks. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $url |
|
214
|
|
|
* |
|
215
|
|
|
* @return mixed |
|
216
|
|
|
*/ |
|
217
|
|
|
public static function normalizeUrl($url) |
|
218
|
|
|
{ |
|
219
|
|
|
return str_replace(self::$baseUrl, '/', $url); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Find a ContentItem from a Dynamic PageView or null if it doesn't exist. |
|
224
|
|
|
* |
|
225
|
|
|
* @param DynamicPageView $pageView |
|
226
|
|
|
* @param $permalink |
|
227
|
|
|
* |
|
228
|
|
|
* @return CollectableItem|ReadableDocument|TemplateReadyDocument|null |
|
229
|
|
|
*/ |
|
230
|
|
|
private static function getContentItem(DynamicPageView $pageView, $permalink) |
|
231
|
|
|
{ |
|
232
|
|
|
$permalink = self::normalizeUrl($permalink); |
|
|
|
|
|
|
233
|
|
|
|
|
234
|
|
|
foreach ($pageView->getCollectableItems() as $collectableItem) |
|
235
|
|
|
{ |
|
236
|
|
|
if ($collectableItem['permalink'] === $permalink) |
|
237
|
|
|
{ |
|
238
|
|
|
return $collectableItem; |
|
239
|
|
|
} |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return null; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* Find the ExpandedValue from a Repeater PageView or null if it doesn't exist. |
|
247
|
|
|
* |
|
248
|
|
|
* @param RepeaterPageView $pageView |
|
249
|
|
|
* @param $permalink |
|
250
|
|
|
* |
|
251
|
|
|
* @return ExpandedValue|null |
|
252
|
|
|
*/ |
|
253
|
|
|
private static function getExpandedValue(RepeaterPageView $pageView, $permalink) |
|
254
|
|
|
{ |
|
255
|
|
|
$url = self::normalizeUrl($permalink); |
|
256
|
|
|
$repeaterPermalinks = $pageView->getRepeaterPermalinks(); |
|
257
|
|
|
|
|
258
|
|
|
foreach ($repeaterPermalinks as $expandedValue) |
|
259
|
|
|
{ |
|
260
|
|
|
if ($expandedValue->getEvaluated() === $url) |
|
261
|
|
|
{ |
|
262
|
|
|
return $expandedValue; |
|
263
|
|
|
} |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return null; |
|
267
|
|
|
} |
|
268
|
|
|
} |
|
269
|
|
|
|