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