| Total Complexity | 43 |
| Total Lines | 385 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 28 | class Builder implements LoggerAwareInterface |
||
| 29 | { |
||
| 30 | public const VERSION = '8.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\Optimize\Html', |
||
| 53 | 'Cecil\Step\Optimize\Css', |
||
| 54 | 'Cecil\Step\Optimize\Js', |
||
| 55 | 'Cecil\Step\Optimize\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 array 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) { |
||
| 105 | $logger = new PrintLogger(self::VERBOSITY_VERBOSE); |
||
| 106 | } |
||
| 107 | $this->setLogger($logger); |
||
| 108 | // set config |
||
| 109 | $this->setConfig($config)->setSourceDir(null)->setDestinationDir(null); |
||
| 110 | // debug mode? |
||
| 111 | if (getenv('CECIL_DEBUG') == 'true' || (bool) $this->getConfig()->get('debug')) { |
||
| 112 | $this->debug = true; |
||
| 113 | } |
||
| 114 | // autoloads local extensions |
||
| 115 | Util::autoload($this, 'extensions'); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Creates a new Builder instance. |
||
| 120 | */ |
||
| 121 | public static function create(): self |
||
| 122 | { |
||
| 123 | $class = new \ReflectionClass(\get_called_class()); |
||
| 124 | |||
| 125 | return $class->newInstanceArgs(\func_get_args()); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Builds a new website. |
||
| 130 | */ |
||
| 131 | public function build(array $options): self |
||
| 132 | { |
||
| 133 | // set start script time and memory usage |
||
| 134 | $startTime = microtime(true); |
||
| 135 | $startMemory = memory_get_usage(); |
||
| 136 | |||
| 137 | // log configuration errors |
||
| 138 | $this->logConfigError(); |
||
| 139 | |||
| 140 | // prepare options |
||
| 141 | $this->options = array_merge([ |
||
| 142 | 'drafts' => false, // build drafts or not |
||
| 143 | 'dry-run' => false, // if dry-run is true, generated files are not saved |
||
| 144 | 'page' => '', // specific page to build |
||
| 145 | ], $options); |
||
| 146 | |||
| 147 | // process each step |
||
| 148 | $steps = []; |
||
| 149 | // init... |
||
| 150 | foreach ($this->steps as $step) { |
||
| 151 | /** @var Step\StepInterface $stepObject */ |
||
| 152 | $stepObject = new $step($this); |
||
| 153 | $stepObject->init($this->options); |
||
| 154 | if ($stepObject->canProcess()) { |
||
| 155 | $steps[] = $stepObject; |
||
| 156 | } |
||
| 157 | } |
||
| 158 | // ...and process! |
||
| 159 | $stepNumber = 0; |
||
| 160 | $stepsTotal = \count($steps); |
||
| 161 | foreach ($steps as $step) { |
||
| 162 | $stepNumber++; |
||
| 163 | /** @var Step\StepInterface $step */ |
||
| 164 | $this->getLogger()->notice($step->getName(), ['step' => [$stepNumber, $stepsTotal]]); |
||
| 165 | $stepStartTime = microtime(true); |
||
| 166 | $stepStartMemory = memory_get_usage(); |
||
| 167 | $step->process(); |
||
| 168 | $this->getLogger()->info(\sprintf('%s done in %s (%s)', $step->getName(), Util::convertMicrotime((float) $stepStartTime), Util::convertMemory(memory_get_usage() - $stepStartMemory))); |
||
| 169 | } |
||
| 170 | |||
| 171 | // process duration |
||
| 172 | $this->getLogger()->notice(\sprintf('Built in %s (%s)', Util::convertMicrotime($startTime), Util::convertMemory(memory_get_usage() - $startMemory))); |
||
|
|
|||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Set configuration. |
||
| 179 | * |
||
| 180 | * @param Config|array|null $config |
||
| 181 | */ |
||
| 182 | public function setConfig($config): self |
||
| 183 | { |
||
| 184 | if (!$config instanceof Config) { |
||
| 185 | $config = new Config($config); |
||
| 186 | } |
||
| 187 | if ($this->config !== $config) { |
||
| 188 | $this->config = $config; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns configuration. |
||
| 196 | */ |
||
| 197 | public function getConfig(): Config |
||
| 198 | { |
||
| 199 | return $this->config; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Config::setSourceDir() alias. |
||
| 204 | */ |
||
| 205 | public function setSourceDir(?string $sourceDir = null): self |
||
| 206 | { |
||
| 207 | $this->config->setSourceDir($sourceDir); |
||
| 208 | |||
| 209 | return $this; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Config::setDestinationDir() alias. |
||
| 214 | */ |
||
| 215 | public function setDestinationDir(?string $destinationDir = null): self |
||
| 216 | { |
||
| 217 | $this->config->setDestinationDir($destinationDir); |
||
| 218 | |||
| 219 | return $this; |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function setLogger(LoggerInterface $logger): void |
||
| 226 | { |
||
| 227 | $this->logger = $logger; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Returns the logger instance. |
||
| 232 | */ |
||
| 233 | public function getLogger(): LoggerInterface |
||
| 234 | { |
||
| 235 | return $this->logger; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Returns debug mode state. |
||
| 240 | */ |
||
| 241 | public function isDebug(): bool |
||
| 242 | { |
||
| 243 | return (bool) $this->debug; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns build options. |
||
| 248 | */ |
||
| 249 | public function getBuildOptions(): array |
||
| 250 | { |
||
| 251 | return $this->options; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set collected pages files. |
||
| 256 | */ |
||
| 257 | public function setPagesFiles(Finder $content): void |
||
| 258 | { |
||
| 259 | $this->content = $content; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Returns pages files. |
||
| 264 | */ |
||
| 265 | public function getPagesFiles(): ?Finder |
||
| 266 | { |
||
| 267 | return $this->content; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Set collected data. |
||
| 272 | */ |
||
| 273 | public function setData(array $data): void |
||
| 274 | { |
||
| 275 | $this->data = $data; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Returns data collection. |
||
| 280 | */ |
||
| 281 | public function getData(?string $language = null): array |
||
| 282 | { |
||
| 283 | if ($language) { |
||
| 284 | if (empty($this->data[$language])) { |
||
| 285 | // fallback to default language |
||
| 286 | return $this->data[$this->config->getLanguageDefault()]; |
||
| 287 | } |
||
| 288 | |||
| 289 | return $this->data[$language]; |
||
| 290 | } |
||
| 291 | |||
| 292 | return $this->data; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set collected static files. |
||
| 297 | */ |
||
| 298 | public function setStatic(array $static): void |
||
| 299 | { |
||
| 300 | $this->static = $static; |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Returns static files collection. |
||
| 305 | */ |
||
| 306 | public function getStatic(): array |
||
| 307 | { |
||
| 308 | return $this->static; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Set/update Pages collection. |
||
| 313 | */ |
||
| 314 | public function setPages(PagesCollection $pages): void |
||
| 315 | { |
||
| 316 | $this->pages = $pages; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Returns pages collection. |
||
| 321 | */ |
||
| 322 | public function getPages(): ?PagesCollection |
||
| 323 | { |
||
| 324 | return $this->pages; |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Set menus collection. |
||
| 329 | */ |
||
| 330 | public function setMenus(array $menus): void |
||
| 331 | { |
||
| 332 | $this->menus = $menus; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns all menus, for a language. |
||
| 337 | */ |
||
| 338 | public function getMenus(string $language): Collection\Menu\Collection |
||
| 339 | { |
||
| 340 | return $this->menus[$language]; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Set taxonomies collection. |
||
| 345 | */ |
||
| 346 | public function setTaxonomies(array $taxonomies): void |
||
| 347 | { |
||
| 348 | $this->taxonomies = $taxonomies; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns taxonomies collection, for a language. |
||
| 353 | */ |
||
| 354 | public function getTaxonomies(string $language): ?Collection\Taxonomy\Collection |
||
| 355 | { |
||
| 356 | return $this->taxonomies[$language]; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Set renderer object. |
||
| 361 | */ |
||
| 362 | public function setRenderer(Renderer\RendererInterface $renderer): void |
||
| 363 | { |
||
| 364 | $this->renderer = $renderer; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Returns Renderer object. |
||
| 369 | */ |
||
| 370 | public function getRenderer(): Renderer\RendererInterface |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Returns application version. |
||
| 377 | * |
||
| 378 | * @throws RuntimeException |
||
| 379 | */ |
||
| 380 | public static function getVersion(): string |
||
| 381 | { |
||
| 382 | if (!isset(self::$version)) { |
||
| 383 | $filePath = __DIR__ . '/../VERSION'; |
||
| 384 | if (Plateform::isPhar()) { |
||
| 385 | $filePath = Plateform::getPharPath() . '/VERSION'; |
||
| 386 | } |
||
| 387 | |||
| 388 | try { |
||
| 389 | if (!file_exists($filePath)) { |
||
| 390 | throw new RuntimeException(\sprintf('File "%s" doesn\'t exist.', $filePath)); |
||
| 391 | } |
||
| 392 | $version = Util\File::fileGetContents($filePath); |
||
| 393 | if ($version === false) { |
||
| 394 | throw new RuntimeException(\sprintf('Can\'t get file "%s".', $filePath)); |
||
| 395 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Log configuration errors. |
||
| 407 | */ |
||
| 408 | protected function logConfigError(): void |
||
| 416 |