Conditions | 29 |
Paths | > 20000 |
Total Lines | 186 |
Code Lines | 109 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
78 | public function process(): void |
||
79 | { |
||
80 | // prepares renderer |
||
81 | $this->builder->setRenderer(new Twig($this->builder, $this->getAllLayoutsPaths())); |
||
82 | |||
83 | // adds global variables |
||
84 | $this->addGlobals(); |
||
85 | |||
86 | $subset = $this->subset; |
||
87 | |||
88 | /** @var Collection $pages */ |
||
89 | $pages = $this->builder->getPages() |
||
90 | // published only |
||
91 | ->filter(function (Page $page) { |
||
92 | return (bool) $page->getVariable('published'); |
||
93 | }) |
||
94 | ->filter(function (Page $page) use ($subset) { |
||
95 | if (empty($subset)) { |
||
96 | return true; |
||
97 | } |
||
98 | if ( |
||
99 | !empty($subset['path']) |
||
100 | && !((bool) preg_match('/' . (string) $subset['path'] . '/i', $page->getPath())) |
||
101 | ) { |
||
102 | return false; |
||
103 | } |
||
104 | if (!empty($subset['language'])) { |
||
105 | $language = $page->getVariable('language', $this->config->getLanguageDefault()); |
||
106 | if ($language !== (string) $subset['language']) { |
||
107 | return false; |
||
108 | } |
||
109 | } |
||
110 | return true; |
||
111 | }) |
||
112 | // enrichs some variables |
||
113 | ->map(function (Page $page) { |
||
114 | $formats = $this->getOutputFormats($page); |
||
115 | // output formats |
||
116 | $page->setVariable('output', $formats); |
||
117 | // alternates formats |
||
118 | $page->setVariable('alternates', $this->getAlternates($formats)); |
||
119 | // translations |
||
120 | $page->setVariable('translations', $this->getTranslations($page)); |
||
121 | |||
122 | return $page; |
||
123 | }); |
||
124 | $total = \count($pages); |
||
125 | |||
126 | // renders each page |
||
127 | $count = 0; |
||
128 | $postprocessors = []; |
||
129 | foreach ((array) $this->config->get('output.postprocessors') as $name => $postprocessor) { |
||
130 | try { |
||
131 | if (!class_exists($postprocessor)) { |
||
132 | throw new RuntimeException(\sprintf('Class "%s" not found', $postprocessor)); |
||
133 | } |
||
134 | $postprocessors[] = new $postprocessor($this->builder); |
||
135 | $this->builder->getLogger()->debug(\sprintf('Output post processor "%s" loaded', $name)); |
||
136 | } catch (\Exception $e) { |
||
137 | $this->builder->getLogger()->error(\sprintf('Unable to load output post processor "%s": %s', $name, $e->getMessage())); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | // some caches to avoid multiple calls |
||
142 | $cacheLocale = $cacheSite = $cacheConfig = []; |
||
143 | |||
144 | /** @var Page $page */ |
||
145 | foreach ($pages as $page) { |
||
146 | $count++; |
||
147 | $rendered = []; |
||
148 | |||
149 | // l10n |
||
150 | $language = $page->getVariable('language', $this->config->getLanguageDefault()); |
||
151 | if (!isset($cacheLocale[$language])) { |
||
152 | $cacheLocale[$language] = $this->config->getLanguageProperty('locale', $language); |
||
153 | } |
||
154 | $this->builder->getRenderer()->setLocale($cacheLocale[$language]); |
||
155 | |||
156 | // global site variables |
||
157 | if (!isset($cacheSite[$language])) { |
||
158 | $cacheSite[$language] = new Site($this->builder, $language); |
||
159 | } |
||
160 | $this->builder->getRenderer()->addGlobal('site', $cacheSite[$language]); |
||
161 | |||
162 | // global config raw variables |
||
163 | if (!isset($cacheConfig[$language])) { |
||
164 | $cacheConfig[$language] = new Config($this->builder, $language); |
||
165 | } |
||
166 | $this->builder->getRenderer()->addGlobal('config', $cacheConfig[$language]); |
||
167 | |||
168 | // excluded format(s)? |
||
169 | $formats = (array) $page->getVariable('output'); |
||
170 | foreach ($formats as $key => $format) { |
||
171 | if ($exclude = $this->config->getOutputFormatProperty($format, 'exclude')) { |
||
172 | // ie: |
||
173 | // formats: |
||
174 | // atom: |
||
175 | // [...] |
||
176 | // exclude: [paginated] |
||
177 | if (!\is_array($exclude)) { |
||
178 | $exclude = [$exclude]; |
||
179 | } |
||
180 | foreach ($exclude as $variable) { |
||
181 | if ($page->hasVariable($variable)) { |
||
182 | unset($formats[$key]); |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | } |
||
187 | |||
188 | // specific output format from subset |
||
189 | if (!empty($this->subset['output'])) { |
||
190 | $currentFormats = $formats; |
||
191 | $formats = []; |
||
192 | if (\in_array((string) $this->subset['output'], $currentFormats)) { |
||
193 | $formats = [(string) $this->subset['output']]; |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // renders each output format |
||
198 | foreach ($formats as $format) { |
||
199 | // search for the template |
||
200 | $layout = Layout::finder($page, $format, $this->config); |
||
201 | // renders with Twig |
||
202 | try { |
||
203 | $deprecations = []; |
||
204 | set_error_handler(function ($type, $msg) use (&$deprecations) { |
||
205 | if (E_USER_DEPRECATED === $type) { |
||
206 | $deprecations[] = $msg; |
||
207 | } |
||
208 | }); |
||
209 | $output = $this->builder->getRenderer()->render($layout['file'], ['page' => $page]); |
||
210 | foreach ($deprecations as $value) { |
||
211 | $this->builder->getLogger()->warning($value); |
||
212 | } |
||
213 | foreach ($postprocessors as $postprocessor) { |
||
214 | $output = $postprocessor->process($page, $output, $format); |
||
215 | } |
||
216 | $rendered[$format] = [ |
||
217 | 'output' => $output, |
||
218 | 'template' => [ |
||
219 | 'scope' => $layout['scope'], |
||
220 | 'file' => $layout['file'], |
||
221 | ], |
||
222 | ]; |
||
223 | $page->addRendered($rendered); |
||
224 | } catch (\Twig\Error\Error $e) { |
||
225 | throw new RuntimeException( |
||
226 | \sprintf( |
||
227 | 'Can\'t render template "%s" for page "%s".', |
||
228 | $e->getSourceContext()->getName(), |
||
229 | $page->getFileName() ?? $page->getId() |
||
230 | ), |
||
231 | previous: $e, |
||
232 | file: $e->getSourceContext()->getPath(), |
||
233 | line: $e->getTemplateLine(), |
||
234 | ); |
||
235 | } catch (\Exception $e) { |
||
236 | throw new RuntimeException($e->getMessage(), previous: $e); |
||
237 | } |
||
238 | } |
||
239 | $this->builder->getPages()->replace($page->getId(), $page); |
||
240 | |||
241 | $templates = array_column($rendered, 'template'); |
||
242 | $message = \sprintf( |
||
243 | 'Page "%s" rendered with [%s]', |
||
244 | $page->getId() ?: 'index', |
||
245 | Util\Str::combineArrayToString($templates, 'scope', 'file') |
||
246 | ); |
||
247 | $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]); |
||
248 | } |
||
249 | // profiler |
||
250 | if ($this->builder->isDebug()) { |
||
251 | try { |
||
252 | // HTML |
||
253 | $htmlDumper = new \Twig\Profiler\Dumper\HtmlDumper(); |
||
254 | $profileHtmlFile = Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR, 'twig_profile.html'); |
||
255 | Util\File::getFS()->dumpFile($profileHtmlFile, $htmlDumper->dump($this->builder->getRenderer()->getDebugProfile())); |
||
256 | // TXT |
||
257 | $textDumper = new \Twig\Profiler\Dumper\TextDumper(); |
||
258 | $profileTextFile = Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR, 'twig_profile.txt'); |
||
259 | Util\File::getFS()->dumpFile($profileTextFile, $textDumper->dump($this->builder->getRenderer()->getDebugProfile())); |
||
260 | // log |
||
261 | $this->builder->getLogger()->debug(\sprintf('Twig profile dumped in "%s"', Util::joinFile($this->config->getDestinationDir(), self::TMP_DIR))); |
||
262 | } catch (\Symfony\Component\Filesystem\Exception\IOException $e) { |
||
263 | throw new RuntimeException($e->getMessage()); |
||
264 | } |
||
382 |