1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Cecil. |
7
|
|
|
* |
8
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Cecil; |
15
|
|
|
|
16
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
17
|
|
|
use Cecil\Exception\RuntimeException; |
18
|
|
|
use Cecil\Generator\GeneratorManager; |
19
|
|
|
use Cecil\Logger\PrintLogger; |
20
|
|
|
use Psr\Log\LoggerAwareInterface; |
21
|
|
|
use Psr\Log\LoggerInterface; |
22
|
|
|
use Symfony\Component\Finder\Finder; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Builder. |
26
|
|
|
*/ |
27
|
|
|
class Builder implements LoggerAwareInterface |
28
|
|
|
{ |
29
|
|
|
public const VERSION = '8.x-dev'; |
30
|
|
|
public const VERBOSITY_QUIET = -1; |
31
|
|
|
public const VERBOSITY_NORMAL = 0; |
32
|
|
|
public const VERBOSITY_VERBOSE = 1; |
33
|
|
|
public const VERBOSITY_DEBUG = 2; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var array Steps processed by build(). |
37
|
|
|
*/ |
38
|
|
|
protected $steps = [ |
39
|
|
|
'Cecil\Step\Pages\Load', |
40
|
|
|
'Cecil\Step\Data\Load', |
41
|
|
|
'Cecil\Step\StaticFiles\Load', |
42
|
|
|
'Cecil\Step\Pages\Create', |
43
|
|
|
'Cecil\Step\Pages\Convert', |
44
|
|
|
'Cecil\Step\Taxonomies\Create', |
45
|
|
|
'Cecil\Step\Pages\Generate', |
46
|
|
|
'Cecil\Step\Menus\Create', |
47
|
|
|
'Cecil\Step\StaticFiles\Copy', |
48
|
|
|
'Cecil\Step\Pages\Render', |
49
|
|
|
'Cecil\Step\Pages\Save', |
50
|
|
|
'Cecil\Step\Assets\Save', |
51
|
|
|
'Cecil\Step\Optimize\Html', |
52
|
|
|
'Cecil\Step\Optimize\Css', |
53
|
|
|
'Cecil\Step\Optimize\Js', |
54
|
|
|
'Cecil\Step\Optimize\Images', |
55
|
|
|
]; |
56
|
|
|
|
57
|
|
|
/** @var Config Configuration. */ |
58
|
|
|
protected $config; |
59
|
|
|
|
60
|
|
|
/** @var LoggerInterface Logger. */ |
61
|
|
|
protected $logger; |
62
|
|
|
|
63
|
|
|
/** @var bool Debug mode. */ |
64
|
|
|
protected $debug = false; |
65
|
|
|
|
66
|
|
|
/** @var array Build options. */ |
67
|
|
|
protected $options; |
68
|
|
|
|
69
|
|
|
/** @var Finder Content iterator. */ |
70
|
|
|
protected $content; |
71
|
|
|
|
72
|
|
|
/** @var array Data collection. */ |
73
|
|
|
protected $data = []; |
74
|
|
|
|
75
|
|
|
/** @var array Static files collection. */ |
76
|
|
|
protected $static = []; |
77
|
|
|
|
78
|
|
|
/** @var PagesCollection Pages collection. */ |
79
|
|
|
protected $pages; |
80
|
|
|
|
81
|
|
|
/** @var array Assets path collection */ |
82
|
|
|
protected $assets = []; |
83
|
|
|
|
84
|
|
|
/** @var array Menus collection. */ |
85
|
|
|
protected $menus; |
86
|
|
|
|
87
|
|
|
/** @var array Taxonomies collection. */ |
88
|
|
|
protected $taxonomies; |
89
|
|
|
|
90
|
|
|
/** @var Renderer\RendererInterface Renderer. */ |
91
|
|
|
protected $renderer; |
92
|
|
|
|
93
|
|
|
/** @var GeneratorManager Generators manager. */ |
94
|
|
|
protected $generatorManager; |
95
|
|
|
|
96
|
|
|
/** @var string Application version. */ |
97
|
|
|
protected static $version; |
98
|
|
|
|
99
|
|
|
/** @var array Build metrics. */ |
100
|
|
|
protected $metrics = []; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Config|array|null $config |
104
|
|
|
* @param LoggerInterface|null $logger |
105
|
|
|
*/ |
106
|
1 |
|
public function __construct($config = null, ?LoggerInterface $logger = null) |
107
|
|
|
{ |
108
|
|
|
// init and set config |
109
|
1 |
|
$this->config = new Config(); |
110
|
1 |
|
if ($config !== null) { |
111
|
1 |
|
$this->setConfig($config); |
112
|
|
|
} |
113
|
|
|
// debug mode? |
114
|
1 |
|
if (getenv('CECIL_DEBUG') == 'true' || $this->getConfig()->isEnabled('debug')) { |
115
|
1 |
|
$this->debug = true; |
116
|
|
|
} |
117
|
|
|
// set logger |
118
|
1 |
|
if ($logger === null) { |
119
|
|
|
$logger = new PrintLogger(self::VERBOSITY_VERBOSE); |
120
|
|
|
} |
121
|
1 |
|
$this->setLogger($logger); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Creates a new Builder instance. |
126
|
|
|
*/ |
127
|
1 |
|
public static function create(): self |
128
|
|
|
{ |
129
|
1 |
|
$class = new \ReflectionClass(\get_called_class()); |
130
|
|
|
|
131
|
1 |
|
return $class->newInstanceArgs(\func_get_args()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Builds a new website. |
136
|
|
|
*/ |
137
|
1 |
|
public function build(array $options): self |
138
|
|
|
{ |
139
|
|
|
// set start script time and memory usage |
140
|
1 |
|
$startTime = microtime(true); |
141
|
1 |
|
$startMemory = memory_get_usage(); |
142
|
|
|
|
143
|
|
|
// log warnings |
144
|
1 |
|
$this->logBuildWarnings(); |
145
|
|
|
|
146
|
|
|
// prepare options |
147
|
1 |
|
$this->options = array_merge([ |
148
|
1 |
|
'drafts' => false, // build drafts or not |
149
|
1 |
|
'dry-run' => false, // if dry-run is true, generated files are not saved |
150
|
1 |
|
'page' => '', // specific page to build |
151
|
1 |
|
], $options); |
152
|
|
|
|
153
|
|
|
// process each step |
154
|
1 |
|
$steps = []; |
155
|
|
|
// init... |
156
|
1 |
|
foreach ($this->steps as $step) { |
157
|
|
|
/** @var Step\StepInterface $stepObject */ |
158
|
1 |
|
$stepObject = new $step($this); |
159
|
1 |
|
$stepObject->init($this->options); |
160
|
1 |
|
if ($stepObject->canProcess()) { |
161
|
1 |
|
$steps[] = $stepObject; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
// ...and process! |
165
|
1 |
|
$stepNumber = 0; |
166
|
1 |
|
$stepsTotal = \count($steps); |
167
|
1 |
|
foreach ($steps as $step) { |
168
|
1 |
|
$stepNumber++; |
169
|
|
|
/** @var Step\StepInterface $step */ |
170
|
1 |
|
$this->getLogger()->notice($step->getName(), ['step' => [$stepNumber, $stepsTotal]]); |
171
|
1 |
|
$stepStartTime = microtime(true); |
172
|
1 |
|
$stepStartMemory = memory_get_usage(); |
173
|
1 |
|
$step->process(); |
174
|
|
|
// step duration and memory usage |
175
|
1 |
|
$this->metrics['steps'][$stepNumber]['name'] = $step->getName(); |
176
|
1 |
|
$this->metrics['steps'][$stepNumber]['duration'] = Util::convertMicrotime((float) $stepStartTime); |
177
|
1 |
|
$this->metrics['steps'][$stepNumber]['memory'] = Util::convertMemory(memory_get_usage() - $stepStartMemory); |
178
|
1 |
|
$this->getLogger()->info(\sprintf( |
179
|
1 |
|
'%s done in %s (%s)', |
180
|
1 |
|
$this->metrics['steps'][$stepNumber]['name'], |
181
|
1 |
|
$this->metrics['steps'][$stepNumber]['duration'], |
182
|
1 |
|
$this->metrics['steps'][$stepNumber]['memory'] |
183
|
1 |
|
)); |
184
|
|
|
} |
185
|
|
|
// build duration and memory usage |
186
|
1 |
|
$this->metrics['total']['duration'] = Util::convertMicrotime($startTime); |
|
|
|
|
187
|
1 |
|
$this->metrics['total']['memory'] = Util::convertMemory(memory_get_usage() - $startMemory); |
188
|
1 |
|
$this->getLogger()->notice(\sprintf('Built in %s (%s)', $this->metrics['total']['duration'], $this->metrics['total']['memory'])); |
189
|
|
|
|
190
|
1 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* Set configuration. |
195
|
|
|
*/ |
196
|
1 |
|
public function setConfig(array|Config $config): self |
197
|
|
|
{ |
198
|
1 |
|
if (\is_array($config)) { |
199
|
1 |
|
$config = new Config($config); |
200
|
|
|
} |
201
|
1 |
|
if ($this->config !== $config) { |
202
|
1 |
|
$this->config = $config; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// import themes configuration |
206
|
1 |
|
$this->importThemesConfig(); |
207
|
|
|
// autoloads local extensions |
208
|
1 |
|
Util::autoload($this, 'extensions'); |
209
|
|
|
|
210
|
1 |
|
return $this; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns configuration. |
215
|
|
|
*/ |
216
|
1 |
|
public function getConfig(): Config |
217
|
|
|
{ |
218
|
1 |
|
if ($this->config === null) { |
219
|
|
|
$this->config = new Config(); |
220
|
|
|
} |
221
|
|
|
|
222
|
1 |
|
return $this->config; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Config::setSourceDir() alias. |
227
|
|
|
*/ |
228
|
1 |
|
public function setSourceDir(string $sourceDir): self |
229
|
|
|
{ |
230
|
1 |
|
$this->getConfig()->setSourceDir($sourceDir); |
231
|
|
|
// import themes configuration |
232
|
1 |
|
$this->importThemesConfig(); |
233
|
|
|
|
234
|
1 |
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Config::setDestinationDir() alias. |
239
|
|
|
*/ |
240
|
1 |
|
public function setDestinationDir(string $destinationDir): self |
241
|
|
|
{ |
242
|
1 |
|
$this->getConfig()->setDestinationDir($destinationDir); |
243
|
|
|
|
244
|
1 |
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Import themes configuration. |
249
|
|
|
*/ |
250
|
1 |
|
public function importThemesConfig(): void |
251
|
|
|
{ |
252
|
1 |
|
foreach ($this->config->get('theme') as $theme) { |
253
|
1 |
|
$this->config->import(Config::loadFile(Util::joinFile($this->config->getThemesPath(), $theme, 'config.yml'), true), Config::PRESERVE); |
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritdoc} |
259
|
|
|
*/ |
260
|
1 |
|
public function setLogger(LoggerInterface $logger): void |
261
|
|
|
{ |
262
|
1 |
|
$this->logger = $logger; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Returns the logger instance. |
267
|
|
|
*/ |
268
|
1 |
|
public function getLogger(): LoggerInterface |
269
|
|
|
{ |
270
|
1 |
|
return $this->logger; |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Returns debug mode state. |
275
|
|
|
*/ |
276
|
1 |
|
public function isDebug(): bool |
277
|
|
|
{ |
278
|
1 |
|
return (bool) $this->debug; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Returns build options. |
283
|
|
|
*/ |
284
|
1 |
|
public function getBuildOptions(): array |
285
|
|
|
{ |
286
|
1 |
|
return $this->options; |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Set collected pages files. |
291
|
|
|
*/ |
292
|
1 |
|
public function setPagesFiles(Finder $content): void |
293
|
|
|
{ |
294
|
1 |
|
$this->content = $content; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Returns pages files. |
299
|
|
|
*/ |
300
|
1 |
|
public function getPagesFiles(): ?Finder |
301
|
|
|
{ |
302
|
1 |
|
return $this->content; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Set collected data. |
307
|
|
|
*/ |
308
|
1 |
|
public function setData(array $data): void |
309
|
|
|
{ |
310
|
1 |
|
$this->data = $data; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Returns data collection. |
315
|
|
|
*/ |
316
|
1 |
|
public function getData(?string $language = null): ?array |
317
|
|
|
{ |
318
|
1 |
|
if ($language) { |
319
|
1 |
|
if (empty($this->data[$language])) { |
320
|
|
|
// fallback to default language |
321
|
1 |
|
return $this->data[$this->config->getLanguageDefault()]; |
322
|
|
|
} |
323
|
|
|
|
324
|
1 |
|
return $this->data[$language]; |
325
|
|
|
} |
326
|
|
|
|
327
|
1 |
|
return $this->data; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* Set collected static files. |
332
|
|
|
*/ |
333
|
1 |
|
public function setStatic(array $static): void |
334
|
|
|
{ |
335
|
1 |
|
$this->static = $static; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Returns static files collection. |
340
|
|
|
*/ |
341
|
1 |
|
public function getStatic(): array |
342
|
|
|
{ |
343
|
1 |
|
return $this->static; |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* Set/update Pages collection. |
348
|
|
|
*/ |
349
|
1 |
|
public function setPages(PagesCollection $pages): void |
350
|
|
|
{ |
351
|
1 |
|
$this->pages = $pages; |
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Returns pages collection. |
356
|
|
|
*/ |
357
|
1 |
|
public function getPages(): ?PagesCollection |
358
|
|
|
{ |
359
|
1 |
|
return $this->pages; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* Set assets path list. |
364
|
|
|
*/ |
365
|
|
|
public function setAssets(array $assets): void |
366
|
|
|
{ |
367
|
|
|
$this->assets = $assets; |
368
|
|
|
} |
369
|
|
|
|
370
|
|
|
/** |
371
|
|
|
* Add an asset path to assets list. |
372
|
|
|
*/ |
373
|
1 |
|
public function addAsset(string $path): void |
374
|
|
|
{ |
375
|
1 |
|
if (!\in_array($path, $this->assets, true)) { |
376
|
1 |
|
$this->assets[] = $path; |
377
|
|
|
} |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/** |
381
|
|
|
* Returns list of assets path. |
382
|
|
|
*/ |
383
|
1 |
|
public function getAssets(): ?array |
384
|
|
|
{ |
385
|
1 |
|
return $this->assets; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* Set menus collection. |
390
|
|
|
*/ |
391
|
1 |
|
public function setMenus(array $menus): void |
392
|
|
|
{ |
393
|
1 |
|
$this->menus = $menus; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Returns all menus, for a language. |
398
|
|
|
*/ |
399
|
1 |
|
public function getMenus(string $language): Collection\Menu\Collection |
400
|
|
|
{ |
401
|
1 |
|
return $this->menus[$language]; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Set taxonomies collection. |
406
|
|
|
*/ |
407
|
1 |
|
public function setTaxonomies(array $taxonomies): void |
408
|
|
|
{ |
409
|
1 |
|
$this->taxonomies = $taxonomies; |
410
|
|
|
} |
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Returns taxonomies collection, for a language. |
414
|
|
|
*/ |
415
|
1 |
|
public function getTaxonomies(string $language): ?Collection\Taxonomy\Collection |
416
|
|
|
{ |
417
|
1 |
|
return $this->taxonomies[$language]; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* Set renderer object. |
422
|
|
|
*/ |
423
|
1 |
|
public function setRenderer(Renderer\RendererInterface $renderer): void |
424
|
|
|
{ |
425
|
1 |
|
$this->renderer = $renderer; |
426
|
|
|
} |
427
|
|
|
|
428
|
|
|
/** |
429
|
|
|
* Returns Renderer object. |
430
|
|
|
*/ |
431
|
1 |
|
public function getRenderer(): Renderer\RendererInterface |
432
|
|
|
{ |
433
|
1 |
|
return $this->renderer; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* Returns metrics array. |
438
|
|
|
*/ |
439
|
|
|
public function getMetrics(): array |
440
|
|
|
{ |
441
|
|
|
return $this->metrics; |
442
|
|
|
} |
443
|
|
|
|
444
|
|
|
/** |
445
|
|
|
* Returns application version. |
446
|
|
|
* |
447
|
|
|
* @throws RuntimeException |
448
|
|
|
*/ |
449
|
1 |
|
public static function getVersion(): string |
450
|
|
|
{ |
451
|
1 |
|
if (!isset(self::$version)) { |
452
|
|
|
try { |
453
|
1 |
|
$filePath = Util\File::getRealPath('VERSION'); |
454
|
|
|
$version = Util\File::fileGetContents($filePath); |
455
|
|
|
if ($version === false) { |
456
|
|
|
throw new RuntimeException(\sprintf('Can\'t read content of "%s".', $filePath)); |
457
|
|
|
} |
458
|
|
|
self::$version = trim($version); |
459
|
1 |
|
} catch (\Exception) { |
460
|
1 |
|
self::$version = self::VERSION; |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
|
464
|
1 |
|
return self::$version; |
465
|
|
|
} |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* Log build warnings. |
469
|
|
|
*/ |
470
|
1 |
|
protected function logBuildWarnings(): void |
471
|
|
|
{ |
472
|
|
|
// baseurl |
473
|
1 |
|
if (empty(trim((string) $this->config->get('baseurl'), '/'))) { |
474
|
|
|
$this->getLogger()->warning('`baseurl` configuration key is required in production.'); |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
} |
478
|
|
|
|