Completed
Push — feature-data-files ( f34d72 )
by Arnaud
03:53
created

Builder::getData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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