Passed
Pull Request — master (#2172)
by Arnaud
04:45
created

Render   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 340
Duplicated Lines 0 %

Test Coverage

Coverage 80.23%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 163
c 4
b 1
f 0
dl 0
loc 340
ccs 142
cts 177
cp 0.8023
rs 5.5199
wmc 56

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getAlternates() 0 17 5
A addGlobals() 0 6 1
A init() 0 17 5
A getOutputFormats() 0 30 5
A getAllLayoutsPaths() 0 20 5
A getTranslations() 0 11 5
F process() 0 185 29

How to fix   Complexity   

Complex Class

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