1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Command\BuildableCommand; |
11
|
|
|
use allejo\stakx\Document\ContentItem; |
12
|
|
|
use allejo\stakx\Document\DynamicPageView; |
13
|
|
|
use allejo\stakx\Document\PageView; |
14
|
|
|
use allejo\stakx\Document\PermalinkDocument; |
15
|
|
|
use allejo\stakx\Document\RepeaterPageView; |
16
|
|
|
use allejo\stakx\Document\TwigDocument; |
17
|
|
|
use allejo\stakx\Exception\FileAwareException; |
18
|
|
|
use allejo\stakx\FrontMatter\ExpandedValue; |
19
|
|
|
use allejo\stakx\Manager\BaseManager; |
20
|
|
|
use allejo\stakx\Manager\ThemeManager; |
21
|
|
|
use allejo\stakx\Manager\TwigManager; |
22
|
|
|
use allejo\stakx\System\Folder; |
23
|
|
|
use allejo\stakx\System\FilePath; |
24
|
|
|
use Twig_Environment; |
25
|
|
|
use Twig_Error_Runtime; |
26
|
|
|
use Twig_Error_Syntax; |
27
|
|
|
use Twig_Source; |
28
|
|
|
use Twig_Template; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This class takes care of rendering the Twig body of PageViews with the respective information and it also takes care |
32
|
|
|
* of writing the rendered Twig to the filesystem. |
33
|
|
|
* |
34
|
|
|
* @internal |
35
|
|
|
* |
36
|
|
|
* @since 0.1.1 |
37
|
|
|
*/ |
38
|
|
|
class Compiler extends BaseManager |
39
|
|
|
{ |
40
|
|
|
/** @var string|false */ |
41
|
|
|
private $redirectTemplate; |
42
|
|
|
|
43
|
|
|
/** @var PageView[][] */ |
44
|
|
|
private $importDependencies; |
45
|
|
|
|
46
|
|
|
/** @var Twig_Template[] */ |
47
|
|
|
private $templateDependencies; |
48
|
|
|
|
49
|
|
|
/** @var PageView[] */ |
50
|
|
|
private $pageViewsFlattened; |
51
|
|
|
|
52
|
|
|
/** @var string[] */ |
53
|
|
|
private $templateMapping; |
54
|
|
|
|
55
|
|
|
/** @var PageView[][] */ |
56
|
|
|
private $pageViews; |
57
|
|
|
|
58
|
|
|
/** @var Folder */ |
59
|
|
|
private $folder; |
60
|
|
|
|
61
|
|
|
/** @var string */ |
62
|
|
|
private $theme; |
63
|
|
|
|
64
|
|
|
/** @var Twig_Environment */ |
65
|
|
|
private $twig; |
66
|
|
|
|
67
|
14 |
|
public function __construct() |
68
|
|
|
{ |
69
|
14 |
|
parent::__construct(); |
70
|
|
|
|
71
|
14 |
|
$this->twig = TwigManager::getInstance(); |
72
|
14 |
|
$this->theme = ''; |
73
|
14 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string|false $template |
77
|
|
|
*/ |
78
|
|
|
public function setRedirectTemplate($template) |
79
|
|
|
{ |
80
|
|
|
$this->redirectTemplate = $template; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Folder $folder |
85
|
|
|
*/ |
86
|
14 |
|
public function setTargetFolder(Folder $folder) |
87
|
|
|
{ |
88
|
14 |
|
$this->folder = $folder; |
89
|
14 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param PageView[][] $pageViews |
93
|
|
|
* @param PageView[] $pageViewsFlattened |
94
|
|
|
*/ |
95
|
14 |
|
public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
96
|
|
|
{ |
97
|
14 |
|
$this->pageViews = &$pageViews; |
98
|
14 |
|
$this->pageViewsFlattened = &$pageViewsFlattened; |
99
|
14 |
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param string $themeName |
103
|
|
|
*/ |
104
|
|
|
public function setThemeName($themeName) |
105
|
|
|
{ |
106
|
|
|
$this->theme = $themeName; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/// |
110
|
|
|
// Twig parent templates |
111
|
|
|
/// |
112
|
|
|
|
113
|
|
|
public function isImportDependency($filePath) |
114
|
|
|
{ |
115
|
|
|
return isset($this->importDependencies[$filePath]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Check whether a given file path is used as a parent template by a PageView |
120
|
|
|
* |
121
|
|
|
* @param string $filePath |
122
|
|
|
* |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
|
|
public function isParentTemplate($filePath) |
126
|
|
|
{ |
127
|
|
|
return isset($this->templateDependencies[$filePath]); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Rebuild all of the PageViews that used a given template as a parent |
132
|
|
|
* |
133
|
|
|
* @param string $filePath The file path to the parent Twig template |
134
|
|
|
*/ |
135
|
|
|
public function refreshParent($filePath) |
136
|
|
|
{ |
137
|
|
|
foreach ($this->templateDependencies[$filePath] as &$parentTemplate) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$this->compilePageView($parentTemplate); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getTemplateMappings() |
144
|
|
|
{ |
145
|
|
|
return $this->templateMapping; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/// |
149
|
|
|
// IO Functionality |
150
|
|
|
/// |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Compile all of the PageViews registered with the compiler. |
154
|
|
|
* |
155
|
|
|
* @since 0.1.0 |
156
|
|
|
*/ |
157
|
14 |
|
public function compileAll() |
158
|
|
|
{ |
159
|
14 |
|
foreach ($this->pageViewsFlattened as &$pageView) |
160
|
|
|
{ |
161
|
14 |
|
$this->compilePageView($pageView); |
162
|
|
|
} |
163
|
14 |
|
} |
164
|
|
|
|
165
|
|
|
public function compileImportDependencies($filePath) |
166
|
|
|
{ |
167
|
|
|
foreach ($this->importDependencies[$filePath] as &$dependent) |
168
|
|
|
{ |
169
|
|
|
$this->compilePageView($dependent); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function compileSome(array $filter = array()) |
174
|
|
|
{ |
175
|
|
|
/** @var PageView $pageView */ |
176
|
|
|
foreach ($this->pageViewsFlattened as &$pageView) |
177
|
|
|
{ |
178
|
|
|
$ns = $filter['namespace']; |
179
|
|
|
|
180
|
|
|
if ($pageView->hasTwigDependency($ns, $filter['dependency']) || |
181
|
|
|
$pageView->hasTwigDependency($ns, null) |
182
|
|
|
) { |
183
|
|
|
$this->compilePageView($pageView); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Compile an individual PageView item. |
190
|
|
|
* |
191
|
|
|
* This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
192
|
|
|
* respective target file. |
193
|
|
|
* |
194
|
|
|
* @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
195
|
|
|
* |
196
|
|
|
* @since 0.1.1 |
197
|
|
|
*/ |
198
|
14 |
|
public function compilePageView(PageView &$pageView) |
199
|
|
|
{ |
200
|
14 |
|
$this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
201
|
14 |
|
$this->output->debug('Compiling {type} PageView: {pageview}', array( |
202
|
14 |
|
'pageview' => $pageView->getRelativeFilePath(), |
203
|
14 |
|
'type' => $pageView->getType() |
204
|
|
|
)); |
205
|
|
|
|
206
|
|
|
try |
207
|
|
|
{ |
208
|
14 |
|
switch ($pageView->getType()) |
209
|
|
|
{ |
210
|
14 |
|
case PageView::STATIC_TYPE: |
211
|
10 |
|
$this->compileStaticPageView($pageView); |
212
|
10 |
|
$this->compileStandardRedirects($pageView); |
213
|
10 |
|
break; |
214
|
|
|
|
215
|
4 |
|
case PageView::DYNAMIC_TYPE: |
216
|
2 |
|
$this->compileDynamicPageViews($pageView); |
217
|
2 |
|
$this->compileStandardRedirects($pageView); |
218
|
2 |
|
break; |
219
|
|
|
|
220
|
2 |
|
case PageView::REPEATER_TYPE: |
221
|
2 |
|
$this->compileRepeaterPageViews($pageView); |
222
|
2 |
|
$this->compileExpandedRedirects($pageView); |
223
|
14 |
|
break; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
catch (Twig_Error_Runtime $e) |
227
|
|
|
{ |
228
|
|
|
throw new FileAwareException( |
229
|
|
|
$e->getRawMessage(), |
230
|
|
|
$e->getCode(), |
231
|
|
|
$e, |
232
|
|
|
$pageView->getRelativeFilePath(), |
233
|
|
|
$e->getTemplateLine() + $pageView->getLineOffset() |
|
|
|
|
234
|
|
|
); |
235
|
|
|
} |
236
|
14 |
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Write the compiled output for a static PageView. |
240
|
|
|
* |
241
|
|
|
* @param PageView $pageView |
242
|
|
|
* |
243
|
|
|
* @since 0.1.1 |
244
|
|
|
*/ |
245
|
10 |
|
private function compileStaticPageView(PageView &$pageView) |
246
|
|
|
{ |
247
|
10 |
|
$targetFile = $pageView->getTargetFile(); |
248
|
10 |
|
$output = $this->renderStaticPageView($pageView); |
249
|
|
|
|
250
|
10 |
|
$this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
251
|
10 |
|
$this->folder->writeFile($targetFile, $output); |
252
|
10 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Write the compiled output for a dynamic PageView. |
256
|
|
|
* |
257
|
|
|
* @param DynamicPageView $pageView |
258
|
|
|
* |
259
|
|
|
* @since 0.1.1 |
260
|
|
|
*/ |
261
|
2 |
|
private function compileDynamicPageViews(DynamicPageView &$pageView) |
262
|
|
|
{ |
263
|
2 |
|
$contentItems = $pageView->getRepeatableItems(); |
264
|
2 |
|
$template = $this->createTwigTemplate($pageView); |
265
|
|
|
|
266
|
2 |
|
foreach ($contentItems as &$contentItem) |
267
|
|
|
{ |
268
|
2 |
|
if ($contentItem->isDraft() && !Service::getParameter(BuildableCommand::USE_DRAFTS)) |
269
|
|
|
{ |
270
|
1 |
|
$this->output->debug('{file}: marked as a draft', array( |
271
|
1 |
|
'file' => $contentItem->getRelativeFilePath() |
272
|
|
|
)); |
273
|
|
|
|
274
|
1 |
|
continue; |
275
|
|
|
} |
276
|
|
|
|
277
|
2 |
|
$targetFile = $contentItem->getTargetFile(); |
278
|
2 |
|
$output = $this->renderDynamicPageView($template, $contentItem); |
|
|
|
|
279
|
|
|
|
280
|
2 |
|
$this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
281
|
2 |
|
$this->folder->writeFile($targetFile, $output); |
282
|
|
|
|
283
|
2 |
|
$this->compileStandardRedirects($contentItem); |
|
|
|
|
284
|
|
|
} |
285
|
2 |
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* Write the compiled output for a repeater PageView. |
289
|
|
|
* |
290
|
|
|
* @param RepeaterPageView $pageView |
291
|
|
|
* |
292
|
|
|
* @since 0.1.1 |
293
|
|
|
*/ |
294
|
2 |
|
private function compileRepeaterPageViews(RepeaterPageView &$pageView) |
295
|
|
|
{ |
296
|
2 |
|
$pageView->rewindPermalink(); |
297
|
|
|
|
298
|
2 |
|
$template = $this->createTwigTemplate($pageView); |
299
|
2 |
|
$permalinks = $pageView->getRepeaterPermalinks(); |
|
|
|
|
300
|
|
|
|
301
|
2 |
|
foreach ($permalinks as $permalink) |
302
|
|
|
{ |
303
|
2 |
|
$pageView->bumpPermalink(); |
|
|
|
|
304
|
2 |
|
$targetFile = $pageView->getTargetFile(); |
305
|
2 |
|
$output = $this->renderRepeaterPageView($template, $pageView, $permalink); |
|
|
|
|
306
|
|
|
|
307
|
2 |
|
$this->output->notice('Writing repeater file: {file}', array('file' => $targetFile)); |
308
|
2 |
|
$this->folder->writeFile($targetFile, $output); |
309
|
|
|
} |
310
|
2 |
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* @deprecated |
314
|
|
|
* |
315
|
|
|
* @todo This function needs to be rewritten or removed. Something |
316
|
|
|
* |
317
|
|
|
* @param ContentItem $contentItem |
318
|
|
|
*/ |
319
|
|
|
public function compileContentItem(ContentItem &$contentItem) |
320
|
|
|
{ |
321
|
|
|
$pageView = &$contentItem->getPageView(); |
322
|
|
|
$template = $this->createTwigTemplate($pageView); |
323
|
|
|
|
324
|
|
|
$this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
325
|
|
|
$contentItem->evaluateFrontMatter($pageView->getFrontMatter(false)); |
326
|
|
|
|
327
|
|
|
$targetFile = $contentItem->getTargetFile(); |
328
|
|
|
$output = $this->renderDynamicPageView($template, $contentItem); |
329
|
|
|
|
330
|
|
|
$this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
331
|
|
|
$this->folder->writeFile($targetFile, $output); |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/// |
335
|
|
|
// Redirect handling |
336
|
|
|
/// |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Write redirects for standard redirects. |
340
|
|
|
* |
341
|
|
|
* @param PermalinkDocument $pageView |
342
|
|
|
* |
343
|
|
|
* @since 0.1.1 |
344
|
|
|
*/ |
345
|
12 |
|
private function compileStandardRedirects(PermalinkDocument &$pageView) |
346
|
|
|
{ |
347
|
12 |
|
$redirects = $pageView->getRedirects(); |
348
|
|
|
|
349
|
12 |
|
foreach ($redirects as $redirect) |
|
|
|
|
350
|
|
|
{ |
351
|
4 |
|
$redirectPageView = PageView::createRedirect( |
352
|
4 |
|
$redirect, |
353
|
4 |
|
$pageView->getPermalink(), |
354
|
4 |
|
$this->redirectTemplate |
355
|
|
|
); |
356
|
|
|
|
357
|
4 |
|
$this->compileStaticPageView($redirectPageView); |
358
|
|
|
} |
359
|
12 |
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Write redirects for expanded redirects. |
363
|
|
|
* |
364
|
|
|
* @param RepeaterPageView $pageView |
365
|
|
|
* |
366
|
|
|
* @since 0.1.1 |
367
|
|
|
*/ |
368
|
2 |
|
private function compileExpandedRedirects(RepeaterPageView &$pageView) |
369
|
|
|
{ |
370
|
2 |
|
$permalinks = $pageView->getRepeaterPermalinks(); |
371
|
|
|
|
372
|
|
|
/** @var ExpandedValue[] $repeaterRedirect */ |
373
|
2 |
|
foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) |
374
|
|
|
{ |
375
|
|
|
/** |
376
|
|
|
* @var int $index |
377
|
|
|
* @var ExpandedValue $redirect |
378
|
|
|
*/ |
379
|
|
|
foreach ($repeaterRedirect as $index => $redirect) |
380
|
|
|
{ |
381
|
|
|
$redirectPageView = PageView::createRedirect( |
382
|
|
|
$redirect->getEvaluated(), |
383
|
|
|
$permalinks[$index]->getEvaluated(), |
384
|
|
|
$this->redirectTemplate |
385
|
|
|
); |
386
|
|
|
$this->compilePageView($redirectPageView); |
387
|
|
|
} |
388
|
|
|
} |
389
|
2 |
|
} |
390
|
|
|
|
391
|
|
|
/// |
392
|
|
|
// Twig Functionality |
393
|
|
|
/// |
394
|
|
|
|
395
|
|
|
/** |
396
|
|
|
* Get the compiled HTML for a specific iteration of a repeater PageView. |
397
|
|
|
* |
398
|
|
|
* @param Twig_Template $template |
399
|
|
|
* @param PageView $pageView |
400
|
|
|
* @param ExpandedValue $expandedValue |
401
|
|
|
* |
402
|
|
|
* @since 0.1.1 |
403
|
|
|
* |
404
|
|
|
* @return string |
405
|
|
|
*/ |
406
|
2 |
|
private function renderRepeaterPageView(Twig_Template &$template, RepeaterPageView &$pageView, ExpandedValue &$expandedValue) |
407
|
|
|
{ |
408
|
2 |
|
$pageView->setFrontMatter(array( |
409
|
2 |
|
'permalink' => $expandedValue->getEvaluated(), |
410
|
2 |
|
'iterators' => $expandedValue->getIterators(), |
411
|
|
|
)); |
412
|
|
|
|
413
|
|
|
return $template |
414
|
2 |
|
->render(array( |
415
|
2 |
|
'this' => $pageView->createJail(), |
416
|
|
|
)); |
417
|
|
|
} |
418
|
|
|
|
419
|
|
|
/** |
420
|
|
|
* Get the compiled HTML for a specific ContentItem. |
421
|
|
|
* |
422
|
|
|
* @param Twig_Template $template |
423
|
|
|
* @param TwigDocument $twigItem |
424
|
|
|
* |
425
|
|
|
* @return string |
426
|
|
|
* @since 0.1.1 |
427
|
|
|
* |
428
|
|
|
*/ |
429
|
2 |
|
private function renderDynamicPageView(Twig_Template &$template, TwigDocument &$twigItem) |
430
|
|
|
{ |
431
|
|
|
return $template |
432
|
2 |
|
->render(array( |
433
|
2 |
|
'this' => $twigItem->createJail(), |
434
|
|
|
)); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Get the compiled HTML for a static PageView. |
439
|
|
|
* |
440
|
|
|
* @param PageView $pageView |
441
|
|
|
* |
442
|
|
|
* @since 0.1.1 |
443
|
|
|
* |
444
|
|
|
* @throws \Exception |
445
|
|
|
* @throws \Throwable |
446
|
|
|
* @throws Twig_Error_Syntax |
447
|
|
|
* |
448
|
|
|
* @return string |
449
|
|
|
*/ |
450
|
10 |
|
private function renderStaticPageView(PageView &$pageView) |
451
|
|
|
{ |
452
|
|
|
return $this |
453
|
10 |
|
->createTwigTemplate($pageView) |
454
|
10 |
|
->render(array( |
455
|
10 |
|
'this' => $pageView->createJail(), |
456
|
|
|
)); |
457
|
|
|
} |
458
|
|
|
|
459
|
|
|
/** |
460
|
|
|
* Create a Twig template that just needs an array to render. |
461
|
|
|
* |
462
|
|
|
* @param PageView $pageView The PageView whose body will be used for Twig compilation |
463
|
|
|
* |
464
|
|
|
* @since 0.1.1 |
465
|
|
|
* |
466
|
|
|
* @throws \Exception |
467
|
|
|
* @throws \Throwable |
468
|
|
|
* @throws Twig_Error_Syntax |
469
|
|
|
* |
470
|
|
|
* @return Twig_Template |
471
|
|
|
*/ |
472
|
14 |
|
private function createTwigTemplate(PageView &$pageView) |
473
|
|
|
{ |
474
|
|
|
try |
475
|
|
|
{ |
476
|
14 |
|
$template = $this->twig->createTemplate($pageView->getContent()); |
477
|
|
|
|
478
|
14 |
|
$this->templateMapping[$template->getTemplateName()] = $pageView->getRelativeFilePath(); |
479
|
|
|
|
480
|
14 |
|
if (Service::getParameter(BuildableCommand::WATCHING)) |
481
|
|
|
{ |
482
|
|
|
// Keep track of import dependencies |
483
|
|
|
foreach ($pageView->getImportDependencies() as $dependency) |
484
|
|
|
{ |
485
|
|
|
$this->importDependencies[$dependency][$pageView->getName()] = &$pageView; |
|
|
|
|
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
// Keep track of Twig extends' |
489
|
|
|
$parent = $template->getParent(array()); |
490
|
|
|
|
491
|
|
|
while ($parent !== false) |
492
|
|
|
{ |
493
|
|
|
// Replace the '@theme' namespace in Twig with the path to the theme folder and create a UnixFilePath object from the given path |
494
|
|
|
$path = str_replace('@theme', $this->fs->appendPath(ThemeManager::THEME_FOLDER, $this->theme), $parent->getTemplateName()); |
495
|
|
|
$path = new FilePath($path); |
496
|
|
|
|
497
|
|
|
$this->templateDependencies[(string)$path][$pageView->getName()] = &$pageView; |
|
|
|
|
498
|
|
|
|
499
|
|
|
$parent = $parent->getParent(array()); |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|
503
|
14 |
|
return $template; |
504
|
|
|
} |
505
|
|
|
catch (Twig_Error_Syntax $e) |
506
|
|
|
{ |
507
|
|
|
$e->setTemplateLine($e->getTemplateLine() + $pageView->getLineOffset()); |
508
|
|
|
$e->setSourceContext(new Twig_Source( |
509
|
|
|
$pageView->getContent(), |
510
|
|
|
$pageView->getName(), |
|
|
|
|
511
|
|
|
$pageView->getRelativeFilePath() |
512
|
|
|
)); |
513
|
|
|
|
514
|
|
|
throw $e; |
515
|
|
|
} |
516
|
|
|
} |
517
|
|
|
} |
518
|
|
|
|