Completed
Push — taxonomies ( eb4f22 )
by Arnaud
02:03
created

Builder::getRenderer()   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 = '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\ConfigImport',
42
        'Cecil\Step\ContentLoad',
43
        'Cecil\Step\PagesCreate',
44
        'Cecil\Step\PagesConvert',
45
        'Cecil\Step\PagesGenerate',
46
        'Cecil\Step\MenusCreate',
47
        'Cecil\Step\StaticCopy',
48
        'Cecil\Step\PagesRender',
49
        'Cecil\Step\PagesSave',
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 PagesCollection
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 PagesCollection
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 $taxonomies
229
     */
230
    public function setTaxonomies($taxonomies)
231
    {
232
        $this->taxonomies = $taxonomies;
0 ignored issues
show
Bug introduced by
The property taxonomies does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
233
    }
234
235
    /**
236
     * @return Collection\Taxonomy\Collection
237
     */
238
    public function getTaxonomies()
239
    {
240
        return $this->taxonomies;
241
    }
242
243
    /**
244
     * @param \Closure|null $messageCallback
245
     */
246
    public function setMessageCallback($messageCallback = null)
247
    {
248
        if ($messageCallback === null) {
249
            $messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) {
250
                switch ($code) {
251
                    case 'CONFIG':
252
                    case 'LOCATE':
253
                    case 'CREATE':
254
                    case 'CONVERT':
255
                    case 'GENERATE':
256
                    case 'MENU':
257
                    case 'COPY':
258
                    case 'RENDER':
259
                    case 'SAVE':
260
                    case 'TIME':
261
                        $log = sprintf("%s\n", $message);
262
                        $this->addLog($log);
263
                        break;
264
                    case 'CONFIG_PROGRESS':
265
                    case 'LOCATE_PROGRESS':
266
                    case 'CREATE_PROGRESS':
267
                    case 'CONVERT_PROGRESS':
268
                    case 'GENERATE_PROGRESS':
269
                    case 'MENU_PROGRESS':
270
                    case 'COPY_PROGRESS':
271
                    case 'RENDER_PROGRESS':
272
                    case 'SAVE_PROGRESS':
273
                        if ($itemsCount > 0) {
274
                            $log = sprintf("(%u/%u) %s\n", $itemsCount, $itemsMax, $message);
275
                            $this->addLog($log, 1);
276
                        } else {
277
                            $log = sprintf("%s\n", $message);
278
                            $this->addLog($log, 1);
279
                        }
280
                        break;
281
                    case 'LOCATE_ERROR':
282
                    case 'CREATE_ERROR':
283
                    case 'CONVERT_ERROR':
284
                    case 'GENERATE_ERROR':
285
                    case 'MENU_ERROR':
286
                    case 'COPY_ERROR':
287
                    case 'RENDER_ERROR':
288
                    case 'SAVE_ERROR':
289
                        $log = sprintf(">> %s\n", $message);
290
                        $this->addLog($log);
291
                        break;
292
                }
293
            };
294
        }
295
        $this->messageCallback = $messageCallback;
296
    }
297
298
    /**
299
     * @return \Closure
300
     */
301
    public function getMessageCb()
302
    {
303
        return $this->messageCallback;
304
    }
305
306
    /**
307
     * @param $renderer
308
     */
309
    public function setRenderer($renderer)
310
    {
311
        $this->renderer = $renderer;
312
    }
313
314
    /**
315
     * @return Renderer\Twig
316
     */
317
    public function getRenderer()
318
    {
319
        return $this->renderer;
320
    }
321
322
    /**
323
     * @param string $log
324
     * @param int    $type
325
     *
326
     * @return array|null
327
     */
328
    public function addLog($log, $type = 0)
329
    {
330
        $this->log[] = [
331
            'type' => $type,
332
            'log'  => $log,
333
        ];
334
335
        return $this->getLog($type);
336
    }
337
338
    /**
339
     * @param int $type
340
     *
341
     * @return array|null
342
     */
343
    public function getLog($type = 0)
344
    {
345
        if (is_array($this->log)) {
346
            return array_filter($this->log, function ($key) use ($type) {
347
                return $key['type'] <= $type;
348
            });
349
        }
350
    }
351
352
    /**
353
     * @param int $type
354
     *
355
     * Display $log string.
356
     */
357
    public function showLog($type = 0)
358
    {
359
        if ($log = $this->getLog($type)) {
360
            foreach ($log as $value) {
361
                printf('%s', $value['log']);
362
            }
363
        }
364
    }
365
366
    /**
367
     * @return array $options
368
     */
369
    public function getBuildOptions()
370
    {
371
        return $this->options;
372
    }
373
374
    /**
375
     * Builds a new website.
376
     *
377
     * @param array $options
378
     *
379
     * @return $this
380
     */
381
    public function build($options)
382
    {
383
        // backward compatibility
384
        if ($options === true) {
385
            $options['verbosity'] = self::VERBOSITY_VERBOSE;
386
        }
387
        $this->options = array_merge([
388
            'verbosity' => self::VERBOSITY_NORMAL, // -1: quiet, 0: normal, 1: verbose, 2: debug
389
            'drafts'    => false, // build drafts or not
390
            'dry-run'   => false, // if dry-run is true, generated files are not saved
391
        ], $options);
392
393
        $steps = [];
394
        // init...
395
        foreach ($this->steps as $step) {
396
            /* @var $stepClass Step\StepInterface */
397
            $stepClass = new $step($this);
398
            $stepClass->init($this->options);
399
            $steps[] = $stepClass;
400
        }
401
        $this->steps = $steps;
402
        // ... and process!
403
        foreach ($this->steps as $step) {
404
            /* @var $step Step\StepInterface */
405
            $step->runProcess();
406
        }
407
        // show process time
408
        call_user_func_array($this->messageCallback, [
409
            'TIME',
410
            sprintf('Built in %ss', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2)),
411
        ]);
412
        // show log
413
        $this->showLog($this->options['verbosity']);
414
415
        return $this;
416
    }
417
418
    /**
419
     * Return version.
420
     *
421
     * @return string
422
     */
423
    public static function getVersion()
424
    {
425
        if (!isset(self::$version)) {
426
            $filePath = __DIR__.'/../VERSION';
427
            if (Plateform::isPhar()) {
428
                $filePath = Plateform::getPharPath().'/VERSION';
429
            }
430
431
            try {
432
                if (!file_exists($filePath)) {
433
                    throw new \Exception(sprintf('%s file doesn\'t exist!', $filePath));
434
                }
435
                self::$version = trim(file_get_contents($filePath));
436
                if (self::$version === false) {
437
                    throw new \Exception(sprintf('Can\'t get %s file!', $filePath));
438
                }
439
            } catch (\Exception $e) {
440
                self::$version = self::VERSION;
441
            }
442
        }
443
444
        return self::$version;
445
    }
446
}
447