Completed
Push — include-lib ( fd24a6...4173d3 )
by Arnaud
13:24
created

Builder::setDestinationDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\Collection as PageCollection;
12
use Cecil\Generator\GeneratorManager;
13
use Symfony\Component\Finder\Finder;
14
15
/**
16
 * Class Builder.
17
 */
18
class Builder
19
{
20
    const VERSION = '2.x-dev';
21
    const VERBOSITY_QUIET = -1;
22
    const VERBOSITY_NORMAL = 0;
23
    const VERBOSITY_VERBOSE = 1;
24
    const VERBOSITY_DEBUG = 2;
25
26
    /**
27
     * Library version.
28
     *
29
     * @var string
30
     */
31
    protected $version;
32
    /**
33
     * Steps that are processed by build().
34
     *
35
     * @var array
36
     *
37
     * @see build()
38
     */
39
    protected $steps = [
40
        'Cecil\Step\ImportConfig',
41
        'Cecil\Step\LocateContent',
42
        'Cecil\Step\CreatePages',
43
        'Cecil\Step\ConvertPages',
44
        'Cecil\Step\GeneratePages',
45
        'Cecil\Step\GenerateMenus',
46
        'Cecil\Step\CopyStatic',
47
        'Cecil\Step\RenderPages',
48
        'Cecil\Step\SavePages',
49
    ];
50
    /**
51
     * Config.
52
     *
53
     * @var Config
54
     */
55
    protected $config;
56
    /**
57
     * Content iterator.
58
     *
59
     * @var Finder
60
     */
61
    protected $content;
62
    /**
63
     * Pages collection.
64
     *
65
     * @var PageCollection
66
     */
67
    protected $pages;
68
    /**
69
     * Collection of site menus.
70
     *
71
     * @var Collection\Menu\Collection
72
     */
73
    protected $menus;
74
    /**
75
     * Twig renderer.
76
     *
77
     * @var Renderer\Twig
78
     */
79
    protected $renderer;
80
    /**
81
     * @var \Closure
82
     */
83
    protected $messageCallback;
84
    /**
85
     * @var GeneratorManager
86
     */
87
    protected $generatorManager;
88
    /**
89
     * @var array
90
     */
91
    protected $log;
92
    /**
93
     * @var array
94
     */
95
    protected $options;
96
97
    /**
98
     * Builder constructor.
99
     *
100
     * @param Config|array|null $config
101
     * @param \Closure|null     $messageCallback
102
     */
103
    public function __construct($config = null, \Closure $messageCallback = null)
104
    {
105
        $this->setConfig($config)
106
            ->setSourceDir(null)
107
            ->setDestinationDir(null);
108
        $this->setMessageCallback($messageCallback);
109
    }
110
111
    /**
112
     * Creates a new Builder instance.
113
     *
114
     * @return Builder
115
     */
116
    public static function create()
117
    {
118
        $class = new \ReflectionClass(get_called_class());
119
120
        return $class->newInstanceArgs(func_get_args());
121
    }
122
123
    /**
124
     * Set config.
125
     *
126
     * @param Config|array|null $config
127
     *
128
     * @return $this
129
     */
130
    public function setConfig($config)
131
    {
132
        if (!$config instanceof Config) {
133
            $config = new Config($config);
134
        }
135
        if ($this->config !== $config) {
136
            $this->config = $config;
137
        }
138
139
        return $this;
140
    }
141
142
    /**
143
     * @return Config
144
     */
145
    public function getConfig()
146
    {
147
        return $this->config;
148
    }
149
150
    /**
151
     * Config::setSourceDir alias.
152
     *
153
     * @param $sourceDir
154
     *
155
     * @return $this
156
     */
157
    public function setSourceDir($sourceDir)
158
    {
159
        $this->config->setSourceDir($sourceDir);
160
161
        return $this;
162
    }
163
164
    /**
165
     * Config::setDestinationDir alias.
166
     *
167
     * @param $destinationDir
168
     *
169
     * @return $this
170
     */
171
    public function setDestinationDir($destinationDir)
172
    {
173
        $this->config->setDestinationDir($destinationDir);
174
175
        return $this;
176
    }
177
178
    /**
179
     * @param $content
180
     */
181
    public function setContent($content)
182
    {
183
        $this->content = $content;
184
    }
185
186
    /**
187
     * @return Finder
188
     */
189
    public function getContent()
190
    {
191
        return $this->content;
192
    }
193
194
    /**
195
     * @param $pages
196
     */
197
    public function setPages($pages)
198
    {
199
        $this->pages = $pages;
200
    }
201
202
    /**
203
     * @return PageCollection
204
     */
205
    public function getPages()
206
    {
207
        return $this->pages;
208
    }
209
210
    /**
211
     * @param $menus
212
     */
213
    public function setMenus($menus)
214
    {
215
        $this->menus = $menus;
216
    }
217
218
    /**
219
     * @return Collection\Menu\Collection
220
     */
221
    public function getMenus()
222
    {
223
        return $this->menus;
224
    }
225
226
    /**
227
     * @param \Closure|null $messageCallback
228
     */
229
    public function setMessageCallback($messageCallback = null)
230
    {
231
        if ($messageCallback === null) {
232
            $messageCallback = function ($code, $message = '', $itemsCount = 0, $itemsMax = 0) {
233
                switch ($code) {
234
                    case 'CONFIG':
235
                    case 'LOCATE':
236
                    case 'CREATE':
237
                    case 'CONVERT':
238
                    case 'GENERATE':
239
                    case 'MENU':
240
                    case 'COPY':
241
                    case 'RENDER':
242
                    case 'SAVE':
243
                    case 'TIME':
244
                        $log = sprintf("%s\n", $message);
245
                        $this->addLog($log);
246
                        break;
247
                    case 'CONFIG_PROGRESS':
248
                    case 'LOCATE_PROGRESS':
249
                    case 'CREATE_PROGRESS':
250
                    case 'CONVERT_PROGRESS':
251
                    case 'GENERATE_PROGRESS':
252
                    case 'MENU_PROGRESS':
253
                    case 'COPY_PROGRESS':
254
                    case 'RENDER_PROGRESS':
255
                    case 'SAVE_PROGRESS':
256
                        if ($itemsCount > 0) {
257
                            $log = sprintf("(%u/%u) %s\n", $itemsCount, $itemsMax, $message);
258
                            $this->addLog($log, 1);
259
                        } else {
260
                            $log = sprintf("%s\n", $message);
261
                            $this->addLog($log, 1);
262
                        }
263
                        break;
264
                    case 'LOCATE_ERROR':
265
                    case 'CREATE_ERROR':
266
                    case 'CONVERT_ERROR':
267
                    case 'GENERATE_ERROR':
268
                    case 'MENU_ERROR':
269
                    case 'COPY_ERROR':
270
                    case 'RENDER_ERROR':
271
                    case 'SAVE_ERROR':
272
                        $log = sprintf(">> %s\n", $message);
273
                        $this->addLog($log);
274
                        break;
275
                }
276
            };
277
        }
278
        $this->messageCallback = $messageCallback;
279
    }
280
281
    /**
282
     * @return \Closure
283
     */
284
    public function getMessageCb()
285
    {
286
        return $this->messageCallback;
287
    }
288
289
    /**
290
     * @param $renderer
291
     */
292
    public function setRenderer($renderer)
293
    {
294
        $this->renderer = $renderer;
295
    }
296
297
    /**
298
     * @return Renderer\Twig
299
     */
300
    public function getRenderer()
301
    {
302
        return $this->renderer;
303
    }
304
305
    /**
306
     * @param string $log
307
     * @param int    $type
308
     *
309
     * @return array|null
310
     */
311
    public function addLog($log, $type = 0)
312
    {
313
        $this->log[] = [
314
            'type' => $type,
315
            'log'  => $log,
316
        ];
317
318
        return $this->getLog($type);
319
    }
320
321
    /**
322
     * @param int $type
323
     *
324
     * @return array|null
325
     */
326
    public function getLog($type = 0)
327
    {
328
        if (is_array($this->log)) {
329
            return array_filter($this->log, function ($key) use ($type) {
330
                return $key['type'] <= $type;
331
            });
332
        }
333
    }
334
335
    /**
336
     * @param int $type
337
     *
338
     * Display $log string.
339
     */
340
    public function showLog($type = 0)
341
    {
342
        if ($log = $this->getLog($type)) {
343
            foreach ($log as $value) {
344
                printf('%s', $value['log']);
345
            }
346
        }
347
    }
348
349
    /**
350
     * @return array $options
351
     */
352
    public function getBuildOptions()
353
    {
354
        return $this->options;
355
    }
356
357
    /**
358
     * Builds a new website.
359
     *
360
     * @param array $options
361
     *
362
     * @return $this
363
     */
364
    public function build($options)
365
    {
366
        // backward compatibility
367
        if ($options === true) {
368
            $options['verbosity'] = self::VERBOSITY_VERBOSE;
369
        }
370
        $this->options = array_merge([
371
            'verbosity' => self::VERBOSITY_NORMAL, // -1: quiet, 0: normal, 1: verbose, 2: debug
372
            'drafts'    => false, // build drafts or not
373
            'dry-run'   => false, // if dry-run is true, generated files are not saved
374
        ], $options);
375
376
        $steps = [];
377
        // init...
378
        foreach ($this->steps as $step) {
379
            /* @var $stepClass Step\StepInterface */
380
            $stepClass = new $step($this);
381
            $stepClass->init($this->options);
382
            $steps[] = $stepClass;
383
        }
384
        $this->steps = $steps;
385
        // ... and process!
386
        foreach ($this->steps as $step) {
387
            /* @var $step Step\StepInterface */
388
            $step->runProcess();
389
        }
390
        // show process time
391
        call_user_func_array($this->messageCallback, [
392
            'TIME',
393
            sprintf('Built in %ss', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2)),
394
        ]);
395
        // show log
396
        $this->showLog($this->options['verbosity']);
397
398
        return $this;
399
    }
400
401
    /**
402
     * Return version.
403
     *
404
     * @return string
405
     */
406
    public function getVersion()
407
    {
408
        if (!isset($this->version)) {
409
            try {
410
                $this->version = @file_get_contents(__DIR__.'/../VERSION');
411
                if ($this->version === false) {
412
                    throw new \Exception('Can\'t get version file!');
413
                }
414
            } catch (\Exception $e) {
415
                $this->version = self::VERSION;
416
            }
417
        }
418
419
        return $this->version;
420
    }
421
}
422