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