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