Passed
Push — logger ( 576894 )
by Arnaud
11:17
created

Builder::getStatic()   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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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 that are 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\AssetsCopy',
51
        'Cecil\Step\PostProcessHtml',
52
        'Cecil\Step\PostProcessCss',
53
        'Cecil\Step\PostProcessJs',
54
        'Cecil\Step\PostProcessImages',
55
    ];
56
    /** @var string App version. */
57
    protected static $version;
58
    /** @var Config Configuration. */
59
    protected $config;
60
    /** @var Finder Content iterator. */
61
    protected $content;
62
    /** @var array Data collection. */
63
    protected $data = [];
64
    /** @var array Static files collection. */
65
    protected $static = [];
66
    /** @var PagesCollection Pages collection. */
67
    protected $pages;
68
    /** @var Collection\Menu\Collection Menus collection. */
69
    protected $menus;
70
    /** @var Collection\Taxonomy\Collection Taxonomies collection. */
71
    protected $taxonomies;
72
    /** @var Renderer\RendererInterface Renderer. */
73
    protected $renderer;
74
    /** @var \Closure Message callback. */
75
    protected $messageCallback;
76
    /** @var GeneratorManager Generators manager. */
77
    protected $generatorManager;
78
    /** @var array Log. */
79
    protected $log;
80
    /** @var array Options. */
81
    protected $options;
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
        if ($logger === null) {
92
            $logger = new PrintLogger(); // default logger
93
        }
94
        $this->setLogger($logger);
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function setLogger(LoggerInterface $logger)
101
    {
102
        $this->logger = $logger;
0 ignored issues
show
Bug Best Practice introduced by
The property logger does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
103
    }
104
105
    /**
106
     * Returns the logger instance.
107
     *
108
     * @return LoggerInterface
109
     */
110
    public function getLogger(): LoggerInterface
111
    {
112
        return $this->logger;
113
    }
114
115
    /**
116
     * Creates a new Builder instance.
117
     *
118
     * @return Builder
119
     */
120
    public static function create(): self
121
    {
122
        $class = new \ReflectionClass(get_called_class());
123
124
        return $class->newInstanceArgs(func_get_args());
125
    }
126
127
    /**
128
     * Set configuration.
129
     *
130
     * @param Config|array|null $config
131
     *
132
     * @return self
133
     */
134
    public function setConfig($config): self
135
    {
136
        if (!$config instanceof Config) {
137
            $config = new Config($config);
138
        }
139
        if ($this->config !== $config) {
140
            $this->config = $config;
141
        }
142
143
        return $this;
144
    }
145
146
    /**
147
     * @return Config
148
     */
149
    public function getConfig(): Config
150
    {
151
        return $this->config;
152
    }
153
154
    /**
155
     * Config::setSourceDir() alias.
156
     *
157
     * @param string|null $sourceDir
158
     *
159
     * @return self
160
     */
161
    public function setSourceDir(string $sourceDir = null): self
162
    {
163
        $this->config->setSourceDir($sourceDir);
164
165
        return $this;
166
    }
167
168
    /**
169
     * Config::setDestinationDir() alias.
170
     *
171
     * @param string|null $destinationDir
172
     *
173
     * @return self
174
     */
175
    public function setDestinationDir(string $destinationDir = null): self
176
    {
177
        $this->config->setDestinationDir($destinationDir);
178
179
        return $this;
180
    }
181
182
    /**
183
     * Set collected content.
184
     *
185
     * @param Finder $content
186
     *
187
     * @return void
188
     */
189
    public function setContent(Finder $content): void
190
    {
191
        $this->content = $content;
192
    }
193
194
    /**
195
     * @return Finder|null
196
     */
197
    public function getContent(): ?Finder
198
    {
199
        return $this->content;
200
    }
201
202
    /**
203
     * Set collected data.
204
     *
205
     * @param array $data
206
     *
207
     * @return void
208
     */
209
    public function setData(array $data): void
210
    {
211
        $this->data = $data;
212
    }
213
214
    /**
215
     * @return array
216
     */
217
    public function getData(): array
218
    {
219
        return $this->data;
220
    }
221
222
    /**
223
     * Set collected static files.
224
     *
225
     * @param array $static
226
     *
227
     * @return void
228
     */
229
    public function setStatic(array $static): void
230
    {
231
        $this->static = $static;
232
    }
233
234
    /**
235
     * @return array Static files collection.
236
     */
237
    public function getStatic(): array
238
    {
239
        return $this->static;
240
    }
241
242
    /**
243
     * Set/update Pages colelction.
244
     *
245
     * @param PagesCollection $pages
246
     *
247
     * @return void
248
     */
249
    public function setPages(PagesCollection $pages): void
250
    {
251
        $this->pages = $pages;
252
    }
253
254
    /**
255
     * @return PagesCollection
256
     */
257
    public function getPages(): PagesCollection
258
    {
259
        return $this->pages;
260
    }
261
262
    /**
263
     * @param Collection\Menu\Collection $menus
264
     *
265
     * @return void
266
     */
267
    public function setMenus(Collection\Menu\Collection $menus): void
268
    {
269
        $this->menus = $menus;
270
    }
271
272
    /**
273
     * @return Collection\Menu\Collection
274
     */
275
    public function getMenus(): Collection\Menu\Collection
276
    {
277
        return $this->menus;
278
    }
279
280
    /**
281
     * Set taxonomies collection.
282
     *
283
     * @param Collection\Taxonomy\Collection $taxonomies
284
     *
285
     * @return void
286
     */
287
    public function setTaxonomies(Collection\Taxonomy\Collection $taxonomies): void
288
    {
289
        $this->taxonomies = $taxonomies;
290
    }
291
292
    /**
293
     * @return Collection\Taxonomy\Collection|null
294
     */
295
    public function getTaxonomies(): ?Collection\Taxonomy\Collection
296
    {
297
        return $this->taxonomies;
298
    }
299
300
    /**
301
     * Set renderer object.
302
     *
303
     * @param Renderer\RendererInterface $renderer
304
     *
305
     * @return void
306
     */
307
    public function setRenderer(Renderer\RendererInterface $renderer): void
308
    {
309
        $this->renderer = $renderer;
310
    }
311
312
    /**
313
     * @return Renderer\RendererInterface
314
     */
315
    public function getRenderer(): Renderer\RendererInterface
316
    {
317
        return $this->renderer;
318
    }
319
320
    /**
321
     * @return array $options
322
     */
323
    public function getBuildOptions(): array
324
    {
325
        return $this->options;
326
    }
327
328
    /**
329
     * Builds a new website.
330
     *
331
     * @param array $options
332
     *
333
     * @return self
334
     */
335
    public function build(array $options): self
336
    {
337
        // set start script time
338
        $startTime = microtime(true);
339
        // prepare options
340
        $this->options = array_merge([
341
            'verbosity' => self::VERBOSITY_NORMAL,
342
            'drafts'    => false, // build drafts or not
343
            'dry-run'   => false, // if dry-run is true, generated files are not saved
344
        ], $options);
345
346
        // process each step
347
        $steps = [];
348
        // init...
349
        foreach ($this->steps as $step) {
350
            /** @var Step\StepInterface $stepClass */
351
            $stepClass = new $step($this);
352
            $stepClass->init($this->options);
353
            $steps[] = $stepClass;
354
        }
355
        $this->steps = $steps;
356
        // ... and process!
357
        foreach ($this->steps as $step) {
358
            /** @var Step\StepInterface $step */
359
            $step->runProcess();
360
        }
361
362
        // add process duration to log
363
        call_user_func_array($this->messageCallback, [
364
            'TIME',
365
            sprintf('Built in %ss', round(microtime(true) - $startTime, 2)),
366
        ]);
367
        // show log
368
        //$this->showLog($this->options['verbosity']);
369
370
        return $this;
371
    }
372
373
    /**
374
     * Return version.
375
     *
376
     * @return string
377
     */
378
    public static function getVersion(): string
379
    {
380
        if (!isset(self::$version)) {
381
            $filePath = __DIR__.'/../VERSION';
382
            if (Plateform::isPhar()) {
383
                $filePath = Plateform::getPharPath().'/VERSION';
384
            }
385
386
            try {
387
                if (!file_exists($filePath)) {
388
                    throw new \Exception(sprintf('%s file doesn\'t exist!', $filePath));
389
                }
390
                $version = Util::fileGetContents($filePath);
391
                if ($version === false) {
392
                    throw new \Exception(sprintf('Can\'t get %s file!', $filePath));
393
                }
394
                self::$version = trim($version);
395
            } catch (\Exception $e) {
396
                self::$version = self::VERSION;
397
            }
398
        }
399
400
        return self::$version;
401
    }
402
}
403