Completed
Push — feature-static-files ( 136c95 )
by Arnaud
05:49
created

Builder::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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