| Total Complexity | 56 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 2 | Features | 1 |
Complex classes like Render 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 Render, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 42 | class Render extends AbstractStep |
||
| 43 | { |
||
| 44 | public const TMP_DIR = '.cecil'; |
||
| 45 | |||
| 46 | protected $subset = []; |
||
| 47 | |||
| 48 | #[Inject] |
||
| 49 | private TwigFactory $twigFactory; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * {@inheritdoc} |
||
| 53 | */ |
||
| 54 | public function getName(): string |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | public function init(array $options): void |
||
| 63 | { |
||
| 64 | if (!is_dir($this->config->getLayoutsPath()) && !$this->config->hasTheme()) { |
||
| 65 | $message = \sprintf('"%s" is not a valid layouts directory', $this->config->getLayoutsPath()); |
||
| 66 | $this->logger->debug($message); |
||
| 67 | } |
||
| 68 | |||
| 69 | // render a subset of pages? |
||
| 70 | if (!empty($options['render-subset'])) { |
||
| 71 | $subset = \sprintf('pages.subsets.%s', (string) $options['render-subset']); |
||
| 72 | if (!$this->config->has($subset)) { |
||
| 73 | throw new ConfigException(\sprintf('Subset "%s" not found.', $subset)); |
||
| 74 | } |
||
| 75 | $this->subset = (array) $this->config->get($subset); |
||
| 76 | } |
||
| 77 | |||
| 78 | $this->canProcess = true; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * {@inheritdoc} |
||
| 83 | * |
||
| 84 | * @throws RuntimeException |
||
| 85 | */ |
||
| 86 | public function process(): void |
||
| 87 | { |
||
| 88 | // prepares renderer |
||
| 89 | $this->builder->setRenderer($this->twigFactory->create($this->getAllLayoutsPaths())); |
||
| 90 | |||
| 91 | // adds global variables |
||
| 92 | $this->addGlobals(); |
||
| 93 | |||
| 94 | $subset = $this->subset; |
||
| 95 | |||
| 96 | /** @var Collection $pages */ |
||
| 97 | $pages = $this->builder->getPages() |
||
| 98 | // published only |
||
| 99 | ->filter(function (Page $page) { |
||
| 100 | return (bool) $page->getVariable('published'); |
||
| 101 | }) |
||
| 102 | ->filter(function (Page $page) use ($subset) { |
||
| 103 | if (empty($subset)) { |
||
| 104 | return true; |
||
| 105 | } |
||
| 106 | if ( |
||
| 107 | !empty($subset['path']) |
||
| 108 | && !((bool) preg_match('/' . (string) $subset['path'] . '/i', $page->getPath())) |
||
| 109 | ) { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | if (!empty($subset['language'])) { |
||
| 113 | $language = $page->getVariable('language', $this->config->getLanguageDefault()); |
||
| 114 | if ($language !== (string) $subset['language']) { |
||
| 115 | return false; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | return true; |
||
| 119 | }) |
||
| 120 | // enrichs some variables |
||
| 121 | ->map(function (Page $page) { |
||
| 122 | $formats = $this->getOutputFormats($page); |
||
| 123 | // output formats |
||
| 124 | $page->setVariable('output', $formats); |
||
| 125 | // alternates formats |
||
| 126 | $page->setVariable('alternates', $this->getAlternates($formats)); |
||
| 127 | // translations |
||
| 128 | $page->setVariable('translations', $this->getTranslations($page)); |
||
| 129 | |||
| 130 | return $page; |
||
| 131 | }); |
||
| 132 | $total = \count($pages); |
||
| 133 | |||
| 134 | // renders each page |
||
| 135 | $count = 0; |
||
| 136 | $postprocessors = []; |
||
| 137 | foreach ((array) $this->config->get('output.postprocessors') as $name => $postprocessor) { |
||
| 138 | try { |
||
| 139 | if (!class_exists($postprocessor)) { |
||
| 140 | throw new RuntimeException(\sprintf('Class "%s" not found', $postprocessor)); |
||
| 141 | } |
||
| 142 | $postprocessors[] = new $postprocessor($this->builder); |
||
| 143 | $this->logger->debug(\sprintf('Output post processor "%s" loaded', $name)); |
||
| 144 | } catch (\Exception $e) { |
||
| 145 | $this->logger->error(\sprintf('Unable to load output post processor "%s": %s', $name, $e->getMessage())); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | // some cache to avoid multiple calls |
||
| 150 | $cache = []; |
||
| 151 | |||
| 152 | /** @var Page $page */ |
||
| 153 | foreach ($pages as $page) { |
||
| 154 | $count++; |
||
| 155 | $rendered = []; |
||
| 156 | |||
| 157 | // l10n |
||
| 158 | $language = $page->getVariable('language', $this->config->getLanguageDefault()); |
||
| 159 | if (!isset($cache['locale'][$language])) { |
||
| 160 | $cache['locale'][$language] = $this->config->getLanguageProperty('locale', $language); |
||
| 161 | } |
||
| 162 | $this->builder->getRenderer()->setLocale($cache['locale'][$language]); |
||
| 163 | |||
| 164 | // global site variables |
||
| 165 | if (!isset($cache['site'][$language])) { |
||
| 166 | $cache['site'][$language] = new Site($this->builder, $language); |
||
| 167 | } |
||
| 168 | $this->builder->getRenderer()->addGlobal('site', $cache['site'][$language]); |
||
| 169 | |||
| 170 | // global config raw variables |
||
| 171 | if (!isset($cache['config'][$language])) { |
||
| 172 | $cache['config'][$language] = new RendererConfig($this->builder, $language); |
||
| 173 | } |
||
| 174 | $this->builder->getRenderer()->addGlobal('config', $cache['config'][$language]); |
||
| 175 | |||
| 176 | // excluded format(s)? |
||
| 177 | $formats = (array) $page->getVariable('output'); |
||
| 178 | foreach ($formats as $key => $format) { |
||
| 179 | if ($exclude = $this->config->getOutputFormatProperty($format, 'exclude')) { |
||
| 180 | // ie: |
||
| 181 | // formats: |
||
| 182 | // atom: |
||
| 183 | // [...] |
||
| 184 | // exclude: [paginated] |
||
| 185 | if (!\is_array($exclude)) { |
||
| 186 | $exclude = [$exclude]; |
||
| 187 | } |
||
| 188 | foreach ($exclude as $variable) { |
||
| 189 | if ($page->hasVariable($variable)) { |
||
| 190 | unset($formats[$key]); |
||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | // specific output format from subset |
||
| 197 | if (!empty($this->subset['output'])) { |
||
| 198 | $currentFormats = $formats; |
||
| 199 | $formats = []; |
||
| 200 | if (\in_array((string) $this->subset['output'], $currentFormats)) { |
||
| 201 | $formats = [(string) $this->subset['output']]; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | // renders each output format |
||
| 206 | foreach ($formats as $format) { |
||
| 207 | // search for the template |
||
| 208 | $layout = Layout::finder($page, $format, $this->config); |
||
| 209 | // renders with Twig |
||
| 210 | try { |
||
| 211 | $deprecations = []; |
||
| 212 | set_error_handler(function ($type, $msg) use (&$deprecations) { |
||
| 213 | if (E_USER_DEPRECATED === $type) { |
||
| 214 | $deprecations[] = $msg; |
||
| 215 | } |
||
| 216 | }); |
||
| 217 | $output = $this->builder->getRenderer()->render($layout['file'], ['page' => $page]); |
||
| 218 | foreach ($deprecations as $value) { |
||
| 219 | $this->builder->getLogger()->warning($value); |
||
| 220 | } |
||
| 221 | foreach ($postprocessors as $postprocessor) { |
||
| 222 | $output = $postprocessor->process($page, $output, $format); |
||
| 223 | } |
||
| 224 | $rendered[$format] = [ |
||
| 225 | 'output' => $output, |
||
| 226 | 'template' => [ |
||
| 227 | 'scope' => $layout['scope'], |
||
| 228 | 'file' => $layout['file'], |
||
| 229 | ], |
||
| 230 | ]; |
||
| 231 | $page->addRendered($rendered); |
||
| 232 | } catch (\Twig\Error\Error $e) { |
||
| 233 | throw new RuntimeException( |
||
| 234 | \sprintf( |
||
| 235 | 'Unable to render template "%s" for page "%s".', |
||
| 236 | $e->getSourceContext()->getName(), |
||
| 237 | $page->getFileName() ?? $page->getId() |
||
| 238 | ), |
||
| 239 | previous: $e, |
||
| 240 | file: $e->getSourceContext()->getPath(), |
||
| 241 | line: $e->getTemplateLine(), |
||
| 242 | ); |
||
| 243 | } catch (\Exception $e) { |
||
| 244 | throw new RuntimeException($e->getMessage(), previous: $e); |
||
| 245 | } |
||
| 246 | } |
||
| 247 | $this->builder->getPages()->replace($page->getId(), $page); |
||
| 248 | |||
| 249 | $templates = array_column($rendered, 'template'); |
||
| 250 | $message = \sprintf( |
||
| 251 | 'Page "%s" rendered with [%s]', |
||
| 252 | $page->getId() ?: 'index', |
||
| 253 | Util\Str::combineArrayToString($templates, 'scope', 'file') |
||
| 254 | ); |
||
| 255 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
| 256 | } |
||
| 257 | // profiler |
||
| 258 | if ($this->builder->isDebug()) { |
||
| 259 | try { |
||
| 260 | // HTML |
||
| 261 | $htmlDumper = new \Twig\Profiler\Dumper\HtmlDumper(); |
||
| 262 | $profileHtmlFile = Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR, 'twig_profile.html'); |
||
| 263 | Util\File::getFS()->dumpFile($profileHtmlFile, $htmlDumper->dump($this->builder->getRenderer()->getDebugProfile())); |
||
| 264 | // TXT |
||
| 265 | $textDumper = new \Twig\Profiler\Dumper\TextDumper(); |
||
| 266 | $profileTextFile = Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR, 'twig_profile.txt'); |
||
| 267 | Util\File::getFS()->dumpFile($profileTextFile, $textDumper->dump($this->builder->getRenderer()->getDebugProfile())); |
||
| 268 | // log |
||
| 269 | $this->builder->getLogger()->debug(\sprintf('Twig profile dumped in "%s"', Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR))); |
||
| 270 | } catch (\Symfony\Component\Filesystem\Exception\IOException $e) { |
||
| 271 | throw new RuntimeException($e->getMessage()); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns an array of layouts directories. |
||
| 278 | */ |
||
| 279 | protected function getAllLayoutsPaths(): array |
||
| 280 | { |
||
| 281 | $paths = []; |
||
| 282 | |||
| 283 | // layouts/ |
||
| 284 | if (is_dir($this->config->getLayoutsPath())) { |
||
| 285 | $paths[] = $this->config->getLayoutsPath(); |
||
| 286 | } |
||
| 287 | // <theme>/layouts/ |
||
| 288 | if ($this->config->hasTheme()) { |
||
| 289 | foreach ($this->config->getTheme() ?? [] as $theme) { |
||
| 290 | $paths[] = $this->config->getThemeDirPath($theme); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | // resources/layouts/ |
||
| 294 | if (is_dir($this->config->getLayoutsInternalPath())) { |
||
| 295 | $paths[] = $this->config->getLayoutsInternalPath(); |
||
| 296 | } |
||
| 297 | |||
| 298 | return $paths; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Adds global variables. |
||
| 303 | */ |
||
| 304 | protected function addGlobals() |
||
| 305 | { |
||
| 306 | $this->builder->getRenderer()->addGlobal('cecil', [ |
||
| 307 | 'url' => \sprintf('https://cecil.app/#%s', Builder::getVersion()), |
||
| 308 | 'version' => Builder::getVersion(), |
||
| 309 | 'poweredby' => \sprintf('Cecil v%s', Builder::getVersion()), |
||
| 310 | ]); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Get available output formats. |
||
| 315 | * |
||
| 316 | * @throws RuntimeException |
||
| 317 | */ |
||
| 318 | protected function getOutputFormats(Page $page): array |
||
| 319 | { |
||
| 320 | // Get page output format(s) if defined. |
||
| 321 | // ie: |
||
| 322 | // ```yaml |
||
| 323 | // output: txt |
||
| 324 | // ``` |
||
| 325 | if ($page->getVariable('output')) { |
||
| 326 | $formats = $page->getVariable('output'); |
||
| 327 | if (!\is_array($formats)) { |
||
| 328 | $formats = [$formats]; |
||
| 329 | } |
||
| 330 | |||
| 331 | return $formats; |
||
| 332 | } |
||
| 333 | |||
| 334 | // Get available output formats for the page type. |
||
| 335 | // ie: |
||
| 336 | // ```yaml |
||
| 337 | // page: [html, json] |
||
| 338 | // ``` |
||
| 339 | $formats = $this->config->get('output.pagetypeformats.' . $page->getType()); |
||
| 340 | if (empty($formats)) { |
||
| 341 | throw new RuntimeException('Configuration key "pagetypeformats" can\'t be empty.'); |
||
| 342 | } |
||
| 343 | if (!\is_array($formats)) { |
||
| 344 | $formats = [$formats]; |
||
| 345 | } |
||
| 346 | |||
| 347 | return array_unique($formats); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get alternates. |
||
| 352 | */ |
||
| 353 | protected function getAlternates(array $formats): array |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Returns the collection of translated pages for a given page. |
||
| 374 | */ |
||
| 375 | protected function getTranslations(Page $refPage): Collection |
||
| 387 | } |
||
| 388 | } |
||
| 389 |