1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Brendt\Stitcher; |
4
|
|
|
|
5
|
|
|
use AsyncInterop\Promise; |
6
|
|
|
use Brendt\Stitcher\Adapter\Adapter; |
7
|
|
|
use Brendt\Stitcher\Exception\InvalidSiteException; |
8
|
|
|
use Brendt\Stitcher\Exception\TemplateNotFoundException; |
9
|
|
|
use Brendt\Stitcher\Factory\ParserFactory; |
10
|
|
|
use Brendt\Stitcher\Factory\TemplateEngineFactory; |
11
|
|
|
use Brendt\Stitcher\Site\Page; |
12
|
|
|
use Brendt\Stitcher\Site\Site; |
13
|
|
|
use Symfony\Component\Config\FileLocator; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
15
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\Finder\Finder; |
18
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
19
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
20
|
|
|
use Symfony\Component\Yaml\Yaml; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The Stitcher class is the core compiler of every Stitcher application. This class takes care of all routes, pages, |
24
|
|
|
* templates and data, and "stitches" everything together. |
25
|
|
|
* |
26
|
|
|
* The stitching process is done in several steps, with the final result being a fully rendered website in the |
27
|
|
|
* `directories.public` folder. |
28
|
|
|
*/ |
29
|
|
|
class Stitcher |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ContainerBuilder |
33
|
|
|
*/ |
34
|
|
|
protected static $container; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected static $configDefaults = [ |
40
|
|
|
'directories.src' => './src', |
41
|
|
|
'directories.public' => './public', |
42
|
|
|
'directories.cache' => './.cache', |
43
|
|
|
'meta' => [], |
44
|
|
|
'minify' => false, |
45
|
|
|
'engines.template' => 'smarty', |
46
|
|
|
'engines.image' => 'gd', |
47
|
|
|
'engines.optimizer' => true, |
48
|
|
|
'engines.async' => true, |
49
|
|
|
'caches.image' => true, |
50
|
|
|
]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* A collection of promises representing Stitcher's state. |
54
|
|
|
* |
55
|
|
|
* @var Promise[] |
56
|
|
|
*/ |
57
|
|
|
private $promises = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string |
61
|
|
|
*/ |
62
|
|
|
private $srcDir; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var string |
66
|
|
|
*/ |
67
|
|
|
private $publicDir; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var string |
71
|
|
|
*/ |
72
|
|
|
private $templateDir; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @see \Brendt\Stitcher\Stitcher::create() |
76
|
|
|
* |
77
|
|
|
* @param string $srcDir |
78
|
|
|
* @param string $publicDir |
79
|
|
|
* @param string $templateDir |
80
|
|
|
*/ |
81
|
|
|
private function __construct(?string $srcDir = './src', ?string $publicDir = './public', ?string $templateDir = './src/template') { |
82
|
|
|
$this->srcDir = $srcDir; |
83
|
|
|
$this->publicDir = $publicDir; |
84
|
|
|
$this->templateDir = $templateDir; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Static constructor |
89
|
|
|
* |
90
|
|
|
* @param string $configPath |
91
|
|
|
* @param array $defaultConfig |
92
|
|
|
* |
93
|
|
|
* @return Stitcher |
94
|
|
|
* |
95
|
|
|
*/ |
96
|
|
|
public static function create(string $configPath = './config.yml', array $defaultConfig = []) : Stitcher { |
97
|
|
|
self::$container = new ContainerBuilder(); |
98
|
|
|
|
99
|
|
|
$configPathParts = explode('/', $configPath); |
100
|
|
|
$configFileName = array_pop($configPathParts); |
101
|
|
|
$configPath = implode('/', $configPathParts) . '/'; |
102
|
|
|
$configFiles = Finder::create()->files()->in($configPath)->name($configFileName)->depth(0); |
103
|
|
|
$srcDir = null; |
104
|
|
|
$publicDir = null; |
105
|
|
|
$templateDir = null; |
106
|
|
|
|
107
|
|
|
/** @var SplFileInfo $configFile */ |
108
|
|
|
foreach ($configFiles as $configFile) { |
109
|
|
|
$fileConfig = Yaml::parse($configFile->getContents()); |
110
|
|
|
|
111
|
|
|
$config = array_merge( |
112
|
|
|
self::$configDefaults, |
113
|
|
|
Config::flatten($fileConfig), |
114
|
|
|
$defaultConfig |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$flatConfig = Config::flatten($config); |
118
|
|
|
$flatConfig['directories.template'] = $flatConfig['directories.template'] ?? $flatConfig['directories.src']; |
119
|
|
|
|
120
|
|
|
foreach ($flatConfig as $key => $value) { |
121
|
|
|
self::$container->setParameter($key, $value); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$srcDir = $flatConfig['directories.src'] ?? $srcDir; |
125
|
|
|
$publicDir = $flatConfig['directories.public'] ?? $publicDir; |
126
|
|
|
$templateDir = $flatConfig['directories.template'] ?? $templateDir; |
127
|
|
|
|
128
|
|
|
if (isset($fileConfig['meta'])) { |
129
|
|
|
self::$container->setParameter('meta', $fileConfig['meta']); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$stitcher = new self($srcDir, $publicDir, $templateDir); |
134
|
|
|
self::$container->set('stitcher', $stitcher); |
135
|
|
|
|
136
|
|
|
$serviceLoader = new YamlFileLoader(self::$container, new FileLocator(__DIR__)); |
137
|
|
|
$serviceLoader->load('services.yml'); |
138
|
|
|
|
139
|
|
|
return $stitcher; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param string $id |
144
|
|
|
* |
145
|
|
|
* @return mixed |
146
|
|
|
*/ |
147
|
|
|
public static function get(string $id) { |
148
|
|
|
return self::$container->get($id); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param string $key |
153
|
|
|
* |
154
|
|
|
* @return mixed |
155
|
|
|
*/ |
156
|
|
|
public static function getParameter(string $key) { |
157
|
|
|
return self::$container->getParameter($key); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* The core stitcher function. This function will compile the configured site and return an array of parsed |
162
|
|
|
* data. |
163
|
|
|
* |
164
|
|
|
* Compiling a site is done in the following steps. |
165
|
|
|
* |
166
|
|
|
* - Load the site configuration @see \Brendt\Stitcher\Stitcher::loadSite() |
167
|
|
|
* - Load all available templates @see \Brendt\Stitcher\Stitcher::loadTemplates() |
168
|
|
|
* - Loop over all pages and transform every page with the configured adapters (in any are set) @see |
169
|
|
|
* \Brendt\Stitcher\Stitcher::parseAdapters() |
170
|
|
|
* - Loop over all transformed pages and parse the variables which weren't parsed by the page's adapters. @see |
171
|
|
|
* \Brendt\Stitcher\Stitcher::parseVariables() |
172
|
|
|
* - Add all variables to the template engine and render the HTML for each page. |
173
|
|
|
* |
174
|
|
|
* This function takes two optional parameters which are used to render pages on the fly when using the |
175
|
|
|
* developer controller. The first one, `routes` will take a string or array of routes which should be rendered, |
176
|
|
|
* instead of all available routes. The second one, `filterValue` is used to provide a filter when the |
177
|
|
|
* CollectionAdapter is used, and only one entry page should be rendered. |
178
|
|
|
* |
179
|
|
|
* @param string|array $routes |
180
|
|
|
* @param string $filterValue |
181
|
|
|
* |
182
|
|
|
* @return array |
183
|
|
|
* @throws TemplateNotFoundException |
184
|
|
|
* |
185
|
|
|
* @see \Brendt\Stitcher\Stitcher::save() |
186
|
|
|
* @see \Brendt\Stitcher\Controller\DevController::run() |
187
|
|
|
* @see \Brendt\Stitcher\Adapter\CollectionAdapter::transform() |
188
|
|
|
*/ |
189
|
|
|
public function stitch($routes = [], string $filterValue = null) { |
190
|
|
|
/** @var TemplateEngineFactory $templateEngineFactory */ |
191
|
|
|
$templateEngineFactory = self::get('factory.template'); |
192
|
|
|
$templateEngine = $templateEngineFactory->getDefault(); |
193
|
|
|
$blanket = []; |
194
|
|
|
|
195
|
|
|
$site = $this->loadSite((array) $routes); |
196
|
|
|
$templates = $this->loadTemplates(); |
197
|
|
|
|
198
|
|
|
foreach ($site as $page) { |
199
|
|
|
$templateIsset = isset($templates[$page->getTemplatePath()]); |
200
|
|
|
|
201
|
|
|
if (!$templateIsset) { |
202
|
|
|
if ($template = $page->getTemplatePath()) { |
203
|
|
|
throw new TemplateNotFoundException("Template {$template} not found."); |
204
|
|
|
} else { |
205
|
|
|
throw new TemplateNotFoundException('No template was set.'); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$pages = $this->parseAdapters($page, $filterValue); |
210
|
|
|
|
211
|
|
|
$pageTemplate = $templates[$page->getTemplatePath()]; |
212
|
|
|
foreach ($pages as $entryPage) { |
213
|
|
|
$entryPage = $this->parseVariables($entryPage); |
214
|
|
|
|
215
|
|
|
// Render each page |
216
|
|
|
$templateEngine->addTemplateVariables($entryPage->getVariables()); |
217
|
|
|
$blanket[$entryPage->getId()] = $templateEngine->renderTemplate($pageTemplate); |
218
|
|
|
$templateEngine->clearTemplateVariables(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $blanket; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Load a site from YAML configuration files in the `directories.src`/site directory. |
227
|
|
|
* All YAML files are loaded and parsed into Page objects and added to a Site collection. |
228
|
|
|
* |
229
|
|
|
* @param array $routes |
230
|
|
|
* |
231
|
|
|
* @return Site |
232
|
|
|
* @throws InvalidSiteException |
233
|
|
|
* @see \Brendt\Stitcher\Site\Page |
234
|
|
|
* @see \Brendt\Stitcher\Site\Site |
235
|
|
|
*/ |
236
|
|
|
public function loadSite(array $routes = []) : Site { |
237
|
|
|
/** @var SplFileInfo[] $files */ |
238
|
|
|
$files = Finder::create()->files()->in("{$this->srcDir}/site")->name('*.yml'); |
239
|
|
|
$site = new Site(); |
240
|
|
|
|
241
|
|
|
foreach ($files as $file) { |
242
|
|
|
try { |
243
|
|
|
$fileContents = (array) Yaml::parse($file->getContents()); |
244
|
|
|
} catch (ParseException $e) { |
245
|
|
|
throw new InvalidSiteException("{$file->getRelativePathname()}: {$e->getMessage()}"); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
foreach ($fileContents as $route => $data) { |
249
|
|
|
if (count($routes) && !in_array($route, $routes)) { |
250
|
|
|
continue; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$page = new Page($route, $data); |
254
|
|
|
$site->addPage($page); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $site; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Load all templates from either the `directories.template` directory. Depending on the configured template |
263
|
|
|
* engine, set with `engines.template`; .html or .tpl files will be loaded. |
264
|
|
|
* |
265
|
|
|
* @return SplFileInfo[] |
266
|
|
|
*/ |
267
|
|
|
public function loadTemplates() { |
268
|
|
|
/** @var TemplateEngineFactory $templateEngineFactory */ |
269
|
|
|
$templateEngineFactory = self::get('factory.template'); |
270
|
|
|
$templateEngine = $templateEngineFactory->getDefault(); |
271
|
|
|
$templateExtension = $templateEngine->getTemplateExtension(); |
272
|
|
|
|
273
|
|
|
/** @var SplFileInfo[] $files */ |
274
|
|
|
$files = Finder::create()->files()->in($this->templateDir)->name("*.{$templateExtension}"); |
275
|
|
|
$templates = []; |
276
|
|
|
|
277
|
|
|
foreach ($files as $file) { |
278
|
|
|
$id = str_replace(".{$templateExtension}", '', $file->getRelativePathname()); |
279
|
|
|
$templates[$id] = $file; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return $templates; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* This function takes a page and optional entry id. The page's adapters will be loaded and looped. |
287
|
|
|
* An adapter will transform a page's original configuration and variables to one or more pages. |
288
|
|
|
* An entry id can be provided as a filter. This filter can be used in an adapter to skip rendering unnecessary |
289
|
|
|
* pages. The filter parameter is used to render pages on the fly when using the developer controller. |
290
|
|
|
* |
291
|
|
|
* @param Page $page |
292
|
|
|
* @param string $entryId |
293
|
|
|
* |
294
|
|
|
* @return Page[] |
295
|
|
|
* |
296
|
|
|
* @see \Brendt\Stitcher\Adapter\Adapter::transform() |
297
|
|
|
* @see \Brendt\Stitcher\Controller\DevController::run() |
298
|
|
|
*/ |
299
|
|
|
public function parseAdapters(Page $page, $entryId = null) { |
300
|
|
|
if (!$page->getAdapters()) { |
301
|
|
|
return [$page->getId() => $page]; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
$pages = [$page]; |
305
|
|
|
|
306
|
|
|
foreach ($page->getAdapters() as $type => $adapterConfig) { |
307
|
|
|
/** @var Adapter $adapter */ |
308
|
|
|
$adapter = self::get("adapter.{$type}"); |
309
|
|
|
|
310
|
|
|
if ($entryId !== null) { |
311
|
|
|
$pages = $adapter->transform($pages, $entryId); |
312
|
|
|
} else { |
313
|
|
|
$pages = $adapter->transform($pages); |
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return $pages; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* This function takes a Page object and parse its variables using a Parser. It will only parse variables which |
322
|
|
|
* weren't parsed already by an adapter. |
323
|
|
|
* |
324
|
|
|
* @param Page $page |
325
|
|
|
* |
326
|
|
|
* @return Page |
327
|
|
|
* |
328
|
|
|
* @see \Brendt\Stitcher\Factory\ParserFactory |
329
|
|
|
* @see \Brendt\Stitcher\Parser\Parser |
330
|
|
|
* @see \Brendt\Stitcher\Site\Page::isParsedVariable() |
331
|
|
|
*/ |
332
|
|
|
public function parseVariables(Page $page) { |
333
|
|
|
foreach ($page->getVariables() as $name => $value) { |
334
|
|
|
if ($page->isParsedVariable($name)) { |
335
|
|
|
continue; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
$page |
339
|
|
|
->setVariableValue($name, $this->getData($value)) |
340
|
|
|
->setVariableIsParsed($name); |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
return $page; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* This function will save a stitched output to HTML files in the `directories.public` directory. |
348
|
|
|
* |
349
|
|
|
* @param array $blanket |
350
|
|
|
* |
351
|
|
|
* @see \Brendt\Stitcher\Stitcher::stitch() |
352
|
|
|
*/ |
353
|
|
|
public function save(array $blanket) { |
354
|
|
|
$fs = new Filesystem(); |
355
|
|
|
|
356
|
|
|
foreach ($blanket as $path => $page) { |
357
|
|
|
if ($path === '/') { |
358
|
|
|
$path = 'index'; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
$fs->dumpFile($this->publicDir . "/{$path}.html", $page); |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* This function will get the parser based on the value. This value is parsed by the parser, or returned if no |
367
|
|
|
* suitable parser was found. |
368
|
|
|
* |
369
|
|
|
* @param $value |
370
|
|
|
* |
371
|
|
|
* @return mixed |
372
|
|
|
* |
373
|
|
|
* @see \Brendt\Stitcher\Factory\ParserFactory |
374
|
|
|
*/ |
375
|
|
|
private function getData($value) { |
376
|
|
|
/** @var ParserFactory $parserFactory */ |
377
|
|
|
$parserFactory = self::get('factory.parser'); |
378
|
|
|
$parser = $parserFactory->getByFileName($value); |
379
|
|
|
|
380
|
|
|
if (!$parser) { |
381
|
|
|
return $value; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
return $parser->parse($value); |
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* @param Promise|null $promise |
389
|
|
|
* |
390
|
|
|
* @return Stitcher |
391
|
|
|
*/ |
392
|
|
|
public function addPromise(?Promise $promise) : Stitcher { |
393
|
|
|
if ($promise) { |
394
|
|
|
$this->promises[] = $promise; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
return $this; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
/** |
401
|
|
|
* @return Promise |
402
|
|
|
*/ |
403
|
|
|
public function getPromise() : Promise { |
404
|
|
|
return \Amp\all($this->promises); |
|
|
|
|
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* @param callable $callback |
409
|
|
|
*/ |
410
|
|
|
public function done(callable $callback) { |
411
|
|
|
$donePromise = $this->getPromise(); |
412
|
|
|
|
413
|
|
|
$donePromise->when($callback); |
414
|
|
|
} |
415
|
|
|
|
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
|
419
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: