• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Cecilapp / Cecil / 14620241211

23 Apr 2025 02:06PM UTC coverage: 83.787%. First build
14620241211

Pull #2148

github

web-flow
Merge 12fc09dec into 6d7ba8f0a
Pull Request #2148: refactor: configuration and cache

361 of 423 new or added lines in 26 files covered. (85.34%)

3049 of 3639 relevant lines covered (83.79%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.82
/src/Step/Pages/Render.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <arnaud@ligny.fr>
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
    /**
35
     * {@inheritdoc}
36
     */
37
    public function getName(): string
38
    {
39
        return 'Rendering pages';
1✔
40
    }
41

42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function init(array $options): void
46
    {
47
        if (!is_dir($this->config->getLayoutsPath()) && !$this->config->hasTheme()) {
1✔
48
            $message = \sprintf('"%s" is not a valid layouts directory', $this->config->getLayoutsPath());
×
49
            $this->builder->getLogger()->debug($message);
×
50
        }
51

52
        $this->canProcess = true;
1✔
53
    }
54

55
    /**
56
     * {@inheritdoc}
57
     *
58
     * @throws RuntimeException
59
     */
60
    public function process(): void
61
    {
62
        // prepares renderer
63
        $this->builder->setRenderer(new Twig($this->builder, $this->getAllLayoutsPaths()));
1✔
64

65
        // adds global variables
66
        $this->addGlobals();
1✔
67

68
        /** @var Collection $pages */
69
        $pages = $this->builder->getPages()
1✔
70
            // published only
1✔
71
            ->filter(function (Page $page) {
1✔
72
                return (bool) $page->getVariable('published');
1✔
73
            })
1✔
74
            // enrichs some variables
1✔
75
            ->map(function (Page $page) {
1✔
76
                $formats = $this->getOutputFormats($page);
1✔
77
                // output formats
78
                $page->setVariable('output', $formats);
1✔
79
                // alternates formats
80
                $page->setVariable('alternates', $this->getAlternates($formats));
1✔
81
                // translations
82
                $page->setVariable('translations', $this->getTranslations($page));
1✔
83

84
                return $page;
1✔
85
            });
1✔
86
        $total = \count($pages);
1✔
87

88
        // renders each page
89
        $count = 0;
1✔
90
        $postprocessors = [];
1✔
91
        foreach ($this->config->get('output.postprocessors') ?? [] as $name => $postprocessor) {
1✔
92
            try {
93
                if (!class_exists($postprocessor)) {
1✔
94
                    throw new RuntimeException(\sprintf('Class "%s" not found', $postprocessor));
1✔
95
                }
96
                $postprocessors[] = new $postprocessor($this->builder);
1✔
97
                $this->builder->getLogger()->debug(\sprintf('Output post processor "%s" loaded', $name));
1✔
98
            } catch (\Exception $e) {
1✔
99
                $this->builder->getLogger()->error(\sprintf('Unable to load output post processor "%s": %s', $name, $e->getMessage()));
1✔
100
            }
101
        }
102
        /** @var Page $page */
103
        foreach ($pages as $page) {
1✔
104
            $count++;
1✔
105
            $rendered = [];
1✔
106

107
            // l10n
108
            $language = $page->getVariable('language', $this->config->getLanguageDefault());
1✔
109
            $locale = $this->config->getLanguageProperty('locale', $language);
1✔
110
            $this->builder->getRenderer()->setLocale($locale);
1✔
111

112
            // global site variables
113
            $this->builder->getRenderer()->addGlobal('site', new Site($this->builder, $language));
1✔
114

115
            // global config raw variables
116
            $this->builder->getRenderer()->addGlobal('config', new Config($this->builder, $language));
1✔
117

118
            // excluded format(s)?
119
            $formats = (array) $page->getVariable('output');
1✔
120
            foreach ($formats as $key => $format) {
1✔
121
                if ($exclude = $this->config->getOutputFormatProperty($format, 'exclude')) {
1✔
122
                    // ie:
123
                    //   formats:
124
                    //     atom:
125
                    //       [...]
126
                    //       exclude: [paginated]
127
                    if (!\is_array($exclude)) {
1✔
128
                        $exclude = [$exclude];
×
129
                    }
130
                    foreach ($exclude as $variable) {
1✔
131
                        if ($page->hasVariable($variable)) {
1✔
132
                            unset($formats[$key]);
1✔
133
                        }
134
                    }
135
                }
136
            }
137

138
            // renders each output format
139
            foreach ($formats as $format) {
1✔
140
                // search for the template
141
                $layout = Layout::finder($page, $format, $this->config);
1✔
142
                // renders with Twig
143
                try {
144
                    $deprecations = [];
1✔
145
                    set_error_handler(function ($type, $msg) use (&$deprecations) {
1✔
146
                        if (E_USER_DEPRECATED === $type) {
1✔
147
                            $deprecations[] = $msg;
1✔
148
                        }
149
                    });
1✔
150
                    $output = $this->builder->getRenderer()->render($layout['file'], ['page' => $page]);
1✔
151
                    foreach ($deprecations as $value) {
1✔
152
                        $this->builder->getLogger()->warning($value);
1✔
153
                    }
154
                    foreach ($postprocessors as $postprocessor) {
1✔
155
                        $output = $postprocessor->process($page, $output, $format);
1✔
156
                    }
157
                    $rendered[$format] = [
1✔
158
                        'output'   => $output,
1✔
159
                        'template' => [
1✔
160
                            'scope' => $layout['scope'],
1✔
161
                            'file'  => $layout['file'],
1✔
162
                        ],
1✔
163
                    ];
1✔
164
                    $page->addRendered($rendered);
1✔
165
                } catch (\Twig\Error\Error $e) {
×
NEW
166
                    throw new RuntimeException(
×
NEW
167
                        \sprintf(
×
NEW
168
                            'Can\'t render template "%s" for page "%s".',
×
NEW
169
                            $e->getSourceContext()->getName(),
×
NEW
170
                            $page->getFileName()
×
NEW
171
                        ),
×
NEW
172
                        previous: $e,
×
NEW
173
                        file: $e->getSourceContext()->getPath(),
×
NEW
174
                        line: $e->getTemplateLine(),
×
NEW
175
                    );
×
176
                } catch (\Exception $e) {
×
NEW
177
                    throw new RuntimeException($e->getMessage(), previous: $e);
×
178
                }
179
            }
180
            $this->builder->getPages()->replace($page->getId(), $page);
1✔
181

182
            $templates = array_column($rendered, 'template');
1✔
183
            $message = \sprintf(
1✔
184
                'Page "%s" rendered with [%s]',
1✔
185
                $page->getId() ?: 'index',
1✔
186
                Util\Str::combineArrayToString($templates, 'scope', 'file')
1✔
187
            );
1✔
188
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
1✔
189
        }
190
        // profiler
191
        if ($this->builder->isDebug()) {
1✔
192
            try {
193
                // HTML
194
                $htmlDumper = new \Twig\Profiler\Dumper\HtmlDumper();
1✔
195
                $profileHtmlFile = Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR, 'twig_profile.html');
1✔
196
                Util\File::getFS()->dumpFile($profileHtmlFile, $htmlDumper->dump($this->builder->getRenderer()->getDebugProfile()));
1✔
197
                // TXT
198
                $textDumper = new \Twig\Profiler\Dumper\TextDumper();
1✔
199
                $profileTextFile = Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR, 'twig_profile.txt');
1✔
200
                Util\File::getFS()->dumpFile($profileTextFile, $textDumper->dump($this->builder->getRenderer()->getDebugProfile()));
1✔
201
                // log
202
                $this->builder->getLogger()->debug(\sprintf('Twig profile dumped in "%s"', Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR)));
1✔
203
            } catch (\Symfony\Component\Filesystem\Exception\IOException $e) {
×
204
                throw new RuntimeException($e->getMessage());
×
205
            }
206
        }
207
    }
208

209
    /**
210
     * Returns an array of layouts directories.
211
     */
212
    protected function getAllLayoutsPaths(): array
213
    {
214
        $paths = [];
1✔
215

216
        // layouts/
217
        if (is_dir($this->config->getLayoutsPath())) {
1✔
218
            $paths[] = $this->config->getLayoutsPath();
1✔
219
        }
220
        // <theme>/layouts/
221
        if ($this->config->hasTheme()) {
1✔
222
            foreach ($this->config->getTheme() ?? [] as $theme) {
1✔
223
                $paths[] = $this->config->getThemeDirPath($theme);
1✔
224
            }
225
        }
226
        // resources/layouts/
227
        if (is_dir($this->config->getLayoutsInternalPath())) {
1✔
228
            $paths[] = $this->config->getLayoutsInternalPath();
1✔
229
        }
230

231
        return $paths;
1✔
232
    }
233

234
    /**
235
     * Adds global variables.
236
     */
237
    protected function addGlobals()
238
    {
239
        $this->builder->getRenderer()->addGlobal('cecil', [
1✔
240
            'url'       => \sprintf('https://cecil.app/#%s', Builder::getVersion()),
1✔
241
            'version'   => Builder::getVersion(),
1✔
242
            'poweredby' => \sprintf('Cecil v%s', Builder::getVersion()),
1✔
243
        ]);
1✔
244
    }
245

246
    /**
247
     * Get available output formats.
248
     *
249
     * @throws RuntimeException
250
     */
251
    protected function getOutputFormats(Page $page): array
252
    {
253
        // Get page output format(s) if defined.
254
        // ie:
255
        // ```yaml
256
        // output: txt
257
        // ```
258
        if ($page->getVariable('output')) {
1✔
259
            $formats = $page->getVariable('output');
1✔
260
            if (!\is_array($formats)) {
1✔
261
                $formats = [$formats];
1✔
262
            }
263

264
            return $formats;
1✔
265
        }
266

267
        // Get available output formats for the page type.
268
        // ie:
269
        // ```yaml
270
        // page: [html, json]
271
        // ```
272
        $formats = $this->config->get('output.pagetypeformats.' . $page->getType());
1✔
273
        if (empty($formats)) {
1✔
274
            throw new RuntimeException('Configuration key "pagetypeformats" can\'t be empty.');
×
275
        }
276
        if (!\is_array($formats)) {
1✔
277
            $formats = [$formats];
×
278
        }
279

280
        return array_unique($formats);
1✔
281
    }
282

283
    /**
284
     * Get alternates.
285
     */
286
    protected function getAlternates(array $formats): array
287
    {
288
        $alternates = [];
1✔
289

290
        if (\count($formats) > 1 || \in_array('html', $formats)) {
1✔
291
            foreach ($formats as $format) {
1✔
292
                $format == 'html' ? $rel = 'canonical' : $rel = 'alternate';
1✔
293
                $alternates[] = [
1✔
294
                    'rel'    => $rel,
1✔
295
                    'type'   => $this->config->getOutputFormatProperty($format, 'mediatype'),
1✔
296
                    'title'  => strtoupper($format),
1✔
297
                    'format' => $format,
1✔
298
                ];
1✔
299
            }
300
        }
301

302
        return $alternates;
1✔
303
    }
304

305
    /**
306
     * Returns the collection of translated pages for a given page.
307
     */
308
    protected function getTranslations(Page $refPage): \Cecil\Collection\Page\Collection
309
    {
310
        $pages = $this->builder->getPages()->filter(function (Page $page) use ($refPage) {
1✔
311
            return $page->getId() !== $refPage->getId()
1✔
312
                && $page->getVariable('langref') == $refPage->getVariable('langref')
1✔
313
                && $page->getType() == $refPage->getType()
1✔
314
                && !empty($page->getVariable('published'))
1✔
315
                && !$page->getVariable('paginated');
1✔
316
        });
1✔
317

318
        return $pages;
1✔
319
    }
320
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc