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