|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Arnaud Ligny <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Cecil; |
|
10
|
|
|
|
|
11
|
|
|
use Cecil\Collection\Page\Collection as PagesCollection; |
|
12
|
|
|
use Cecil\Generator\GeneratorManager; |
|
13
|
|
|
use Cecil\Util\Plateform; |
|
14
|
|
|
use Symfony\Component\Finder\Finder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Builder. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Builder |
|
20
|
|
|
{ |
|
21
|
|
|
const VERSION = '5.x-dev'; |
|
22
|
|
|
const VERBOSITY_QUIET = -1; |
|
23
|
|
|
const VERBOSITY_NORMAL = 0; |
|
24
|
|
|
const VERBOSITY_VERBOSE = 1; |
|
25
|
|
|
const VERBOSITY_DEBUG = 2; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* App version. |
|
29
|
|
|
* |
|
30
|
|
|
* @var string |
|
31
|
|
|
*/ |
|
32
|
|
|
protected static $version; |
|
33
|
|
|
/** |
|
34
|
|
|
* Steps that are processed by build(). |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
* |
|
38
|
|
|
* @see build() |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $steps = [ |
|
41
|
|
|
'Cecil\Step\ConfigImport', |
|
42
|
|
|
'Cecil\Step\ContentLoad', |
|
43
|
|
|
'Cecil\Step\DataLoad', |
|
44
|
|
|
'Cecil\Step\PagesCreate', |
|
45
|
|
|
'Cecil\Step\PagesConvert', |
|
46
|
|
|
'Cecil\Step\TaxonomiesCreate', |
|
47
|
|
|
'Cecil\Step\PagesGenerate', |
|
48
|
|
|
'Cecil\Step\MenusCreate', |
|
49
|
|
|
'Cecil\Step\StaticCopy', |
|
50
|
|
|
'Cecil\Step\PagesRender', |
|
51
|
|
|
'Cecil\Step\PagesSave', |
|
52
|
|
|
'Cecil\Step\OptimizeCss', |
|
53
|
|
|
'Cecil\Step\OptimizeJs', |
|
54
|
|
|
'Cecil\Step\OptimizeHtml', |
|
55
|
|
|
'Cecil\Step\OptimizeImages', |
|
56
|
|
|
]; |
|
57
|
|
|
/** |
|
58
|
|
|
* Config. |
|
59
|
|
|
* |
|
60
|
|
|
* @var Config |
|
61
|
|
|
*/ |
|
62
|
|
|
protected $config; |
|
63
|
|
|
/** |
|
64
|
|
|
* Content iterator. |
|
65
|
|
|
* |
|
66
|
|
|
* @var Finder |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $content; |
|
69
|
|
|
/** |
|
70
|
|
|
* Data array. |
|
71
|
|
|
* |
|
72
|
|
|
* @var array |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $data = []; |
|
75
|
|
|
/** |
|
76
|
|
|
* Pages collection. |
|
77
|
|
|
* |
|
78
|
|
|
* @var PagesCollection |
|
79
|
|
|
*/ |
|
80
|
|
|
protected $pages; |
|
81
|
|
|
/** |
|
82
|
|
|
* Collection of site menus. |
|
83
|
|
|
* |
|
84
|
|
|
* @var Collection\Menu\Collection |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $menus; |
|
87
|
|
|
/** |
|
88
|
|
|
* Collection of site taxonomies. |
|
89
|
|
|
* |
|
90
|
|
|
* @var Collection\Taxonomy\Collection |
|
91
|
|
|
*/ |
|
92
|
|
|
protected $taxonomies; |
|
93
|
|
|
/** |
|
94
|
|
|
* Twig renderer. |
|
95
|
|
|
* |
|
96
|
|
|
* @var Renderer\Twig |
|
97
|
|
|
*/ |
|
98
|
|
|
protected $renderer; |
|
99
|
|
|
/** |
|
100
|
|
|
* @var \Closure |
|
101
|
|
|
*/ |
|
102
|
|
|
protected $messageCallback; |
|
103
|
|
|
/** |
|
104
|
|
|
* @var GeneratorManager |
|
105
|
|
|
*/ |
|
106
|
|
|
protected $generatorManager; |
|
107
|
|
|
/** |
|
108
|
|
|
* @var array |
|
109
|
|
|
*/ |
|
110
|
|
|
protected $log; |
|
111
|
|
|
/** |
|
112
|
|
|
* @var array |
|
113
|
|
|
*/ |
|
114
|
|
|
protected $options; |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Builder constructor. |
|
118
|
|
|
* |
|
119
|
|
|
* @param Config|array|null $config |
|
120
|
|
|
* @param \Closure|null $messageCallback |
|
121
|
|
|
*/ |
|
122
|
|
|
public function __construct($config = null, \Closure $messageCallback = null) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->setConfig($config) |
|
125
|
|
|
->setSourceDir(null) |
|
126
|
|
|
->setDestinationDir(null); |
|
127
|
|
|
$this->setMessageCallback($messageCallback); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Creates a new Builder instance. |
|
132
|
|
|
* |
|
133
|
|
|
* @return Builder |
|
134
|
|
|
*/ |
|
135
|
|
|
public static function create() |
|
136
|
|
|
{ |
|
137
|
|
|
$class = new \ReflectionClass(get_called_class()); |
|
138
|
|
|
|
|
139
|
|
|
return $class->newInstanceArgs(func_get_args()); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Set config. |
|
144
|
|
|
* |
|
145
|
|
|
* @param Config|array|null $config |
|
146
|
|
|
* |
|
147
|
|
|
* @return $this |
|
148
|
|
|
*/ |
|
149
|
|
|
public function setConfig($config) |
|
150
|
|
|
{ |
|
151
|
|
|
if (!$config instanceof Config) { |
|
152
|
|
|
$config = new Config($config); |
|
153
|
|
|
} |
|
154
|
|
|
if ($this->config !== $config) { |
|
155
|
|
|
$this->config = $config; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @return Config |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getConfig() |
|
165
|
|
|
{ |
|
166
|
|
|
return $this->config; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Config::setSourceDir alias. |
|
171
|
|
|
* |
|
172
|
|
|
* @param $sourceDir |
|
173
|
|
|
* |
|
174
|
|
|
* @return $this |
|
175
|
|
|
*/ |
|
176
|
|
|
public function setSourceDir($sourceDir) |
|
177
|
|
|
{ |
|
178
|
|
|
$this->config->setSourceDir($sourceDir); |
|
179
|
|
|
|
|
180
|
|
|
return $this; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* Config::setDestinationDir alias. |
|
185
|
|
|
* |
|
186
|
|
|
* @param $destinationDir |
|
187
|
|
|
* |
|
188
|
|
|
* @return $this |
|
189
|
|
|
*/ |
|
190
|
|
|
public function setDestinationDir($destinationDir) |
|
191
|
|
|
{ |
|
192
|
|
|
$this->config->setDestinationDir($destinationDir); |
|
193
|
|
|
|
|
194
|
|
|
return $this; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param Finder $content |
|
199
|
|
|
*/ |
|
200
|
|
|
public function setContent(Finder $content) |
|
201
|
|
|
{ |
|
202
|
|
|
$this->content = $content; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return Finder |
|
207
|
|
|
*/ |
|
208
|
|
|
public function getContent(): Finder |
|
209
|
|
|
{ |
|
210
|
|
|
return $this->content; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param array $data |
|
215
|
|
|
*/ |
|
216
|
|
|
public function setData(array $data) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->data = $data; |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return array |
|
223
|
|
|
*/ |
|
224
|
|
|
public function getData(): array |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->data; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param $pages |
|
231
|
|
|
*/ |
|
232
|
|
|
public function setPages($pages) |
|
233
|
|
|
{ |
|
234
|
|
|
$this->pages = $pages; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @return PagesCollection |
|
239
|
|
|
*/ |
|
240
|
|
|
public function getPages() |
|
241
|
|
|
{ |
|
242
|
|
|
return $this->pages; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
/** |
|
246
|
|
|
* @param $menus |
|
247
|
|
|
*/ |
|
248
|
|
|
public function setMenus($menus) |
|
249
|
|
|
{ |
|
250
|
|
|
$this->menus = $menus; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @return Collection\Menu\Collection |
|
255
|
|
|
*/ |
|
256
|
|
|
public function getMenus() |
|
257
|
|
|
{ |
|
258
|
|
|
return $this->menus; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param $taxonomies |
|
263
|
|
|
*/ |
|
264
|
|
|
public function setTaxonomies($taxonomies) |
|
265
|
|
|
{ |
|
266
|
|
|
$this->taxonomies = $taxonomies; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @return Collection\Taxonomy\Collection |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getTaxonomies() |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->taxonomies; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param \Closure|null $messageCallback |
|
279
|
|
|
*/ |
|
280
|
|
|
public function setMessageCallback($messageCallback = null) |
|
281
|
|
|
{ |
|
282
|
|
|
if ($messageCallback === null) { |
|
283
|
|
|
$messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) { |
|
284
|
|
|
switch ($code) { |
|
285
|
|
|
case 'CONFIG': |
|
286
|
|
|
case 'LOCATE': |
|
287
|
|
|
case 'DATA': |
|
288
|
|
|
case 'CREATE': |
|
289
|
|
|
case 'CONVERT': |
|
290
|
|
|
case 'GENERATE': |
|
291
|
|
|
case 'MENU': |
|
292
|
|
|
case 'COPY': |
|
293
|
|
|
case 'OPTIMIZE': |
|
294
|
|
|
case 'RENDER': |
|
295
|
|
|
case 'SAVE': |
|
296
|
|
|
case 'TIME': |
|
297
|
|
|
$log = sprintf("%s\n", $message); |
|
298
|
|
|
$this->addLog($log); |
|
299
|
|
|
break; |
|
300
|
|
|
case 'CONFIG_PROGRESS': |
|
301
|
|
|
case 'LOCATE_PROGRESS': |
|
302
|
|
|
case 'DATA_PROGRESS': |
|
303
|
|
|
case 'CREATE_PROGRESS': |
|
304
|
|
|
case 'CONVERT_PROGRESS': |
|
305
|
|
|
case 'GENERATE_PROGRESS': |
|
306
|
|
|
case 'MENU_PROGRESS': |
|
307
|
|
|
case 'COPY_PROGRESS': |
|
308
|
|
|
case 'OPTIMIZE_PROGRESS': |
|
309
|
|
|
case 'RENDER_PROGRESS': |
|
310
|
|
|
case 'SAVE_PROGRESS': |
|
311
|
|
|
if ($itemsCount > 0) { |
|
312
|
|
|
$log = sprintf("(%u/%u) %s\n", $itemsCount, $itemsMax, $message); |
|
313
|
|
|
$this->addLog($log, 1); |
|
314
|
|
|
} else { |
|
315
|
|
|
$log = sprintf("%s\n", $message); |
|
316
|
|
|
$this->addLog($log, 1); |
|
317
|
|
|
} |
|
318
|
|
|
break; |
|
319
|
|
|
case 'CONFIG_ERROR': |
|
320
|
|
|
case 'LOCATE_ERROR': |
|
321
|
|
|
case 'DATA_ERROR': |
|
322
|
|
|
case 'CREATE_ERROR': |
|
323
|
|
|
case 'CONVERT_ERROR': |
|
324
|
|
|
case 'GENERATE_ERROR': |
|
325
|
|
|
case 'MENU_ERROR': |
|
326
|
|
|
case 'COPY_ERROR': |
|
327
|
|
|
case 'OPTIMIZE_ERROR': |
|
328
|
|
|
case 'RENDER_ERROR': |
|
329
|
|
|
case 'SAVE_ERROR': |
|
330
|
|
|
$log = sprintf(">> %s\n", $message); |
|
331
|
|
|
$this->addLog($log); |
|
332
|
|
|
break; |
|
333
|
|
|
} |
|
334
|
|
|
}; |
|
335
|
|
|
} |
|
336
|
|
|
$this->messageCallback = $messageCallback; |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @return \Closure |
|
341
|
|
|
*/ |
|
342
|
|
|
public function getMessageCb() |
|
343
|
|
|
{ |
|
344
|
|
|
return $this->messageCallback; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param $renderer |
|
349
|
|
|
*/ |
|
350
|
|
|
public function setRenderer($renderer) |
|
351
|
|
|
{ |
|
352
|
|
|
$this->renderer = $renderer; |
|
353
|
|
|
} |
|
354
|
|
|
|
|
355
|
|
|
/** |
|
356
|
|
|
* @return Renderer\Twig |
|
357
|
|
|
*/ |
|
358
|
|
|
public function getRenderer() |
|
359
|
|
|
{ |
|
360
|
|
|
return $this->renderer; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* @param string $log |
|
365
|
|
|
* @param int $type |
|
366
|
|
|
* |
|
367
|
|
|
* @return array|null |
|
368
|
|
|
*/ |
|
369
|
|
|
public function addLog($log, $type = 0) |
|
370
|
|
|
{ |
|
371
|
|
|
$this->log[] = [ |
|
372
|
|
|
'type' => $type, |
|
373
|
|
|
'log' => $log, |
|
374
|
|
|
]; |
|
375
|
|
|
|
|
376
|
|
|
return $this->getLog($type); |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
/** |
|
380
|
|
|
* @param int $type |
|
381
|
|
|
* |
|
382
|
|
|
* @return array|null |
|
383
|
|
|
*/ |
|
384
|
|
|
public function getLog($type = 0) |
|
385
|
|
|
{ |
|
386
|
|
|
if (is_array($this->log)) { |
|
|
|
|
|
|
387
|
|
|
return array_filter($this->log, function ($key) use ($type) { |
|
388
|
|
|
return $key['type'] <= $type; |
|
389
|
|
|
}); |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
|
|
/** |
|
394
|
|
|
* @param int $type |
|
395
|
|
|
* |
|
396
|
|
|
* Display $log string. |
|
397
|
|
|
*/ |
|
398
|
|
|
public function showLog($type = 0) |
|
399
|
|
|
{ |
|
400
|
|
|
if ($log = $this->getLog($type)) { |
|
401
|
|
|
foreach ($log as $value) { |
|
402
|
|
|
printf('%s', $value['log']); |
|
403
|
|
|
} |
|
404
|
|
|
} |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* @return array $options |
|
409
|
|
|
*/ |
|
410
|
|
|
public function getBuildOptions() |
|
411
|
|
|
{ |
|
412
|
|
|
return $this->options; |
|
413
|
|
|
} |
|
414
|
|
|
|
|
415
|
|
|
/** |
|
416
|
|
|
* Builds a new website. |
|
417
|
|
|
* |
|
418
|
|
|
* @param array $options |
|
419
|
|
|
* |
|
420
|
|
|
* @return $this |
|
421
|
|
|
*/ |
|
422
|
|
|
public function build($options) |
|
423
|
|
|
{ |
|
424
|
|
|
// start script time |
|
425
|
|
|
$startTime = microtime(true); |
|
426
|
|
|
// backward compatibility |
|
427
|
|
|
if ($options === true) { |
|
|
|
|
|
|
428
|
|
|
$options['verbosity'] = self::VERBOSITY_VERBOSE; |
|
429
|
|
|
} |
|
430
|
|
|
$this->options = array_merge([ |
|
431
|
|
|
'verbosity' => self::VERBOSITY_NORMAL, // -1: quiet, 0: normal, 1: verbose, 2: debug |
|
432
|
|
|
'drafts' => false, // build drafts or not |
|
433
|
|
|
'dry-run' => false, // if dry-run is true, generated files are not saved |
|
434
|
|
|
], $options); |
|
435
|
|
|
|
|
436
|
|
|
$steps = []; |
|
437
|
|
|
// init... |
|
438
|
|
|
foreach ($this->steps as $step) { |
|
439
|
|
|
/* @var $stepClass Step\StepInterface */ |
|
440
|
|
|
$stepClass = new $step($this); |
|
441
|
|
|
$stepClass->init($this->options); |
|
442
|
|
|
$steps[] = $stepClass; |
|
443
|
|
|
} |
|
444
|
|
|
$this->steps = $steps; |
|
445
|
|
|
// ... and process! |
|
446
|
|
|
foreach ($this->steps as $step) { |
|
447
|
|
|
/* @var $step Step\StepInterface */ |
|
448
|
|
|
$step->runProcess(); |
|
449
|
|
|
} |
|
450
|
|
|
// show process time |
|
451
|
|
|
call_user_func_array($this->messageCallback, [ |
|
452
|
|
|
'TIME', |
|
453
|
|
|
sprintf('Built in %ss', round(microtime(true) - $startTime, 2)), |
|
454
|
|
|
]); |
|
455
|
|
|
// show log |
|
456
|
|
|
$this->showLog($this->options['verbosity']); |
|
457
|
|
|
|
|
458
|
|
|
return $this; |
|
459
|
|
|
} |
|
460
|
|
|
|
|
461
|
|
|
/** |
|
462
|
|
|
* Return version. |
|
463
|
|
|
* |
|
464
|
|
|
* @return string |
|
465
|
|
|
*/ |
|
466
|
|
|
public static function getVersion() |
|
467
|
|
|
{ |
|
468
|
|
|
if (!isset(self::$version)) { |
|
469
|
|
|
$filePath = __DIR__.'/../VERSION'; |
|
470
|
|
|
if (Plateform::isPhar()) { |
|
471
|
|
|
$filePath = Plateform::getPharPath().'/VERSION'; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
try { |
|
475
|
|
|
if (!file_exists($filePath)) { |
|
476
|
|
|
throw new \Exception(sprintf('%s file doesn\'t exist!', $filePath)); |
|
477
|
|
|
} |
|
478
|
|
|
self::$version = trim(file_get_contents($filePath)); |
|
479
|
|
|
if (self::$version === false) { |
|
480
|
|
|
throw new \Exception(sprintf('Can\'t get %s file!', $filePath)); |
|
481
|
|
|
} |
|
482
|
|
|
} catch (\Exception $e) { |
|
483
|
|
|
self::$version = self::VERSION; |
|
484
|
|
|
} |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
return self::$version; |
|
488
|
|
|
} |
|
489
|
|
|
} |
|
490
|
|
|
|