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\Collection as PageCollection; |
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 = '4.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\ImportConfig', |
42
|
|
|
'Cecil\Step\LocateContent', |
43
|
|
|
'Cecil\Step\CreatePages', |
44
|
|
|
'Cecil\Step\ConvertPages', |
45
|
|
|
'Cecil\Step\GeneratePages', |
46
|
|
|
'Cecil\Step\GenerateMenus', |
47
|
|
|
'Cecil\Step\CopyStatic', |
48
|
|
|
'Cecil\Step\RenderPages', |
49
|
|
|
'Cecil\Step\SavePages', |
50
|
|
|
]; |
51
|
|
|
/** |
52
|
|
|
* Config. |
53
|
|
|
* |
54
|
|
|
* @var Config |
55
|
|
|
*/ |
56
|
|
|
protected $config; |
57
|
|
|
/** |
58
|
|
|
* Content iterator. |
59
|
|
|
* |
60
|
|
|
* @var Finder |
61
|
|
|
*/ |
62
|
|
|
protected $content; |
63
|
|
|
/** |
64
|
|
|
* Pages collection. |
65
|
|
|
* |
66
|
|
|
* @var PageCollection |
67
|
|
|
*/ |
68
|
|
|
protected $pages; |
69
|
|
|
/** |
70
|
|
|
* Collection of site menus. |
71
|
|
|
* |
72
|
|
|
* @var Collection\Menu\Collection |
73
|
|
|
*/ |
74
|
|
|
protected $menus; |
75
|
|
|
/** |
76
|
|
|
* Twig renderer. |
77
|
|
|
* |
78
|
|
|
* @var Renderer\Twig |
79
|
|
|
*/ |
80
|
|
|
protected $renderer; |
81
|
|
|
/** |
82
|
|
|
* @var \Closure |
83
|
|
|
*/ |
84
|
|
|
protected $messageCallback; |
85
|
|
|
/** |
86
|
|
|
* @var GeneratorManager |
87
|
|
|
*/ |
88
|
|
|
protected $generatorManager; |
89
|
|
|
/** |
90
|
|
|
* @var array |
91
|
|
|
*/ |
92
|
|
|
protected $log; |
93
|
|
|
/** |
94
|
|
|
* @var array |
95
|
|
|
*/ |
96
|
|
|
protected $options; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Builder constructor. |
100
|
|
|
* |
101
|
|
|
* @param Config|array|null $config |
102
|
|
|
* @param \Closure|null $messageCallback |
103
|
|
|
*/ |
104
|
|
|
public function __construct($config = null, \Closure $messageCallback = null) |
105
|
|
|
{ |
106
|
|
|
$this->setConfig($config) |
107
|
|
|
->setSourceDir(null) |
108
|
|
|
->setDestinationDir(null); |
109
|
|
|
$this->setMessageCallback($messageCallback); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Creates a new Builder instance. |
114
|
|
|
* |
115
|
|
|
* @return Builder |
116
|
|
|
*/ |
117
|
|
|
public static function create() |
118
|
|
|
{ |
119
|
|
|
$class = new \ReflectionClass(get_called_class()); |
120
|
|
|
|
121
|
|
|
return $class->newInstanceArgs(func_get_args()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Set config. |
126
|
|
|
* |
127
|
|
|
* @param Config|array|null $config |
128
|
|
|
* |
129
|
|
|
* @return $this |
130
|
|
|
*/ |
131
|
|
|
public function setConfig($config) |
132
|
|
|
{ |
133
|
|
|
if (!$config instanceof Config) { |
134
|
|
|
$config = new Config($config); |
135
|
|
|
} |
136
|
|
|
if ($this->config !== $config) { |
137
|
|
|
$this->config = $config; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return Config |
145
|
|
|
*/ |
146
|
|
|
public function getConfig() |
147
|
|
|
{ |
148
|
|
|
return $this->config; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Config::setSourceDir alias. |
153
|
|
|
* |
154
|
|
|
* @param $sourceDir |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function setSourceDir($sourceDir) |
159
|
|
|
{ |
160
|
|
|
$this->config->setSourceDir($sourceDir); |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Config::setDestinationDir alias. |
167
|
|
|
* |
168
|
|
|
* @param $destinationDir |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
*/ |
172
|
|
|
public function setDestinationDir($destinationDir) |
173
|
|
|
{ |
174
|
|
|
$this->config->setDestinationDir($destinationDir); |
175
|
|
|
|
176
|
|
|
return $this; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param $content |
181
|
|
|
*/ |
182
|
|
|
public function setContent($content) |
183
|
|
|
{ |
184
|
|
|
$this->content = $content; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return Finder |
189
|
|
|
*/ |
190
|
|
|
public function getContent() |
191
|
|
|
{ |
192
|
|
|
return $this->content; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param $pages |
197
|
|
|
*/ |
198
|
|
|
public function setPages($pages) |
199
|
|
|
{ |
200
|
|
|
$this->pages = $pages; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return PageCollection |
205
|
|
|
*/ |
206
|
|
|
public function getPages() |
207
|
|
|
{ |
208
|
|
|
return $this->pages; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @param $menus |
213
|
|
|
*/ |
214
|
|
|
public function setMenus($menus) |
215
|
|
|
{ |
216
|
|
|
$this->menus = $menus; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* @return Collection\Menu\Collection |
221
|
|
|
*/ |
222
|
|
|
public function getMenus() |
223
|
|
|
{ |
224
|
|
|
return $this->menus; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param \Closure|null $messageCallback |
229
|
|
|
*/ |
230
|
|
|
public function setMessageCallback($messageCallback = null) |
231
|
|
|
{ |
232
|
|
|
if ($messageCallback === null) { |
233
|
|
|
$messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) { |
234
|
|
|
switch ($code) { |
235
|
|
|
case 'CONFIG': |
236
|
|
|
case 'LOCATE': |
237
|
|
|
case 'CREATE': |
238
|
|
|
case 'CONVERT': |
239
|
|
|
case 'GENERATE': |
240
|
|
|
case 'MENU': |
241
|
|
|
case 'COPY': |
242
|
|
|
case 'RENDER': |
243
|
|
|
case 'SAVE': |
244
|
|
|
case 'TIME': |
245
|
|
|
$log = sprintf("%s\n", $message); |
246
|
|
|
$this->addLog($log); |
247
|
|
|
break; |
248
|
|
|
case 'CONFIG_PROGRESS': |
249
|
|
|
case 'LOCATE_PROGRESS': |
250
|
|
|
case 'CREATE_PROGRESS': |
251
|
|
|
case 'CONVERT_PROGRESS': |
252
|
|
|
case 'GENERATE_PROGRESS': |
253
|
|
|
case 'MENU_PROGRESS': |
254
|
|
|
case 'COPY_PROGRESS': |
255
|
|
|
case 'RENDER_PROGRESS': |
256
|
|
|
case 'SAVE_PROGRESS': |
257
|
|
|
if ($itemsCount > 0) { |
258
|
|
|
$log = sprintf("(%u/%u) %s\n", $itemsCount, $itemsMax, $message); |
259
|
|
|
$this->addLog($log, 1); |
260
|
|
|
} else { |
261
|
|
|
$log = sprintf("%s\n", $message); |
262
|
|
|
$this->addLog($log, 1); |
263
|
|
|
} |
264
|
|
|
break; |
265
|
|
|
case 'LOCATE_ERROR': |
266
|
|
|
case 'CREATE_ERROR': |
267
|
|
|
case 'CONVERT_ERROR': |
268
|
|
|
case 'GENERATE_ERROR': |
269
|
|
|
case 'MENU_ERROR': |
270
|
|
|
case 'COPY_ERROR': |
271
|
|
|
case 'RENDER_ERROR': |
272
|
|
|
case 'SAVE_ERROR': |
273
|
|
|
$log = sprintf(">> %s\n", $message); |
274
|
|
|
$this->addLog($log); |
275
|
|
|
break; |
276
|
|
|
} |
277
|
|
|
}; |
278
|
|
|
} |
279
|
|
|
$this->messageCallback = $messageCallback; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* @return \Closure |
284
|
|
|
*/ |
285
|
|
|
public function getMessageCb() |
286
|
|
|
{ |
287
|
|
|
return $this->messageCallback; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* @param $renderer |
292
|
|
|
*/ |
293
|
|
|
public function setRenderer($renderer) |
294
|
|
|
{ |
295
|
|
|
$this->renderer = $renderer; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @return Renderer\Twig |
300
|
|
|
*/ |
301
|
|
|
public function getRenderer() |
302
|
|
|
{ |
303
|
|
|
return $this->renderer; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* @param string $log |
308
|
|
|
* @param int $type |
309
|
|
|
* |
310
|
|
|
* @return array|null |
311
|
|
|
*/ |
312
|
|
|
public function addLog($log, $type = 0) |
313
|
|
|
{ |
314
|
|
|
$this->log[] = [ |
315
|
|
|
'type' => $type, |
316
|
|
|
'log' => $log, |
317
|
|
|
]; |
318
|
|
|
|
319
|
|
|
return $this->getLog($type); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param int $type |
324
|
|
|
* |
325
|
|
|
* @return array|null |
326
|
|
|
*/ |
327
|
|
|
public function getLog($type = 0) |
328
|
|
|
{ |
329
|
|
|
if (is_array($this->log)) { |
330
|
|
|
return array_filter($this->log, function ($key) use ($type) { |
331
|
|
|
return $key['type'] <= $type; |
332
|
|
|
}); |
333
|
|
|
} |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* @param int $type |
338
|
|
|
* |
339
|
|
|
* Display $log string. |
340
|
|
|
*/ |
341
|
|
|
public function showLog($type = 0) |
342
|
|
|
{ |
343
|
|
|
if ($log = $this->getLog($type)) { |
344
|
|
|
foreach ($log as $value) { |
345
|
|
|
printf('%s', $value['log']); |
346
|
|
|
} |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* @return array $options |
352
|
|
|
*/ |
353
|
|
|
public function getBuildOptions() |
354
|
|
|
{ |
355
|
|
|
return $this->options; |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Builds a new website. |
360
|
|
|
* |
361
|
|
|
* @param array $options |
362
|
|
|
* |
363
|
|
|
* @return $this |
364
|
|
|
*/ |
365
|
|
|
public function build($options) |
366
|
|
|
{ |
367
|
|
|
// backward compatibility |
368
|
|
|
if ($options === true) { |
369
|
|
|
$options['verbosity'] = self::VERBOSITY_VERBOSE; |
370
|
|
|
} |
371
|
|
|
$this->options = array_merge([ |
372
|
|
|
'verbosity' => self::VERBOSITY_NORMAL, // -1: quiet, 0: normal, 1: verbose, 2: debug |
373
|
|
|
'drafts' => false, // build drafts or not |
374
|
|
|
'dry-run' => false, // if dry-run is true, generated files are not saved |
375
|
|
|
], $options); |
376
|
|
|
|
377
|
|
|
$steps = []; |
378
|
|
|
// init... |
379
|
|
|
foreach ($this->steps as $step) { |
380
|
|
|
/* @var $stepClass Step\StepInterface */ |
381
|
|
|
$stepClass = new $step($this); |
382
|
|
|
$stepClass->init($this->options); |
383
|
|
|
$steps[] = $stepClass; |
384
|
|
|
} |
385
|
|
|
$this->steps = $steps; |
386
|
|
|
// ... and process! |
387
|
|
|
foreach ($this->steps as $step) { |
388
|
|
|
/* @var $step Step\StepInterface */ |
389
|
|
|
$step->runProcess(); |
390
|
|
|
} |
391
|
|
|
// show process time |
392
|
|
|
call_user_func_array($this->messageCallback, [ |
393
|
|
|
'TIME', |
394
|
|
|
sprintf('Built in %ss', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2)), |
395
|
|
|
]); |
396
|
|
|
// show log |
397
|
|
|
$this->showLog($this->options['verbosity']); |
398
|
|
|
|
399
|
|
|
return $this; |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
/** |
403
|
|
|
* Return version. |
404
|
|
|
* |
405
|
|
|
* @return string |
406
|
|
|
*/ |
407
|
|
|
public static function getVersion() |
408
|
|
|
{ |
409
|
|
|
if (!isset(self::$version)) { |
410
|
|
|
$filePath = __DIR__.'/../VERSION'; |
411
|
|
|
if (Plateform::isPhar()) { |
412
|
|
|
$filePath = Plateform::getPharPath().'/VERSION'; |
413
|
|
|
} |
414
|
|
|
try { |
415
|
|
|
if (!file_exists($filePath)) { |
416
|
|
|
throw new \Exception(sprintf('%s file doesn\'t exist!', $filePath)); |
417
|
|
|
} |
418
|
|
|
self::$version = trim(file_get_contents($filePath)); |
419
|
|
|
if (self::$version === false) { |
420
|
|
|
throw new \Exception(sprintf('Can\'t get %s file!', $filePath)); |
421
|
|
|
} |
422
|
|
|
} catch (\Exception $e) { |
423
|
|
|
self::$version = self::VERSION; |
424
|
|
|
} |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
return self::$version; |
428
|
|
|
} |
429
|
|
|
} |
430
|
|
|
|