• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 14620241211

23 Apr 2025 02:06PM UTC coverage: 83.787%. First build
14620241211

Pull #2148

github

web-flow
Merge 12fc09dec into 6d7ba8f0a
Pull Request #2148: refactor: configuration and cache

361 of 423 new or added lines in 26 files covered. (85.34%)

3049 of 3639 relevant lines covered (83.79%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
/src/Builder.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace Cecil;
15

16
use Cecil\Collection\Page\Collection as PagesCollection;
17
use Cecil\Exception\RuntimeException;
18
use Cecil\Generator\GeneratorManager;
19
use Cecil\Logger\PrintLogger;
20
use Psr\Log\LoggerAwareInterface;
21
use Psr\Log\LoggerInterface;
22
use Symfony\Component\Finder\Finder;
23

24
/**
25
 * Class Builder.
26
 */
27
class Builder implements LoggerAwareInterface
28
{
29
    public const VERSION = '8.x-dev';
30
    public const VERBOSITY_QUIET = -1;
31
    public const VERBOSITY_NORMAL = 0;
32
    public const VERBOSITY_VERBOSE = 1;
33
    public const VERBOSITY_DEBUG = 2;
34

35
    /**
36
     * @var array Steps processed by build().
37
     */
38
    protected $steps = [
39
        'Cecil\Step\Pages\Load',
40
        'Cecil\Step\Data\Load',
41
        'Cecil\Step\StaticFiles\Load',
42
        'Cecil\Step\Pages\Create',
43
        'Cecil\Step\Pages\Convert',
44
        'Cecil\Step\Taxonomies\Create',
45
        'Cecil\Step\Pages\Generate',
46
        'Cecil\Step\Menus\Create',
47
        'Cecil\Step\StaticFiles\Copy',
48
        'Cecil\Step\Pages\Render',
49
        'Cecil\Step\Pages\Save',
50
        'Cecil\Step\Assets\Save',
51
        'Cecil\Step\Optimize\Html',
52
        'Cecil\Step\Optimize\Css',
53
        'Cecil\Step\Optimize\Js',
54
        'Cecil\Step\Optimize\Images',
55
    ];
56

57
    /** @var Config Configuration. */
58
    protected $config;
59

60
    /** @var LoggerInterface Logger. */
61
    protected $logger;
62

63
    /** @var bool Debug mode. */
64
    protected $debug = false;
65

66
    /** @var array Build options. */
67
    protected $options;
68

69
    /** @var Finder Content iterator. */
70
    protected $content;
71

72
    /** @var array Data collection. */
73
    protected $data = [];
74

75
    /** @var array Static files collection. */
76
    protected $static = [];
77

78
    /** @var PagesCollection Pages collection. */
79
    protected $pages;
80

81
    /** @var array Assets path collection */
82
    protected $assets = [];
83

84
    /** @var array Menus collection. */
85
    protected $menus;
86

87
    /** @var array Taxonomies collection. */
88
    protected $taxonomies;
89

90
    /** @var Renderer\RendererInterface Renderer. */
91
    protected $renderer;
92

93
    /** @var GeneratorManager Generators manager. */
94
    protected $generatorManager;
95

96
    /** @var string Application version. */
97
    protected static $version;
98

99
    /** @var array Build metrics. */
100
    protected $metrics = [];
101

102
    /** @var string curent build ID */
103
    protected $buildId;
104

105
    /**
106
     * @param Config|array|null    $config
107
     * @param LoggerInterface|null $logger
108
     */
109
    public function __construct($config = null, ?LoggerInterface $logger = null)
110
    {
111
        // init and set config
112
        $this->config = new Config();
1✔
113
        if ($config !== null) {
1✔
114
            $this->setConfig($config);
1✔
115
        }
116
        // debug mode?
117
        if (getenv('CECIL_DEBUG') == 'true' || $this->getConfig()->isEnabled('debug')) {
1✔
118
            $this->debug = true;
1✔
119
        }
120
        // set logger
121
        if ($logger === null) {
1✔
122
            $logger = new PrintLogger(self::VERBOSITY_VERBOSE);
×
123
        }
124
        $this->setLogger($logger);
1✔
125
    }
126

127
    /**
128
     * Creates a new Builder instance.
129
     */
130
    public static function create(): self
131
    {
132
        $class = new \ReflectionClass(\get_called_class());
1✔
133

134
        return $class->newInstanceArgs(\func_get_args());
1✔
135
    }
136

137
    /**
138
     * Builds a new website.
139
     */
140
    public function build(array $options): self
141
    {
142
        // set start script time and memory usage
143
        $startTime = microtime(true);
1✔
144
        $startMemory = memory_get_usage();
1✔
145

146
        // log warnings
147
        $this->logBuildWarnings();
1✔
148

149
        // prepare options
150
        $this->options = array_merge([
1✔
151
            'drafts'  => false, // build drafts or not
1✔
152
            'dry-run' => false, // if dry-run is true, generated files are not saved
1✔
153
            'page'    => '',    // specific page to build
1✔
154
        ], $options);
1✔
155

156
        // set build ID
157
        $this->buildId = date('YmdHis');
1✔
158

159
        // process each step
160
        $steps = [];
1✔
161
        // init...
162
        foreach ($this->steps as $step) {
1✔
163
            /** @var Step\StepInterface $stepObject */
164
            $stepObject = new $step($this);
1✔
165
            $stepObject->init($this->options);
1✔
166
            if ($stepObject->canProcess()) {
1✔
167
                $steps[] = $stepObject;
1✔
168
            }
169
        }
170
        // ...and process!
171
        $stepNumber = 0;
1✔
172
        $stepsTotal = \count($steps);
1✔
173
        foreach ($steps as $step) {
1✔
174
            $stepNumber++;
1✔
175
            /** @var Step\StepInterface $step */
176
            $this->getLogger()->notice($step->getName(), ['step' => [$stepNumber, $stepsTotal]]);
1✔
177
            $stepStartTime = microtime(true);
1✔
178
            $stepStartMemory = memory_get_usage();
1✔
179
            $step->process();
1✔
180
            // step duration and memory usage
181
            $this->metrics['steps'][$stepNumber]['name'] = $step->getName();
1✔
182
            $this->metrics['steps'][$stepNumber]['duration'] = Util::convertMicrotime((float) $stepStartTime);
1✔
183
            $this->metrics['steps'][$stepNumber]['memory']   = Util::convertMemory(memory_get_usage() - $stepStartMemory);
1✔
184
            $this->getLogger()->info(\sprintf(
1✔
185
                '%s done in %s (%s)',
1✔
186
                $this->metrics['steps'][$stepNumber]['name'],
1✔
187
                $this->metrics['steps'][$stepNumber]['duration'],
1✔
188
                $this->metrics['steps'][$stepNumber]['memory']
1✔
189
            ));
1✔
190
        }
191
        // build duration and memory usage
192
        $this->metrics['total']['duration'] = Util::convertMicrotime($startTime);
1✔
193
        $this->metrics['total']['memory']   = Util::convertMemory(memory_get_usage() - $startMemory);
1✔
194
        $this->getLogger()->notice(\sprintf('Built in %s (%s)', $this->metrics['total']['duration'], $this->metrics['total']['memory']));
1✔
195

196
        return $this;
1✔
197
    }
198

199
    /**
200
     * Returns current build ID.
201
     */
202
    public function getBuilId(): string
203
    {
204
        return $this->buildId;
1✔
205
    }
206

207
    /**
208
     * Set configuration.
209
     */
210
    public function setConfig(array|Config $config): self
211
    {
212
        if (\is_array($config)) {
1✔
213
            $config = new Config($config);
1✔
214
        }
215
        if ($this->config !== $config) {
1✔
216
            $this->config = $config;
1✔
217
        }
218

219
        // import themes configuration
220
        $this->importThemesConfig();
1✔
221
        // autoloads local extensions
222
        Util::autoload($this, 'extensions');
1✔
223

224
        return $this;
1✔
225
    }
226

227
    /**
228
     * Returns configuration.
229
     */
230
    public function getConfig(): Config
231
    {
232
        if ($this->config === null) {
1✔
NEW
233
            $this->config = new Config();
×
234
        }
235

236
        return $this->config;
1✔
237
    }
238

239
    /**
240
     * Config::setSourceDir() alias.
241
     */
242
    public function setSourceDir(string $sourceDir): self
243
    {
244
        $this->getConfig()->setSourceDir($sourceDir);
1✔
245
        // import themes configuration
246
        $this->importThemesConfig();
1✔
247

248
        return $this;
1✔
249
    }
250

251
    /**
252
     * Config::setDestinationDir() alias.
253
     */
254
    public function setDestinationDir(string $destinationDir): self
255
    {
256
        $this->getConfig()->setDestinationDir($destinationDir);
1✔
257

258
        return $this;
1✔
259
    }
260

261
    /**
262
     * Import themes configuration.
263
     */
264
    public function importThemesConfig(): void
265
    {
266
        foreach ($this->config->get('theme') as $theme) {
1✔
267
            $this->config->import(Config::loadFile(Util::joinFile($this->config->getThemesPath(), $theme, 'config.yml'), true), Config::PRESERVE);
1✔
268
        }
269
    }
270

271
    /**
272
     * {@inheritdoc}
273
     */
274
    public function setLogger(LoggerInterface $logger): void
275
    {
276
        $this->logger = $logger;
1✔
277
    }
278

279
    /**
280
     * Returns the logger instance.
281
     */
282
    public function getLogger(): LoggerInterface
283
    {
284
        return $this->logger;
1✔
285
    }
286

287
    /**
288
     * Returns debug mode state.
289
     */
290
    public function isDebug(): bool
291
    {
292
        return (bool) $this->debug;
1✔
293
    }
294

295
    /**
296
     * Returns build options.
297
     */
298
    public function getBuildOptions(): array
299
    {
300
        return $this->options;
1✔
301
    }
302

303
    /**
304
     * Set collected pages files.
305
     */
306
    public function setPagesFiles(Finder $content): void
307
    {
308
        $this->content = $content;
1✔
309
    }
310

311
    /**
312
     * Returns pages files.
313
     */
314
    public function getPagesFiles(): ?Finder
315
    {
316
        return $this->content;
1✔
317
    }
318

319
    /**
320
     * Set collected data.
321
     */
322
    public function setData(array $data): void
323
    {
324
        $this->data = $data;
1✔
325
    }
326

327
    /**
328
     * Returns data collection.
329
     */
330
    public function getData(?string $language = null): ?array
331
    {
332
        if ($language) {
1✔
333
            if (empty($this->data[$language])) {
1✔
334
                // fallback to default language
335
                return $this->data[$this->config->getLanguageDefault()];
1✔
336
            }
337

338
            return $this->data[$language];
1✔
339
        }
340

341
        return $this->data;
1✔
342
    }
343

344
    /**
345
     * Set collected static files.
346
     */
347
    public function setStatic(array $static): void
348
    {
349
        $this->static = $static;
1✔
350
    }
351

352
    /**
353
     * Returns static files collection.
354
     */
355
    public function getStatic(): array
356
    {
357
        return $this->static;
1✔
358
    }
359

360
    /**
361
     * Set/update Pages collection.
362
     */
363
    public function setPages(PagesCollection $pages): void
364
    {
365
        $this->pages = $pages;
1✔
366
    }
367

368
    /**
369
     * Returns pages collection.
370
     */
371
    public function getPages(): ?PagesCollection
372
    {
373
        return $this->pages;
1✔
374
    }
375

376
    /**
377
     * Set assets path list.
378
     */
379
    public function setAssets(array $assets): void
380
    {
381
        $this->assets = $assets;
×
382
    }
383

384
    /**
385
     * Add an asset path to assets list.
386
     */
387
    public function addAsset(string $path): void
388
    {
389
        if (!\in_array($path, $this->assets, true)) {
1✔
390
            $this->assets[] = $path;
1✔
391
        }
392
    }
393

394
    /**
395
     * Returns list of assets path.
396
     */
397
    public function getAssets(): ?array
398
    {
399
        return $this->assets;
1✔
400
    }
401

402
    /**
403
     * Set menus collection.
404
     */
405
    public function setMenus(array $menus): void
406
    {
407
        $this->menus = $menus;
1✔
408
    }
409

410
    /**
411
     * Returns all menus, for a language.
412
     */
413
    public function getMenus(string $language): Collection\Menu\Collection
414
    {
415
        return $this->menus[$language];
1✔
416
    }
417

418
    /**
419
     * Set taxonomies collection.
420
     */
421
    public function setTaxonomies(array $taxonomies): void
422
    {
423
        $this->taxonomies = $taxonomies;
1✔
424
    }
425

426
    /**
427
     * Returns taxonomies collection, for a language.
428
     */
429
    public function getTaxonomies(string $language): ?Collection\Taxonomy\Collection
430
    {
431
        return $this->taxonomies[$language];
1✔
432
    }
433

434
    /**
435
     * Set renderer object.
436
     */
437
    public function setRenderer(Renderer\RendererInterface $renderer): void
438
    {
439
        $this->renderer = $renderer;
1✔
440
    }
441

442
    /**
443
     * Returns Renderer object.
444
     */
445
    public function getRenderer(): Renderer\RendererInterface
446
    {
447
        return $this->renderer;
1✔
448
    }
449

450
    /**
451
     * Returns metrics array.
452
     */
453
    public function getMetrics(): array
454
    {
455
        return $this->metrics;
×
456
    }
457

458
    /**
459
     * Returns application version.
460
     *
461
     * @throws RuntimeException
462
     */
463
    public static function getVersion(): string
464
    {
465
        if (!isset(self::$version)) {
1✔
466
            try {
467
                $filePath = Util\File::getRealPath('VERSION');
1✔
468
                $version = Util\File::fileGetContents($filePath);
×
469
                if ($version === false) {
×
470
                    throw new RuntimeException(\sprintf('Can\'t read content of "%s".', $filePath));
×
471
                }
472
                self::$version = trim($version);
×
473
            } catch (\Exception) {
1✔
474
                self::$version = self::VERSION;
1✔
475
            }
476
        }
477

478
        return self::$version;
1✔
479
    }
480

481
    /**
482
     * Log build warnings.
483
     */
484
    protected function logBuildWarnings(): void
485
    {
486
        // baseurl
487
        if (empty(trim((string) $this->config->get('baseurl'), '/'))) {
1✔
488
            $this->getLogger()->warning('`baseurl` configuration key is required in production.');
×
489
        }
490
    }
491
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc