| Conditions | 13 |
| Paths | 385 |
| Total Lines | 110 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 49 | public function process() |
||
| 50 | { |
||
| 51 | // prepares renderer |
||
| 52 | $this->builder->setRenderer(new Twig($this->getAllLayoutsPaths(), $this->builder)); |
||
| 53 | |||
| 54 | // adds global variables |
||
| 55 | $this->addGlobals(); |
||
| 56 | |||
| 57 | call_user_func_array($this->builder->getMessageCb(), ['RENDER', 'Rendering pages']); |
||
| 58 | |||
| 59 | // collects published pages |
||
| 60 | /** @var Page $page */ |
||
| 61 | $filteredPages = $this->builder->getPages()->filter(function (Page $page) { |
||
| 62 | return !empty($page->getVariable('published')); |
||
| 63 | }); |
||
| 64 | $max = count($filteredPages); |
||
| 65 | |||
| 66 | // renders each page |
||
| 67 | $count = 0; |
||
| 68 | /** @var Page $page */ |
||
| 69 | foreach ($filteredPages as $page) { |
||
| 70 | $count++; |
||
| 71 | $formats = ['html']; |
||
|
|
|||
| 72 | $rendered = []; |
||
| 73 | |||
| 74 | // i18n |
||
| 75 | $pageLang = $page->getVariable('language'); |
||
| 76 | $locale = $this->config->getLanguageProperty('locale', $pageLang); |
||
| 77 | // the PHP Intl extension is needed to use localized date |
||
| 78 | if (extension_loaded('intl')) { |
||
| 79 | \Locale::setDefault($locale); |
||
| 80 | } |
||
| 81 | // the PHP Gettext extension is needed to use translation |
||
| 82 | if (extension_loaded('gettext')) { |
||
| 83 | $localePath = realpath(Util::joinFile($this->config->getSourceDir(), 'locale')); |
||
| 84 | $domain = 'messages'; |
||
| 85 | putenv("LC_ALL=$locale"); |
||
| 86 | putenv("LANGUAGE=$locale"); |
||
| 87 | setlocale(LC_ALL, "$locale.UTF-8"); |
||
| 88 | bindtextdomain($domain, $localePath); |
||
| 89 | } |
||
| 90 | |||
| 91 | // global site variables |
||
| 92 | $this->builder->getRenderer()->addGlobal('site', new Site($this->builder, $pageLang)); |
||
| 93 | |||
| 94 | // get Page's output formats |
||
| 95 | $formats = $this->getOutputFormats($page); |
||
| 96 | $page->setVariable('output', $formats); |
||
| 97 | |||
| 98 | // excluded format(s)? |
||
| 99 | foreach ($formats as $key => $format) { |
||
| 100 | if ($exclude = $this->config->getOutputFormatProperty($format, 'exclude')) { |
||
| 101 | // ie: |
||
| 102 | // formats: |
||
| 103 | // atom: |
||
| 104 | // [...] |
||
| 105 | // exclude: [paginated] |
||
| 106 | if (!is_array($exclude)) { |
||
| 107 | $exclude = [$exclude]; |
||
| 108 | } |
||
| 109 | foreach ($exclude as $variable) { |
||
| 110 | if ($page->hasVariable($variable)) { |
||
| 111 | unset($formats[$key]); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | // get and set alternates links |
||
| 118 | $page->setVariable('alternates', $this->getAlternates($formats)); |
||
| 119 | |||
| 120 | // renders each output format |
||
| 121 | foreach ($formats as $format) { |
||
| 122 | // search for the template |
||
| 123 | $layout = Layout::finder($page, $format, $this->config); |
||
| 124 | // renders with Twig |
||
| 125 | try { |
||
| 126 | $output = $this->builder->getRenderer()->render($layout['file'], ['page' => $page]); |
||
| 127 | $output = $this->postProcessOutput($output, $page, $format); |
||
| 128 | $rendered[$format]['output'] = $output; |
||
| 129 | $rendered[$format]['template']['scope'] = $layout['scope']; |
||
| 130 | $rendered[$format]['template']['file'] = $layout['file']; |
||
| 131 | } catch (\Twig\Error\Error $e) { |
||
| 132 | throw new Exception(sprintf( |
||
| 133 | "%s\nChecks template \"%s:%s\" line %s, used by page \"%s\".", |
||
| 134 | $e->getMessage(), |
||
| 135 | $layout['scope'], |
||
| 136 | $layout['file'], |
||
| 137 | $e->getLine(), |
||
| 138 | $page->getId() |
||
| 139 | )); |
||
| 140 | } catch (\Exception $e) { |
||
| 141 | throw new Exception(sprintf( |
||
| 142 | 'Error in template "%s:%s" for page "%s"', |
||
| 143 | $layout['scope'], |
||
| 144 | $layout['file'], |
||
| 145 | $page->getId() |
||
| 146 | )); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | $page->setVariable('rendered', $rendered); |
||
| 150 | $this->builder->getPages()->replace($page->getId(), $page); |
||
| 151 | |||
| 152 | $templates = array_column($rendered, 'template'); |
||
| 153 | $message = sprintf( |
||
| 154 | '%s [%s]', |
||
| 155 | ($page->getId() ?: 'index'), |
||
| 156 | Util::combineArrayToString($templates, 'scope', 'file') |
||
| 157 | ); |
||
| 158 | call_user_func_array($this->builder->getMessageCb(), ['RENDER_PROGRESS', $message, $count, $max]); |
||
| 159 | } |
||
| 305 |