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; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Document\BasePageView; |
11
|
|
|
use allejo\stakx\Document\DynamicPageView; |
12
|
|
|
use allejo\stakx\Document\PermalinkDocument; |
13
|
|
|
use allejo\stakx\Document\RepeaterPageView; |
14
|
|
|
use allejo\stakx\Document\StaticPageView; |
15
|
|
|
use allejo\stakx\Document\TemplateReadyDocument; |
16
|
|
|
use allejo\stakx\Event\CompileProcessPostRenderPageView; |
17
|
|
|
use allejo\stakx\Event\CompileProcessPreRenderPageView; |
18
|
|
|
use allejo\stakx\Event\CompileProcessTemplateCreation; |
19
|
|
|
use allejo\stakx\Exception\FileAwareException; |
20
|
|
|
use allejo\stakx\Filesystem\Folder; |
21
|
|
|
use allejo\stakx\FrontMatter\ExpandedValue; |
22
|
|
|
use allejo\stakx\Manager\CollectionManager; |
23
|
|
|
use allejo\stakx\Manager\DataManager; |
24
|
|
|
use allejo\stakx\Manager\MenuManager; |
25
|
|
|
use allejo\stakx\Manager\PageManager; |
26
|
|
|
use allejo\stakx\Templating\TemplateBridgeInterface; |
27
|
|
|
use allejo\stakx\Templating\TemplateErrorInterface; |
28
|
|
|
use allejo\stakx\Templating\TemplateInterface; |
29
|
|
|
use Psr\Log\LoggerInterface; |
30
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* This class takes care of rendering the Twig body of PageViews with the respective information and it also takes care |
34
|
|
|
* of writing the rendered Twig to the filesystem. |
35
|
|
|
* |
36
|
|
|
* @internal |
37
|
|
|
* |
38
|
|
|
* @since 0.1.1 |
39
|
|
|
*/ |
40
|
|
|
class Compiler |
41
|
|
|
{ |
42
|
|
|
/** @var string|false */ |
43
|
|
|
private $redirectTemplate; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* All of the PageViews handled by this Compiler instance indexed by their file paths relative to the site root. |
47
|
|
|
* |
48
|
|
|
* ``` |
49
|
|
|
* array['_pages/index.html.twig'] = &PageView; |
50
|
|
|
* ``` |
51
|
|
|
* |
52
|
|
|
* @var BasePageView[] |
53
|
|
|
*/ |
54
|
|
|
private $pageViewsFlattened; |
55
|
|
|
|
56
|
|
|
/** @var string[] */ |
57
|
|
|
private $templateMapping; |
58
|
|
|
|
59
|
|
|
/** @var Folder */ |
60
|
|
|
private $folder; |
61
|
|
|
|
62
|
|
|
/** @var string */ |
63
|
|
|
private $theme; |
64
|
|
|
|
65
|
|
|
private $templateBridge; |
66
|
|
|
private $pageManager; |
67
|
|
|
private $eventDispatcher; |
68
|
|
|
private $configuration; |
69
|
|
|
|
70
|
12 |
|
public function __construct( |
71
|
|
|
TemplateBridgeInterface $templateBridge, |
72
|
|
|
Configuration $configuration, |
73
|
|
|
CollectionManager $collectionManager, |
74
|
|
|
DataManager $dataManager, |
75
|
|
|
MenuManager $menuManager, |
76
|
|
|
PageManager $pageManager, |
77
|
|
|
EventDispatcherInterface $eventDispatcher, |
78
|
|
|
LoggerInterface $logger |
79
|
|
|
) { |
80
|
12 |
|
$this->templateBridge = $templateBridge; |
81
|
12 |
|
$this->theme = ''; |
82
|
12 |
|
$this->pageManager = $pageManager; |
83
|
12 |
|
$this->eventDispatcher = $eventDispatcher; |
84
|
12 |
|
$this->logger = $logger; |
|
|
|
|
85
|
12 |
|
$this->configuration = $configuration; |
86
|
|
|
|
87
|
12 |
|
$this->pageViewsFlattened = &$pageManager->getPageViewsFlattened(); |
88
|
12 |
|
$this->redirectTemplate = $this->configuration->getRedirectTemplate(); |
89
|
|
|
|
90
|
|
|
// Global variables maintained by stakx |
91
|
12 |
|
$this->templateBridge->setGlobalVariable('site', $configuration->getConfiguration()); |
92
|
12 |
|
$this->templateBridge->setGlobalVariable('data', $dataManager->getJailedDataItems()); |
93
|
12 |
|
$this->templateBridge->setGlobalVariable('collections', $collectionManager->getJailedCollections()); |
94
|
12 |
|
$this->templateBridge->setGlobalVariable('menu', $menuManager->getSiteMenu()); |
95
|
12 |
|
$this->templateBridge->setGlobalVariable('pages', $pageManager->getJailedStaticPageViews()); |
96
|
12 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param Folder $folder |
100
|
|
|
*/ |
101
|
12 |
|
public function setTargetFolder(Folder $folder) |
102
|
|
|
{ |
103
|
12 |
|
$this->folder = $folder; |
104
|
12 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $themeName |
108
|
|
|
*/ |
109
|
|
|
public function setThemeName($themeName) |
110
|
|
|
{ |
111
|
|
|
$this->theme = $themeName; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/// |
115
|
|
|
// Twig parent templates |
116
|
|
|
/// |
117
|
|
|
|
118
|
|
|
public function getTemplateMappings() |
119
|
|
|
{ |
120
|
|
|
return $this->templateMapping; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/// |
124
|
|
|
// IO Functionality |
125
|
|
|
/// |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Compile all of the PageViews registered with the compiler. |
129
|
|
|
* |
130
|
|
|
* @since 0.1.0 |
131
|
|
|
*/ |
132
|
12 |
|
public function compileAll() |
133
|
|
|
{ |
134
|
12 |
|
foreach ($this->pageViewsFlattened as &$pageView) |
135
|
|
|
{ |
136
|
12 |
|
$this->compilePageView($pageView); |
137
|
|
|
} |
138
|
12 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Compile an individual PageView item. |
142
|
|
|
* |
143
|
|
|
* This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
144
|
|
|
* respective target file. |
145
|
|
|
* |
146
|
|
|
* @param DynamicPageView|RepeaterPageView|StaticPageView $pageView The PageView that needs to be compiled |
|
|
|
|
147
|
|
|
* |
148
|
|
|
* @since 0.1.1 |
149
|
|
|
*/ |
150
|
12 |
|
public function compilePageView(BasePageView &$pageView) |
151
|
|
|
{ |
152
|
12 |
|
$this->templateBridge->setGlobalVariable('__currentTemplate', $pageView->getAbsoluteFilePath()); |
153
|
12 |
|
$this->logger->debug('Compiling {type} PageView: {pageview}', [ |
154
|
12 |
|
'pageview' => $pageView->getRelativeFilePath(), |
155
|
12 |
|
'type' => $pageView->getType(), |
156
|
|
|
]); |
157
|
|
|
|
158
|
|
|
try |
159
|
|
|
{ |
160
|
12 |
|
switch ($pageView->getType()) |
161
|
|
|
{ |
162
|
|
|
case BasePageView::STATIC_TYPE: |
163
|
10 |
|
$this->compileStaticPageView($pageView); |
|
|
|
|
164
|
10 |
|
$this->compileStandardRedirects($pageView); |
165
|
10 |
|
break; |
166
|
|
|
|
167
|
|
|
case BasePageView::DYNAMIC_TYPE: |
168
|
|
|
$this->compileDynamicPageView($pageView); |
|
|
|
|
169
|
|
|
$this->compileStandardRedirects($pageView); |
170
|
|
|
break; |
171
|
|
|
|
172
|
|
|
case BasePageView::REPEATER_TYPE: |
173
|
3 |
|
$this->compileRepeaterPageView($pageView); |
|
|
|
|
174
|
3 |
|
$this->compileExpandedRedirects($pageView); |
175
|
12 |
|
break; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
catch (TemplateErrorInterface $e) |
179
|
|
|
{ |
180
|
|
|
throw new FileAwareException( |
181
|
|
|
$e->getMessage(), |
182
|
|
|
$e->getCode(), |
183
|
|
|
$e, |
184
|
|
|
$pageView->getRelativeFilePath(), |
185
|
|
|
$e->getTemplateLine() + $pageView->getLineOffset() |
|
|
|
|
186
|
|
|
); |
187
|
|
|
} |
188
|
12 |
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Write the compiled output for a static PageView. |
192
|
|
|
* |
193
|
|
|
* @since 0.1.1 |
194
|
|
|
* |
195
|
|
|
* @throws TemplateErrorInterface |
196
|
|
|
*/ |
197
|
10 |
|
private function compileStaticPageView(StaticPageView &$pageView) |
198
|
|
|
{ |
199
|
10 |
|
$pageView->compile(); |
200
|
|
|
|
201
|
10 |
|
$this->writeToFilesystem( |
202
|
10 |
|
$pageView->getTargetFile(), |
203
|
10 |
|
$this->renderStaticPageView($pageView), |
204
|
10 |
|
BasePageView::STATIC_TYPE |
205
|
|
|
); |
206
|
10 |
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Write the compiled output for a dynamic PageView. |
210
|
|
|
* |
211
|
|
|
* @param DynamicPageView $pageView |
212
|
|
|
* |
213
|
|
|
* @since 0.1.1 |
214
|
|
|
* |
215
|
|
|
* @throws TemplateErrorInterface |
216
|
|
|
*/ |
217
|
|
|
private function compileDynamicPageView(DynamicPageView &$pageView) |
218
|
|
|
{ |
219
|
|
|
$contentItems = $pageView->getCollectableItems(); |
220
|
|
|
$template = $this->createTwigTemplate($pageView); |
221
|
|
|
|
222
|
|
|
foreach ($contentItems as &$contentItem) |
223
|
|
|
{ |
224
|
|
|
if ($contentItem->isDraft() && !Service::hasRunTimeFlag(RuntimeStatus::USING_DRAFTS)) |
225
|
|
|
{ |
226
|
|
|
$this->logger->debug('{file}: marked as a draft', [ |
227
|
|
|
'file' => $contentItem->getRelativeFilePath(), |
228
|
|
|
]); |
229
|
|
|
|
230
|
|
|
continue; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
$this->writeToFilesystem( |
234
|
|
|
$contentItem->getTargetFile(), |
235
|
|
|
$this->renderDynamicPageView($template, $contentItem), |
|
|
|
|
236
|
|
|
BasePageView::DYNAMIC_TYPE |
237
|
|
|
); |
238
|
|
|
|
239
|
|
|
$this->compileStandardRedirects($contentItem); |
|
|
|
|
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Write the compiled output for a repeater PageView. |
245
|
|
|
* |
246
|
|
|
* @param RepeaterPageView $pageView |
247
|
|
|
* |
248
|
|
|
* @since 0.1.1 |
249
|
|
|
* |
250
|
|
|
* @throws TemplateErrorInterface |
251
|
|
|
*/ |
252
|
3 |
|
private function compileRepeaterPageView(RepeaterPageView &$pageView) |
253
|
|
|
{ |
254
|
3 |
|
$pageView->rewindPermalink(); |
255
|
|
|
|
256
|
3 |
|
$template = $this->createTwigTemplate($pageView); |
257
|
3 |
|
$permalinks = $pageView->getRepeaterPermalinks(); |
|
|
|
|
258
|
|
|
|
259
|
3 |
|
foreach ($permalinks as $permalink) |
260
|
|
|
{ |
261
|
3 |
|
$pageView->bumpPermalink(); |
|
|
|
|
262
|
|
|
|
263
|
3 |
|
$this->writeToFilesystem( |
264
|
3 |
|
$pageView->getTargetFile(), |
265
|
3 |
|
$this->renderRepeaterPageView($template, $pageView, $permalink), |
|
|
|
|
266
|
3 |
|
BasePageView::REPEATER_TYPE |
267
|
|
|
); |
268
|
|
|
} |
269
|
3 |
|
} |
270
|
|
|
|
271
|
|
|
/** |
272
|
|
|
* Write the given $output to the $targetFile as a $fileType PageView. |
273
|
|
|
* |
274
|
|
|
* @param string $targetFile |
275
|
|
|
* @param string $output |
276
|
|
|
* @param string $fileType |
277
|
|
|
*/ |
278
|
12 |
|
private function writeToFilesystem($targetFile, $output, $fileType) |
279
|
|
|
{ |
280
|
12 |
|
$this->logger->notice('Writing {type} PageView file: {file}', [ |
281
|
12 |
|
'type' => $fileType, |
282
|
12 |
|
'file' => $targetFile, |
283
|
|
|
]); |
284
|
12 |
|
$this->folder->writeFile($targetFile, $output); |
285
|
12 |
|
} |
286
|
|
|
|
287
|
|
|
/// |
288
|
|
|
// Redirect handling |
289
|
|
|
/// |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Write redirects for standard redirects. |
293
|
|
|
* |
294
|
|
|
* @throws TemplateErrorInterface |
295
|
|
|
* |
296
|
|
|
* @since 0.1.1 |
297
|
|
|
*/ |
298
|
10 |
|
private function compileStandardRedirects(PermalinkDocument &$pageView) |
299
|
|
|
{ |
300
|
10 |
|
$redirects = $pageView->getRedirects(); |
301
|
|
|
|
302
|
10 |
View Code Duplication |
foreach ($redirects as $redirect) |
|
|
|
|
303
|
|
|
{ |
304
|
1 |
|
$redirectPageView = BasePageView::createRedirect( |
305
|
1 |
|
$redirect, |
306
|
1 |
|
$pageView->getPermalink(), |
307
|
1 |
|
$this->redirectTemplate |
308
|
|
|
); |
309
|
1 |
|
$redirectPageView->evaluateFrontMatter([], [ |
310
|
1 |
|
'site' => $this->configuration->getConfiguration(), |
311
|
|
|
]); |
312
|
|
|
|
313
|
1 |
|
$this->compileStaticPageView($redirectPageView); |
314
|
|
|
} |
315
|
10 |
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* Write redirects for expanded redirects. |
319
|
|
|
* |
320
|
|
|
* @param RepeaterPageView $pageView |
321
|
|
|
* |
322
|
|
|
* @since 0.1.1 |
323
|
|
|
*/ |
324
|
3 |
|
private function compileExpandedRedirects(RepeaterPageView &$pageView) |
325
|
|
|
{ |
326
|
3 |
|
$permalinks = $pageView->getRepeaterPermalinks(); |
327
|
|
|
|
328
|
|
|
/** @var ExpandedValue[] $repeaterRedirect */ |
329
|
3 |
|
foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) |
330
|
|
|
{ |
331
|
|
|
/** |
332
|
|
|
* @var int |
333
|
|
|
* @var ExpandedValue $redirect |
334
|
|
|
*/ |
335
|
1 |
View Code Duplication |
foreach ($repeaterRedirect as $index => $redirect) |
|
|
|
|
336
|
|
|
{ |
337
|
1 |
|
$redirectPageView = BasePageView::createRedirect( |
338
|
1 |
|
$redirect->getEvaluated(), |
339
|
1 |
|
$permalinks[$index]->getEvaluated(), |
340
|
1 |
|
$this->redirectTemplate |
341
|
|
|
); |
342
|
1 |
|
$redirectPageView->evaluateFrontMatter([], [ |
343
|
1 |
|
'site' => $this->configuration->getConfiguration(), |
344
|
|
|
]); |
345
|
|
|
|
346
|
1 |
|
$this->compilePageView($redirectPageView); |
347
|
|
|
} |
348
|
|
|
} |
349
|
3 |
|
} |
350
|
|
|
|
351
|
|
|
/// |
352
|
|
|
// Twig Functionality |
353
|
|
|
/// |
354
|
|
|
|
355
|
|
|
/** |
356
|
|
|
* Get the compiled HTML for a specific iteration of a repeater PageView. |
357
|
|
|
* |
358
|
|
|
* @param TemplateInterface $template |
359
|
|
|
* @param RepeaterPageView $pageView |
360
|
|
|
* @param ExpandedValue $expandedValue |
361
|
|
|
* |
362
|
|
|
* @since 0.1.1 |
363
|
|
|
* |
364
|
|
|
* @return string |
365
|
|
|
*/ |
366
|
3 |
|
private function renderRepeaterPageView(TemplateInterface &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
367
|
|
|
{ |
368
|
|
|
$defaultContext = [ |
369
|
3 |
|
'this' => $pageView->createJail(), |
370
|
|
|
]; |
371
|
|
|
|
372
|
3 |
|
$pageView->evaluateFrontMatter([ |
373
|
3 |
|
'permalink' => $expandedValue->getEvaluated(), |
374
|
3 |
|
'iterators' => $expandedValue->getIterators(), |
375
|
|
|
]); |
376
|
|
|
|
377
|
3 |
|
$preEvent = new CompileProcessPreRenderPageView(BasePageView::REPEATER_TYPE); |
378
|
3 |
|
$this->eventDispatcher->dispatch(CompileProcessPreRenderPageView::NAME, $preEvent); |
379
|
|
|
|
380
|
3 |
|
$context = array_merge($preEvent->getCustomVariables(), $defaultContext); |
381
|
|
|
$output = $template |
382
|
3 |
|
->render($context) |
383
|
|
|
; |
384
|
|
|
|
385
|
3 |
|
$postEvent = new CompileProcessPostRenderPageView(BasePageView::REPEATER_TYPE, $output); |
386
|
3 |
|
$this->eventDispatcher->dispatch(CompileProcessPostRenderPageView::NAME, $postEvent); |
387
|
|
|
|
388
|
3 |
|
return $postEvent->getCompiledOutput(); |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
/** |
392
|
|
|
* Get the compiled HTML for a specific ContentItem. |
393
|
|
|
* |
394
|
|
|
* @since 0.1.1 |
395
|
|
|
* |
396
|
|
|
* @return string |
397
|
|
|
*/ |
398
|
|
View Code Duplication |
private function renderDynamicPageView(TemplateInterface &$template, TemplateReadyDocument &$twigItem) |
|
|
|
|
399
|
|
|
{ |
400
|
|
|
$defaultContext = [ |
401
|
|
|
'this' => $twigItem->createJail(), |
402
|
|
|
]; |
403
|
|
|
|
404
|
|
|
$preEvent = new CompileProcessPreRenderPageView(BasePageView::DYNAMIC_TYPE); |
405
|
|
|
$this->eventDispatcher->dispatch(CompileProcessPreRenderPageView::NAME, $preEvent); |
406
|
|
|
|
407
|
|
|
$context = array_merge($preEvent->getCustomVariables(), $defaultContext); |
408
|
|
|
$output = $template |
409
|
|
|
->render($context) |
410
|
|
|
; |
411
|
|
|
|
412
|
|
|
$postEvent = new CompileProcessPostRenderPageView(BasePageView::DYNAMIC_TYPE, $output); |
413
|
|
|
$this->eventDispatcher->dispatch(CompileProcessPostRenderPageView::NAME, $postEvent); |
414
|
|
|
|
415
|
|
|
return $postEvent->getCompiledOutput(); |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Get the compiled HTML for a static PageView. |
420
|
|
|
* |
421
|
|
|
* @since 0.1.1 |
422
|
|
|
* |
423
|
|
|
* @throws TemplateErrorInterface |
424
|
|
|
* |
425
|
|
|
* @return string |
426
|
|
|
*/ |
427
|
10 |
View Code Duplication |
private function renderStaticPageView(StaticPageView &$pageView) |
|
|
|
|
428
|
|
|
{ |
429
|
|
|
$defaultContext = [ |
430
|
10 |
|
'this' => $pageView->createJail(), |
431
|
|
|
]; |
432
|
|
|
|
433
|
10 |
|
$preEvent = new CompileProcessPreRenderPageView(BasePageView::STATIC_TYPE); |
434
|
10 |
|
$this->eventDispatcher->dispatch(CompileProcessPreRenderPageView::NAME, $preEvent); |
435
|
|
|
|
436
|
10 |
|
$context = array_merge($preEvent->getCustomVariables(), $defaultContext); |
437
|
|
|
$output = $this |
438
|
10 |
|
->createTwigTemplate($pageView) |
439
|
10 |
|
->render($context) |
440
|
|
|
; |
441
|
|
|
|
442
|
10 |
|
$postEvent = new CompileProcessPostRenderPageView(BasePageView::STATIC_TYPE, $output); |
443
|
10 |
|
$this->eventDispatcher->dispatch(CompileProcessPostRenderPageView::NAME, $postEvent); |
444
|
|
|
|
445
|
10 |
|
return $postEvent->getCompiledOutput(); |
446
|
|
|
} |
447
|
|
|
|
448
|
|
|
/** |
449
|
|
|
* Create a Twig template that just needs an array to render. |
450
|
|
|
* |
451
|
|
|
* @since 0.1.1 |
452
|
|
|
* |
453
|
|
|
* @throws TemplateErrorInterface |
454
|
|
|
* |
455
|
|
|
* @return TemplateInterface |
456
|
|
|
*/ |
457
|
12 |
|
private function createTwigTemplate(BasePageView &$pageView) |
458
|
|
|
{ |
459
|
|
|
try |
460
|
|
|
{ |
461
|
12 |
|
$template = $this->templateBridge->createTemplate($pageView->getContent()); |
462
|
|
|
|
463
|
12 |
|
$this->templateMapping[$template->getTemplateName()] = $pageView->getRelativeFilePath(); |
464
|
|
|
|
465
|
12 |
|
$event = new CompileProcessTemplateCreation($pageView, $template, $this->theme); |
466
|
12 |
|
$this->eventDispatcher->dispatch(CompileProcessTemplateCreation::NAME, $event); |
467
|
|
|
|
468
|
12 |
|
return $template; |
469
|
|
|
} |
470
|
|
|
catch (TemplateErrorInterface $e) |
471
|
|
|
{ |
472
|
|
|
$e |
473
|
|
|
->setTemplateLine($e->getTemplateLine() + $pageView->getLineOffset()) |
474
|
|
|
->setContent($pageView->getContent()) |
475
|
|
|
->setName($pageView->getRelativeFilePath()) |
476
|
|
|
->setRelativeFilePath($pageView->getRelativeFilePath()) |
477
|
|
|
->buildException() |
478
|
|
|
; |
479
|
|
|
|
480
|
|
|
throw $e; |
481
|
|
|
} |
482
|
|
|
} |
483
|
|
|
} |
484
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: