| Total Complexity | 51 | 
| Total Lines | 557 | 
| Duplicated Lines | 0 % | 
| Changes | 5 | ||
| Bugs | 1 | Features | 0 | 
Complex classes like Builder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Builder, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 40 | class Builder implements LoggerAwareInterface | ||
| 41 | { | ||
| 42 | public const VERSION = '8.x-dev'; | ||
| 43 | public const VERBOSITY_QUIET = -1; | ||
| 44 | public const VERBOSITY_NORMAL = 0; | ||
| 45 | public const VERBOSITY_VERBOSE = 1; | ||
| 46 | public const VERBOSITY_DEBUG = 2; | ||
| 47 | /** | ||
| 48 | * Default options for the build process. | ||
| 49 | * These options can be overridden when calling the build() method. | ||
| 50 | * - 'drafts': if true, builds drafts too (default: false) | ||
| 51 | * - 'dry-run': if true, generated files are not saved (default: false) | ||
| 52 | * - 'page': if specified, only this page is processed (default: '') | ||
| 53 | * - 'render-subset': limits the render step to a specific subset (default: '') | ||
| 54 | * @var array<string, bool|string> | ||
| 55 | * @see \Cecil\Builder::build() | ||
| 56 | */ | ||
| 57 | public const OPTIONS = [ | ||
| 58 | 'drafts' => false, | ||
| 59 | 'dry-run' => false, | ||
| 60 | 'page' => '', | ||
| 61 | 'render-subset' => '', | ||
| 62 | ]; | ||
| 63 | /** | ||
| 64 | * Steps processed by build(), in order. | ||
| 65 | * These steps are executed sequentially to build the website. | ||
| 66 | * Each step is a class that implements the StepInterface. | ||
| 67 | * @var array<string> | ||
| 68 | * @see \Cecil\Step\StepInterface | ||
| 69 | */ | ||
| 70 | public const STEPS = [ | ||
| 71 | 'Cecil\Step\Pages\Load', | ||
| 72 | 'Cecil\Step\Data\Load', | ||
| 73 | 'Cecil\Step\StaticFiles\Load', | ||
| 74 | 'Cecil\Step\Pages\Create', | ||
| 75 | 'Cecil\Step\Pages\Convert', | ||
| 76 | 'Cecil\Step\Taxonomies\Create', | ||
| 77 | 'Cecil\Step\Pages\Generate', | ||
| 78 | 'Cecil\Step\Menus\Create', | ||
| 79 | 'Cecil\Step\StaticFiles\Copy', | ||
| 80 | 'Cecil\Step\Pages\Render', | ||
| 81 | 'Cecil\Step\Pages\Save', | ||
| 82 | 'Cecil\Step\Assets\Save', | ||
| 83 | 'Cecil\Step\Optimize\Html', | ||
| 84 | 'Cecil\Step\Optimize\Css', | ||
| 85 | 'Cecil\Step\Optimize\Js', | ||
| 86 | 'Cecil\Step\Optimize\Images', | ||
| 87 | ]; | ||
| 88 | |||
| 89 | /** | ||
| 90 | * Configuration object. | ||
| 91 | * This object holds all the configuration settings for the build process. | ||
| 92 | * It can be set to an array or a Config instance. | ||
| 93 | * @var Config|array|null | ||
| 94 | * @see \Cecil\Config | ||
| 95 | */ | ||
| 96 | protected $config; | ||
| 97 | /** | ||
| 98 | * Logger instance. | ||
| 99 | * This logger is used to log messages during the build process. | ||
| 100 | * It can be set to any PSR-3 compliant logger. | ||
| 101 | * @var LoggerInterface | ||
| 102 | * @see \Psr\Log\LoggerInterface | ||
| 103 | * */ | ||
| 104 | protected $logger; | ||
| 105 | /** | ||
| 106 | * Debug mode state. | ||
| 107 | * If true, debug messages are logged. | ||
| 108 | * @var bool | ||
| 109 | */ | ||
| 110 | protected $debug = false; | ||
| 111 | /** | ||
| 112 | * Build options. | ||
| 113 | * These options can be passed to the build() method to customize the build process. | ||
| 114 | * @var array | ||
| 115 | * @see \Cecil\Builder::OPTIONS | ||
| 116 | * @see \Cecil\Builder::build() | ||
| 117 | */ | ||
| 118 | protected $options = []; | ||
| 119 | /** | ||
| 120 | * Content files collection. | ||
| 121 | * This is a Finder instance that collects all the content files (pages, posts, etc.) from the source directory. | ||
| 122 | * @var Finder | ||
| 123 | */ | ||
| 124 | protected $content; | ||
| 125 | /** | ||
| 126 | * Data collection. | ||
| 127 | * This is an associative array that holds data loaded from YAML files in the data directory. | ||
| 128 | * @var array | ||
| 129 | */ | ||
| 130 | protected $data = []; | ||
| 131 | /** | ||
| 132 | * Static files collection. | ||
| 133 | * This is an associative array that holds static files (like images, CSS, JS) that are copied to the destination directory. | ||
| 134 | * @var array | ||
| 135 | */ | ||
| 136 | protected $static = []; | ||
| 137 | /** | ||
| 138 | * Pages collection. | ||
| 139 | * This is a collection of pages that have been processed and are ready for rendering. | ||
| 140 | * It is an instance of PagesCollection, which is a custom collection class for managing pages. | ||
| 141 | * @var PagesCollection | ||
| 142 | */ | ||
| 143 | protected $pages; | ||
| 144 | /** | ||
| 145 | * Assets path collection. | ||
| 146 | * This is an array that holds paths to assets (like CSS, JS, images) that are used in the build process. | ||
| 147 | * It is used to keep track of assets that need to be processed or copied. | ||
| 148 | * It can be set to an array of paths or updated with new asset paths. | ||
| 149 | * @var array | ||
| 150 | */ | ||
| 151 | protected $assets = []; | ||
| 152 | /** | ||
| 153 | * Menus collection. | ||
| 154 | * This is an associative array that holds menus for different languages. | ||
| 155 | * Each key is a language code, and the value is a Collection\Menu\Collection instance | ||
| 156 | * that contains the menu items for that language. | ||
| 157 | * It is used to manage navigation menus across different languages in the website. | ||
| 158 | * @var array | ||
| 159 | * @see \Cecil\Collection\Menu\Collection | ||
| 160 | */ | ||
| 161 | protected $menus; | ||
| 162 | /** | ||
| 163 | * Taxonomies collection. | ||
| 164 | * This is an associative array that holds taxonomies for different languages. | ||
| 165 | * Each key is a language code, and the value is a Collection\Taxonomy\Collection instance | ||
| 166 | * that contains the taxonomy terms for that language. | ||
| 167 | * It is used to manage taxonomies (like categories, tags) across different languages in the website. | ||
| 168 | * @var array | ||
| 169 | * @see \Cecil\Collection\Taxonomy\Collection | ||
| 170 | */ | ||
| 171 | protected $taxonomies; | ||
| 172 | /** | ||
| 173 | * Renderer. | ||
| 174 | * This is an instance of Renderer\Twig that is responsible for rendering pages. | ||
| 175 | * It handles the rendering of templates and the application of data to those templates. | ||
| 176 | * @var Renderer\Twig | ||
| 177 | */ | ||
| 178 | protected $renderer; | ||
| 179 | /** | ||
| 180 | * Generators manager. | ||
| 181 | * This is an instance of GeneratorManager that manages all the generators used in the build process. | ||
| 182 | * Generators are used to create dynamic content or perform specific tasks during the build. | ||
| 183 | * It allows for the registration and execution of various generators that can extend the functionality of the build process. | ||
| 184 | * @var GeneratorManager | ||
| 185 | */ | ||
| 186 | protected $generatorManager; | ||
| 187 | /** | ||
| 188 | * Application version. | ||
| 189 | * @var string | ||
| 190 | */ | ||
| 191 | protected static $version; | ||
| 192 | /** | ||
| 193 | * Build metrics. | ||
| 194 | * This array holds metrics about the build process, such as duration and memory usage for each step. | ||
| 195 | * It is used to track the performance of the build and can be useful for debugging and optimization. | ||
| 196 | * @var array | ||
| 197 | */ | ||
| 198 | protected $metrics = []; | ||
| 199 | /** | ||
| 200 | * Current build ID. | ||
| 201 | * This is a unique identifier for the current build process. | ||
| 202 | * It is generated based on the current date and time when the build starts. | ||
| 203 | * It can be used to track builds, especially in environments where multiple builds may occur. | ||
| 204 | * @var string | ||
| 205 | * @see \Cecil\Builder::build() | ||
| 206 | */ | ||
| 207 | protected $buildId; | ||
| 208 | |||
| 209 | /** | ||
| 210 | * @param Config|array|null $config | ||
| 211 | * @param LoggerInterface|null $logger | ||
| 212 | */ | ||
| 213 | public function __construct($config = null, ?LoggerInterface $logger = null) | ||
| 214 |     { | ||
| 215 | // init and set config | ||
| 216 | $this->config = new Config(); | ||
| 217 |         if ($config !== null) { | ||
| 218 | $this->setConfig($config); | ||
| 219 | } | ||
| 220 | // debug mode? | ||
| 221 |         if (getenv('CECIL_DEBUG') == 'true' || $this->getConfig()->isEnabled('debug')) { | ||
| 222 | $this->debug = true; | ||
| 223 | } | ||
| 224 | // set logger | ||
| 225 |         if ($logger === null) { | ||
| 226 | $logger = new PrintLogger(self::VERBOSITY_VERBOSE); | ||
| 227 | } | ||
| 228 | $this->setLogger($logger); | ||
| 229 | } | ||
| 230 | |||
| 231 | /** | ||
| 232 | * Creates a new Builder instance. | ||
| 233 | */ | ||
| 234 | public static function create(): self | ||
| 235 |     { | ||
| 236 | $class = new \ReflectionClass(\get_called_class()); | ||
| 237 | |||
| 238 | return $class->newInstanceArgs(\func_get_args()); | ||
| 239 | } | ||
| 240 | |||
| 241 | /** | ||
| 242 | * Builds a new website. | ||
| 243 | * This method processes the build steps in order, collects content, data, static files, | ||
| 244 | * generates pages, renders them, and saves the output to the destination directory. | ||
| 245 | * It also collects metrics about the build process, such as duration and memory usage. | ||
| 246 | * @param array<self::OPTIONS> $options | ||
|  | |||
| 247 | * @see \Cecil\Builder::OPTIONS | ||
| 248 | */ | ||
| 249 | public function build(array $options): self | ||
| 250 |     { | ||
| 251 | // set start script time and memory usage | ||
| 252 | $startTime = microtime(true); | ||
| 253 | $startMemory = memory_get_usage(); | ||
| 254 | |||
| 255 | // checks soft errors | ||
| 256 | $this->checkErrors(); | ||
| 257 | |||
| 258 | // merge options with defaults | ||
| 259 | $this->options = array_merge(self::OPTIONS, $options); | ||
| 260 | |||
| 261 | // set build ID | ||
| 262 |         $this->buildId = date('YmdHis'); | ||
| 263 | |||
| 264 | // process each step | ||
| 265 | $steps = []; | ||
| 266 | // init... | ||
| 267 |         foreach (self::STEPS as $step) { | ||
| 268 | /** @var Step\StepInterface $stepObject */ | ||
| 269 | $stepObject = new $step($this); | ||
| 270 | $stepObject->init($this->options); | ||
| 271 |             if ($stepObject->canProcess()) { | ||
| 272 | $steps[] = $stepObject; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | // ...and process | ||
| 276 | $stepNumber = 0; | ||
| 277 | $stepsTotal = \count($steps); | ||
| 278 |         foreach ($steps as $step) { | ||
| 279 | $stepNumber++; | ||
| 280 | /** @var Step\StepInterface $step */ | ||
| 281 | $this->getLogger()->notice($step->getName(), ['step' => [$stepNumber, $stepsTotal]]); | ||
| 282 | $stepStartTime = microtime(true); | ||
| 283 | $stepStartMemory = memory_get_usage(); | ||
| 284 | $step->process(); | ||
| 285 | // step duration and memory usage | ||
| 286 | $this->metrics['steps'][$stepNumber]['name'] = $step->getName(); | ||
| 287 | $this->metrics['steps'][$stepNumber]['duration'] = Util::convertMicrotime(/** @scrutinizer ignore-type */ $stepStartTime); | ||
| 288 | $this->metrics['steps'][$stepNumber]['memory'] = Util::convertMemory(memory_get_usage() - $stepStartMemory); | ||
| 289 | $this->getLogger()->info(\sprintf( | ||
| 290 | '%s done in %s (%s)', | ||
| 291 | $this->metrics['steps'][$stepNumber]['name'], | ||
| 292 | $this->metrics['steps'][$stepNumber]['duration'], | ||
| 293 | $this->metrics['steps'][$stepNumber]['memory'] | ||
| 294 | )); | ||
| 295 | } | ||
| 296 | // build duration and memory usage | ||
| 297 | $this->metrics['total']['duration'] = Util::convertMicrotime(/** @scrutinizer ignore-type */ $startTime); | ||
| 298 | $this->metrics['total']['memory'] = Util::convertMemory(memory_get_usage() - $startMemory); | ||
| 299 |         $this->getLogger()->notice(\sprintf('Built in %s (%s)', $this->metrics['total']['duration'], $this->metrics['total']['memory'])); | ||
| 300 | |||
| 301 | return $this; | ||
| 302 | } | ||
| 303 | |||
| 304 | /** | ||
| 305 | * Returns current build ID. | ||
| 306 | */ | ||
| 307 | public function getBuildId(): string | ||
| 308 |     { | ||
| 309 | return $this->buildId; | ||
| 310 | } | ||
| 311 | |||
| 312 | /** | ||
| 313 | * Set configuration. | ||
| 314 | */ | ||
| 315 | public function setConfig(array|Config $config): self | ||
| 316 |     { | ||
| 317 |         if (\is_array($config)) { | ||
| 318 | $config = new Config($config); | ||
| 319 | } | ||
| 320 |         if ($this->config !== $config) { | ||
| 321 | $this->config = $config; | ||
| 322 | } | ||
| 323 | |||
| 324 | // import themes configuration | ||
| 325 | $this->importThemesConfig(); | ||
| 326 | // autoloads local extensions | ||
| 327 | Util::autoload($this, 'extensions'); | ||
| 328 | |||
| 329 | return $this; | ||
| 330 | } | ||
| 331 | |||
| 332 | /** | ||
| 333 | * Returns configuration. | ||
| 334 | */ | ||
| 335 | public function getConfig(): Config | ||
| 342 | } | ||
| 343 | |||
| 344 | /** | ||
| 345 | * Config::setSourceDir() alias. | ||
| 346 | */ | ||
| 347 | public function setSourceDir(string $sourceDir): self | ||
| 348 |     { | ||
| 349 | $this->getConfig()->setSourceDir($sourceDir); | ||
| 350 | // import themes configuration | ||
| 351 | $this->importThemesConfig(); | ||
| 352 | |||
| 353 | return $this; | ||
| 354 | } | ||
| 355 | |||
| 356 | /** | ||
| 357 | * Config::setDestinationDir() alias. | ||
| 358 | */ | ||
| 359 | public function setDestinationDir(string $destinationDir): self | ||
| 360 |     { | ||
| 361 | $this->getConfig()->setDestinationDir($destinationDir); | ||
| 362 | |||
| 363 | return $this; | ||
| 364 | } | ||
| 365 | |||
| 366 | /** | ||
| 367 | * Import themes configuration. | ||
| 368 | */ | ||
| 369 | public function importThemesConfig(): void | ||
| 370 |     { | ||
| 371 |         foreach ((array) $this->getConfig()->get('theme') as $theme) { | ||
| 372 | $this->getConfig()->import( | ||
| 373 | Config::loadFile(Util::joinFile($this->getConfig()->getThemesPath(), $theme, 'config.yml'), true), | ||
| 374 | Config::IMPORT_PRESERVE | ||
| 375 | ); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | /** | ||
| 380 |      * {@inheritdoc} | ||
| 381 | */ | ||
| 382 | public function setLogger(LoggerInterface $logger): void | ||
| 383 |     { | ||
| 384 | $this->logger = $logger; | ||
| 385 | } | ||
| 386 | |||
| 387 | /** | ||
| 388 | * Returns the logger instance. | ||
| 389 | */ | ||
| 390 | public function getLogger(): LoggerInterface | ||
| 391 |     { | ||
| 392 | return $this->logger; | ||
| 393 | } | ||
| 394 | |||
| 395 | /** | ||
| 396 | * Returns debug mode state. | ||
| 397 | */ | ||
| 398 | public function isDebug(): bool | ||
| 399 |     { | ||
| 400 | return (bool) $this->debug; | ||
| 401 | } | ||
| 402 | |||
| 403 | /** | ||
| 404 | * Returns build options. | ||
| 405 | */ | ||
| 406 | public function getBuildOptions(): array | ||
| 407 |     { | ||
| 408 | return $this->options; | ||
| 409 | } | ||
| 410 | |||
| 411 | /** | ||
| 412 | * Set collected pages files. | ||
| 413 | */ | ||
| 414 | public function setPagesFiles(Finder $content): void | ||
| 415 |     { | ||
| 416 | $this->content = $content; | ||
| 417 | } | ||
| 418 | |||
| 419 | /** | ||
| 420 | * Returns pages files. | ||
| 421 | */ | ||
| 422 | public function getPagesFiles(): ?Finder | ||
| 423 |     { | ||
| 424 | return $this->content; | ||
| 425 | } | ||
| 426 | |||
| 427 | /** | ||
| 428 | * Set collected data. | ||
| 429 | */ | ||
| 430 | public function setData(array $data): void | ||
| 431 |     { | ||
| 432 | $this->data = $data; | ||
| 433 | } | ||
| 434 | |||
| 435 | /** | ||
| 436 | * Returns data collection. | ||
| 437 | */ | ||
| 438 | public function getData(?string $language = null): array | ||
| 439 |     { | ||
| 440 |         if ($language) { | ||
| 441 |             if (empty($this->data[$language])) { | ||
| 442 | // fallback to default language | ||
| 443 | return $this->data[$this->getConfig()->getLanguageDefault()]; | ||
| 444 | } | ||
| 445 | |||
| 446 | return $this->data[$language]; | ||
| 447 | } | ||
| 448 | |||
| 449 | return $this->data; | ||
| 450 | } | ||
| 451 | |||
| 452 | /** | ||
| 453 | * Set collected static files. | ||
| 454 | */ | ||
| 455 | public function setStatic(array $static): void | ||
| 456 |     { | ||
| 457 | $this->static = $static; | ||
| 458 | } | ||
| 459 | |||
| 460 | /** | ||
| 461 | * Returns static files collection. | ||
| 462 | */ | ||
| 463 | public function getStatic(): array | ||
| 464 |     { | ||
| 465 | return $this->static; | ||
| 466 | } | ||
| 467 | |||
| 468 | /** | ||
| 469 | * Set/update Pages collection. | ||
| 470 | */ | ||
| 471 | public function setPages(PagesCollection $pages): void | ||
| 472 |     { | ||
| 473 | $this->pages = $pages; | ||
| 474 | } | ||
| 475 | |||
| 476 | /** | ||
| 477 | * Returns pages collection. | ||
| 478 | */ | ||
| 479 | public function getPages(): ?PagesCollection | ||
| 480 |     { | ||
| 481 | return $this->pages; | ||
| 482 | } | ||
| 483 | |||
| 484 | /** | ||
| 485 | * Set assets path list. | ||
| 486 | */ | ||
| 487 | public function setAssets(array $assets): void | ||
| 488 |     { | ||
| 489 | $this->assets = $assets; | ||
| 490 | } | ||
| 491 | |||
| 492 | /** | ||
| 493 | * Add an asset path to assets list. | ||
| 494 | */ | ||
| 495 | public function addAsset(string $path): void | ||
| 496 |     { | ||
| 497 |         if (!\in_array($path, $this->assets, true)) { | ||
| 498 | $this->assets[] = $path; | ||
| 499 | } | ||
| 500 | } | ||
| 501 | |||
| 502 | /** | ||
| 503 | * Returns list of assets path. | ||
| 504 | */ | ||
| 505 | public function getAssets(): array | ||
| 506 |     { | ||
| 507 | return $this->assets; | ||
| 508 | } | ||
| 509 | |||
| 510 | /** | ||
| 511 | * Set menus collection. | ||
| 512 | */ | ||
| 513 | public function setMenus(array $menus): void | ||
| 516 | } | ||
| 517 | |||
| 518 | /** | ||
| 519 | * Returns all menus, for a language. | ||
| 520 | */ | ||
| 521 | public function getMenus(string $language): Collection\Menu\Collection | ||
| 522 |     { | ||
| 523 | return $this->menus[$language]; | ||
| 524 | } | ||
| 525 | |||
| 526 | /** | ||
| 527 | * Set taxonomies collection. | ||
| 528 | */ | ||
| 529 | public function setTaxonomies(array $taxonomies): void | ||
| 530 |     { | ||
| 531 | $this->taxonomies = $taxonomies; | ||
| 532 | } | ||
| 533 | |||
| 534 | /** | ||
| 535 | * Returns taxonomies collection, for a language. | ||
| 536 | */ | ||
| 537 | public function getTaxonomies(string $language): ?Collection\Taxonomy\Collection | ||
| 538 |     { | ||
| 539 | return $this->taxonomies[$language]; | ||
| 540 | } | ||
| 541 | |||
| 542 | /** | ||
| 543 | * Set renderer object. | ||
| 544 | */ | ||
| 545 | public function setRenderer(Renderer\Twig $renderer): void | ||
| 546 |     { | ||
| 547 | $this->renderer = $renderer; | ||
| 548 | } | ||
| 549 | |||
| 550 | /** | ||
| 551 | * Returns Renderer object. | ||
| 552 | */ | ||
| 553 | public function getRenderer(): Renderer\Twig | ||
| 556 | } | ||
| 557 | |||
| 558 | /** | ||
| 559 | * Returns metrics array. | ||
| 560 | */ | ||
| 561 | public function getMetrics(): array | ||
| 562 |     { | ||
| 563 | return $this->metrics; | ||
| 564 | } | ||
| 565 | |||
| 566 | /** | ||
| 567 | * Returns application version. | ||
| 568 | * | ||
| 569 | * @throws RuntimeException | ||
| 570 | */ | ||
| 571 | public static function getVersion(): string | ||
| 587 | } | ||
| 588 | |||
| 589 | /** | ||
| 590 | * Log soft errors. | ||
| 591 | */ | ||
| 592 | protected function checkErrors(): void | ||
| 597 | } | ||
| 598 | } | ||
| 599 | } | ||
| 600 |