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