Passed
Push — fix/debug-mode ( e4c63f )
by Arnaud
05:20
created

Builder::getConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil;
12
13
use Cecil\Collection\Page\Collection as PagesCollection;
14
use Cecil\Generator\GeneratorManager;
15
use Cecil\Logger\PrintLogger;
16
use Cecil\Util\Plateform;
17
use Psr\Log\LoggerAwareInterface;
18
use Psr\Log\LoggerInterface;
19
use Symfony\Component\Finder\Finder;
20
21
/**
22
 * Class Builder.
23
 */
24
class Builder implements LoggerAwareInterface
25
{
26
    const VERSION = '5.x-dev';
27
    const VERBOSITY_QUIET = -1;
28
    const VERBOSITY_NORMAL = 0;
29
    const VERBOSITY_VERBOSE = 1;
30
    const VERBOSITY_DEBUG = 2;
31
32
    /**
33
     * @var array Steps processed by build().
34
     *
35
     * @see build()
36
     */
37
    protected $steps = [
38
        'Cecil\Step\ConfigImport',
39
        'Cecil\Step\ContentLoad',
40
        'Cecil\Step\DataLoad',
41
        'Cecil\Step\StaticLoad',
42
        'Cecil\Step\PagesCreate',
43
        'Cecil\Step\PagesConvert',
44
        'Cecil\Step\TaxonomiesCreate',
45
        'Cecil\Step\PagesGenerate',
46
        'Cecil\Step\MenusCreate',
47
        'Cecil\Step\StaticCopy',
48
        'Cecil\Step\PagesRender',
49
        'Cecil\Step\PagesSave',
50
        'Cecil\Step\PostProcessHtml',
51
        'Cecil\Step\PostProcessCss',
52
        'Cecil\Step\PostProcessJs',
53
        'Cecil\Step\PostProcessImages',
54
    ];
55
56
    /** @var Config Configuration. */
57
    protected $config;
58
    /** @var LoggerInterface Logger. */
59
    protected $logger;
60
    /** @var bool Debug mode. */
61
    protected $debug = false;
62
    /** @var array Build options. */
63
    protected $options;
64
    /** @var Finder Content iterator. */
65
    protected $content;
66
    /** @var array Data collection. */
67
    protected $data = [];
68
    /** @var array Static files collection. */
69
    protected $static = [];
70
    /** @var PagesCollection Pages collection. */
71
    protected $pages;
72
    /** @var Collection\Menu\Collection Menus collection. */
73
    protected $menus;
74
    /** @var Collection\Taxonomy\Collection Taxonomies collection. */
75
    protected $taxonomies;
76
    /** @var Renderer\RendererInterface Renderer. */
77
    protected $renderer;
78
    /** @var GeneratorManager Generators manager. */
79
    protected $generatorManager;
80
    /** @var string Application version. */
81
    protected static $version;
82
83
    /**
84
     * @param Config|array|null    $config
85
     * @param LoggerInterface|null $logger
86
     */
87
    public function __construct($config = null, LoggerInterface $logger = null)
88
    {
89
        $this->setConfig($config)->setSourceDir(null)->setDestinationDir(null);
90
91
        // default logger
92
        if ($logger === null) {
93
            $logger = new PrintLogger(self::VERBOSITY_VERBOSE);
94
        }
95
        $this->setLogger($logger);
96
97
        // debug mode?
98
        if (getenv('CECIL_DEBUG') == 'true' || (bool) $this->getConfig()->get('debug')) {
99
            $this->debug = true;
100
        }
101
    }
102
103
    /**
104
     * Creates a new Builder instance.
105
     *
106
     * @return Builder
107
     */
108
    public static function create(): self
109
    {
110
        $class = new \ReflectionClass(get_called_class());
111
112
        return $class->newInstanceArgs(func_get_args());
113
    }
114
115
    /**
116
     * Builds a new website.
117
     *
118
     * @param array $options
119
     *
120
     * @return self
121
     */
122
    public function build(array $options): self
123
    {
124
        // set start script time
125
        $startTime = microtime(true);
126
        // prepare options
127
        $this->options = array_merge([
128
            'drafts'  => false, // build drafts or not
129
            'dry-run' => false, // if dry-run is true, generated files are not saved
130
        ], $options);
131
132
        // process each step
133
        $steps = [];
134
        // init...
135
        foreach ($this->steps as $step) {
136
            /** @var Step\StepInterface $stepObject */
137
            $stepObject = new $step($this);
138
            $stepObject->init($this->options);
139
            if ($stepObject->canProcess()) {
140
                $steps[] = $stepObject;
141
            }
142
        }
143
        // ...and process!
144
        $stepNumber = 0;
145
        $stepsTotal = count($steps);
146
        foreach ($steps as $step) {
147
            $stepNumber++;
148
            /** @var Step\StepInterface $step */
149
            $this->getLogger()->notice($step->getName(), ['step' => [$stepNumber, $stepsTotal]]);
150
            $step->process();
151
        }
152
153
        // process duration
154
        $message = sprintf('Built in %ss', round(microtime(true) - $startTime, 2));
155
        $this->getLogger()->notice($message);
156
157
        return $this;
158
    }
159
160
    /**
161
     * Set configuration.
162
     *
163
     * @param Config|array|null $config
164
     *
165
     * @return self
166
     */
167
    public function setConfig($config): self
168
    {
169
        if (!$config instanceof Config) {
170
            $config = new Config($config);
171
        }
172
        if ($this->config !== $config) {
173
            $this->config = $config;
174
        }
175
176
        return $this;
177
    }
178
179
    /**
180
     * @return Config
181
     */
182
    public function getConfig(): Config
183
    {
184
        return $this->config;
185
    }
186
187
    /**
188
     * Config::setSourceDir() alias.
189
     *
190
     * @param string|null $sourceDir
191
     *
192
     * @return self
193
     */
194
    public function setSourceDir(string $sourceDir = null): self
195
    {
196
        $this->config->setSourceDir($sourceDir);
197
198
        return $this;
199
    }
200
201
    /**
202
     * Config::setDestinationDir() alias.
203
     *
204
     * @param string|null $destinationDir
205
     *
206
     * @return self
207
     */
208
    public function setDestinationDir(string $destinationDir = null): self
209
    {
210
        $this->config->setDestinationDir($destinationDir);
211
212
        return $this;
213
    }
214
215
    /**
216
     * {@inheritdoc}
217
     */
218
    public function setLogger(LoggerInterface $logger)
219
    {
220
        $this->logger = $logger;
221
    }
222
223
    /**
224
     * Returns the logger instance.
225
     *
226
     * @return LoggerInterface
227
     */
228
    public function getLogger(): LoggerInterface
229
    {
230
        return $this->logger;
231
    }
232
233
    /**
234
     * Returns debug mode state.
235
     *
236
     * @return bool
237
     */
238
    public function isDebug(): bool
239
    {
240
        return $this->debug;
241
    }
242
243
    /**
244
     * @return array $options Returns build options.
245
     */
246
    public function getBuildOptions(): array
247
    {
248
        return $this->options;
249
    }
250
251
    /**
252
     * Set collected content.
253
     *
254
     * @param Finder $content
255
     *
256
     * @return void
257
     */
258
    public function setContent(Finder $content): void
259
    {
260
        $this->content = $content;
261
    }
262
263
    /**
264
     * @return Finder|null
265
     */
266
    public function getContent(): ?Finder
267
    {
268
        return $this->content;
269
    }
270
271
    /**
272
     * Set collected data.
273
     *
274
     * @param array $data
275
     *
276
     * @return void
277
     */
278
    public function setData(array $data): void
279
    {
280
        $this->data = $data;
281
    }
282
283
    /**
284
     * @return array
285
     */
286
    public function getData(): array
287
    {
288
        return $this->data;
289
    }
290
291
    /**
292
     * Set collected static files.
293
     *
294
     * @param array $static
295
     *
296
     * @return void
297
     */
298
    public function setStatic(array $static): void
299
    {
300
        $this->static = $static;
301
    }
302
303
    /**
304
     * @return array Static files collection.
305
     */
306
    public function getStatic(): array
307
    {
308
        return $this->static;
309
    }
310
311
    /**
312
     * Set/update Pages colelction.
313
     *
314
     * @param PagesCollection $pages
315
     *
316
     * @return void
317
     */
318
    public function setPages(PagesCollection $pages): void
319
    {
320
        $this->pages = $pages;
321
    }
322
323
    /**
324
     * @return PagesCollection
325
     */
326
    public function getPages(): PagesCollection
327
    {
328
        return $this->pages;
329
    }
330
331
    /**
332
     * @param Collection\Menu\Collection $menus
333
     *
334
     * @return void
335
     */
336
    public function setMenus(Collection\Menu\Collection $menus): void
337
    {
338
        $this->menus = $menus;
339
    }
340
341
    /**
342
     * @return Collection\Menu\Collection
343
     */
344
    public function getMenus(): Collection\Menu\Collection
345
    {
346
        return $this->menus;
347
    }
348
349
    /**
350
     * Set taxonomies collection.
351
     *
352
     * @param Collection\Taxonomy\Collection $taxonomies
353
     *
354
     * @return void
355
     */
356
    public function setTaxonomies(Collection\Taxonomy\Collection $taxonomies): void
357
    {
358
        $this->taxonomies = $taxonomies;
359
    }
360
361
    /**
362
     * @return Collection\Taxonomy\Collection|null
363
     */
364
    public function getTaxonomies(): ?Collection\Taxonomy\Collection
365
    {
366
        return $this->taxonomies;
367
    }
368
369
    /**
370
     * Set renderer object.
371
     *
372
     * @param Renderer\RendererInterface $renderer
373
     *
374
     * @return void
375
     */
376
    public function setRenderer(Renderer\RendererInterface $renderer): void
377
    {
378
        $this->renderer = $renderer;
379
    }
380
381
    /**
382
     * @return Renderer\RendererInterface
383
     */
384
    public function getRenderer(): Renderer\RendererInterface
385
    {
386
        return $this->renderer;
387
    }
388
389
    /**
390
     * Returns application version.
391
     *
392
     * See VERSION file at root.
393
     *
394
     * @return string
395
     */
396
    public static function getVersion(): string
397
    {
398
        if (!isset(self::$version)) {
399
            $filePath = __DIR__.'/../VERSION';
400
            if (Plateform::isPhar()) {
401
                $filePath = Plateform::getPharPath().'/VERSION';
402
            }
403
404
            try {
405
                if (!file_exists($filePath)) {
406
                    throw new \Exception(sprintf('%s file doesn\'t exist!', $filePath));
407
                }
408
                $version = Util\File::fileGetContents($filePath);
409
                if ($version === false) {
410
                    throw new \Exception(sprintf('Can\'t get %s file!', $filePath));
411
                }
412
                self::$version = trim($version);
413
            } catch (\Exception $e) {
414
                self::$version = self::VERSION;
415
            }
416
        }
417
418
        return self::$version;
419
    }
420
}
421