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

Cecilapp / Cecil / 5046064611

pending completion
5046064611

push

github

GitHub
perf: native_function_invocation (#1697)

322 of 322 new or added lines in 62 files covered. (100.0%)

2784 of 4121 relevant lines covered (67.56%)

0.68 hits per line

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

87.65
/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 Cecil\Util\Plateform;
21
use Psr\Log\LoggerAwareInterface;
22
use Psr\Log\LoggerInterface;
23
use Symfony\Component\Finder\Finder;
24

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

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

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

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

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

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

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

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

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

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

82
    /** @var array Menus collection. */
83
    protected $menus;
84

85
    /** @var Collection\Taxonomy\Collection Taxonomies collection. */
86
    protected $taxonomies;
87

88
    /** @var Renderer\RendererInterface Renderer. */
89
    protected $renderer;
90

91
    /** @var GeneratorManager Generators manager. */
92
    protected $generatorManager;
93

94
    /** @var string Application version. */
95
    protected static $version;
96

97
    /**
98
     * @param Config|array|null    $config
99
     * @param LoggerInterface|null $logger
100
     */
101
    public function __construct($config = null, LoggerInterface $logger = null)
102
    {
103
        // set logger
104
        if ($logger === null) {
1✔
105
            $logger = new PrintLogger(self::VERBOSITY_VERBOSE);
×
106
        }
107
        $this->setLogger($logger);
1✔
108
        // set config
109
        $this->setConfig($config)->setSourceDir(null)->setDestinationDir(null);
1✔
110
        // debug mode?
111
        if (getenv('CECIL_DEBUG') == 'true' || (bool) $this->getConfig()->get('debug')) {
1✔
112
            $this->debug = true;
1✔
113
        }
114
    }
115

116
    /**
117
     * Creates a new Builder instance.
118
     */
119
    public static function create(): self
120
    {
121
        $class = new \ReflectionClass(\get_called_class());
1✔
122

123
        return $class->newInstanceArgs(\func_get_args());
1✔
124
    }
125

126
    /**
127
     * Builds a new website.
128
     */
129
    public function build(array $options): self
130
    {
131
        // set start script time and memory usage
132
        $startTime = microtime(true);
1✔
133
        $startMemory = memory_get_usage();
1✔
134

135
        // checks the configuration
136
        $this->validConfig();
1✔
137

138
        // prepare options
139
        $this->options = array_merge([
1✔
140
            'drafts'  => false, // build drafts or not
1✔
141
            'dry-run' => false, // if dry-run is true, generated files are not saved
1✔
142
            'page'    => '',    // specific page to build
1✔
143
        ], $options);
1✔
144

145
        // process each step
146
        $steps = [];
1✔
147
        // init...
148
        foreach ($this->steps as $step) {
1✔
149
            /** @var Step\StepInterface $stepObject */
150
            $stepObject = new $step($this);
1✔
151
            $stepObject->init($this->options);
1✔
152
            if ($stepObject->canProcess()) {
1✔
153
                $steps[] = $stepObject;
1✔
154
            }
155
        }
156
        // ...and process!
157
        $stepNumber = 0;
1✔
158
        $stepsTotal = \count($steps);
1✔
159
        foreach ($steps as $step) {
1✔
160
            $stepNumber++;
1✔
161
            /** @var Step\StepInterface $step */
162
            $this->getLogger()->notice($step->getName(), ['step' => [$stepNumber, $stepsTotal]]);
1✔
163
            $step->process();
1✔
164
        }
165

166
        // process duration
167
        $message = sprintf('Built in %s s (%s)', round(microtime(true) - $startTime, 2), Util::convertMemory(memory_get_usage() - $startMemory));
1✔
168
        $this->getLogger()->notice($message);
1✔
169

170
        return $this;
1✔
171
    }
172

173
    /**
174
     * Set configuration.
175
     *
176
     * @param Config|array|null $config
177
     */
178
    public function setConfig($config): self
179
    {
180
        if (!$config instanceof Config) {
1✔
181
            $config = new Config($config);
1✔
182
        }
183
        if ($this->config !== $config) {
1✔
184
            $this->config = $config;
1✔
185
        }
186

187
        return $this;
1✔
188
    }
189

190
    /**
191
     * Returns configuration.
192
     */
193
    public function getConfig(): Config
194
    {
195
        return $this->config;
1✔
196
    }
197

198
    /**
199
     * Config::setSourceDir() alias.
200
     */
201
    public function setSourceDir(string $sourceDir = null): self
202
    {
203
        $this->config->setSourceDir($sourceDir);
1✔
204

205
        return $this;
1✔
206
    }
207

208
    /**
209
     * Config::setDestinationDir() alias.
210
     */
211
    public function setDestinationDir(string $destinationDir = null): self
212
    {
213
        $this->config->setDestinationDir($destinationDir);
1✔
214

215
        return $this;
1✔
216
    }
217

218
    /**
219
     * {@inheritdoc}
220
     */
221
    public function setLogger(LoggerInterface $logger)
222
    {
223
        $this->logger = $logger;
1✔
224
    }
225

226
    /**
227
     * Returns the logger instance.
228
     */
229
    public function getLogger(): LoggerInterface
230
    {
231
        return $this->logger;
1✔
232
    }
233

234
    /**
235
     * Returns debug mode state.
236
     */
237
    public function isDebug(): bool
238
    {
239
        return $this->debug;
1✔
240
    }
241

242
    /**
243
     * Returns build options.
244
     */
245
    public function getBuildOptions(): array
246
    {
247
        return $this->options;
1✔
248
    }
249

250
    /**
251
     * Set collected pages files.
252
     */
253
    public function setPagesFiles(Finder $content): void
254
    {
255
        $this->content = $content;
1✔
256
    }
257

258
    /**
259
     * Returns pages files.
260
     */
261
    public function getPagesFiles(): ?Finder
262
    {
263
        return $this->content;
1✔
264
    }
265

266
    /**
267
     * Set collected data.
268
     */
269
    public function setData(array $data): void
270
    {
271
        $this->data = $data;
1✔
272
    }
273

274
    /**
275
     * Returns data collection.
276
     */
277
    public function getData(): array
278
    {
279
        return $this->data;
1✔
280
    }
281

282
    /**
283
     * Set collected static files.
284
     */
285
    public function setStatic(array $static): void
286
    {
287
        $this->static = $static;
1✔
288
    }
289

290
    /**
291
     * Returns static files collection.
292
     */
293
    public function getStatic(): array
294
    {
295
        return $this->static;
1✔
296
    }
297

298
    /**
299
     * Set/update Pages collection.
300
     */
301
    public function setPages(PagesCollection $pages): void
302
    {
303
        $this->pages = $pages;
1✔
304
    }
305

306
    /**
307
     * Returns pages collection.
308
     */
309
    public function getPages(): ?PagesCollection
310
    {
311
        return $this->pages;
1✔
312
    }
313

314
    /**
315
     * Set menus collection.
316
     */
317
    public function setMenus(array $menus): void
318
    {
319
        $this->menus = $menus;
1✔
320
    }
321

322
    /**
323
     * Returns all menus, for a language.
324
     */
325
    public function getMenus(string $language): Collection\Menu\Collection
326
    {
327
        return $this->menus[$language];
1✔
328
    }
329

330
    /**
331
     * Set taxonomies collection.
332
     */
333
    public function setTaxonomies(Collection\Taxonomy\Collection $taxonomies): void
334
    {
335
        $this->taxonomies = $taxonomies;
1✔
336
    }
337

338
    /**
339
     * Returns taxonomies collection.
340
     */
341
    public function getTaxonomies(): ?Collection\Taxonomy\Collection
342
    {
343
        return $this->taxonomies;
1✔
344
    }
345

346
    /**
347
     * Set renderer object.
348
     */
349
    public function setRenderer(Renderer\RendererInterface $renderer): void
350
    {
351
        $this->renderer = $renderer;
1✔
352
    }
353

354
    /**
355
     * Returns Renderer object.
356
     */
357
    public function getRenderer(): Renderer\RendererInterface
358
    {
359
        return $this->renderer;
1✔
360
    }
361

362
    /**
363
     * Returns application version.
364
     *
365
     * @throws RuntimeException
366
     */
367
    public static function getVersion(): string
368
    {
369
        if (!isset(self::$version)) {
1✔
370
            $filePath = __DIR__ . '/../VERSION';
1✔
371
            if (Plateform::isPhar()) {
1✔
372
                $filePath = Plateform::getPharPath() . '/VERSION';
×
373
            }
374

375
            try {
376
                if (!file_exists($filePath)) {
1✔
377
                    throw new RuntimeException(sprintf('%s file doesn\'t exist!', $filePath));
1✔
378
                }
379
                $version = Util\File::fileGetContents($filePath);
×
380
                if ($version === false) {
×
381
                    throw new RuntimeException(sprintf('Can\'t get %s file!', $filePath));
×
382
                }
383
                self::$version = trim($version);
×
384
            } catch (\Exception $e) {
1✔
385
                self::$version = self::VERSION;
1✔
386
            }
387
        }
388

389
        return self::$version;
1✔
390
    }
391

392
    /**
393
     * Checks the configuration.
394
     */
395
    protected function validConfig(): void
396
    {
397
        // baseurl
398
        if (empty(trim((string) $this->config->get('baseurl'), '/'))) {
1✔
399
            $this->getLogger()->error('Config: `baseurl` is required in production (e.g.: "baseurl: https://example.com/").');
×
400
        }
401
        // default language
402
        if (!preg_match('/^' . Config::LANG_CODE_PATTERN . '$/', (string) $this->config->get('language'))) {
1✔
403
            throw new RuntimeException(sprintf('Config: default language code "%s" is not valid (e.g.: "language: fr-FR").', $this->config->get('language')));
×
404
        }
405
        // locales
406
        foreach ((array) $this->config->get('languages') as $lang) {
1✔
407
            if (!isset($lang['locale'])) {
1✔
408
                throw new RuntimeException('Config: a language locale is not defined.');
×
409
            }
410
            if (!preg_match('/^' . Config::LANG_LOCALE_PATTERN . '$/', $lang['locale'])) {
1✔
411
                throw new RuntimeException(sprintf('Config: the language locale "%s" is not valid (e.g.: "locale: fr_FR").', $lang['locale']));
×
412
            }
413
        }
414
    }
415
}
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