Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 33 | class Compiler extends BaseManager |
||
| 34 | { |
||
| 35 | /** @var string|false */ |
||
| 36 | private $redirectTemplate; |
||
| 37 | |||
| 38 | /** @var Twig_Template[] */ |
||
| 39 | private $templateDependencies; |
||
| 40 | |||
| 41 | /** @var PageView[] */ |
||
| 42 | private $pageViewsFlattened; |
||
| 43 | |||
| 44 | /** @var PageView[][] */ |
||
| 45 | private $pageViews; |
||
| 46 | |||
| 47 | /** @var Folder */ |
||
| 48 | private $folder; |
||
| 49 | |||
| 50 | /** @var string */ |
||
| 51 | private $theme; |
||
| 52 | |||
| 53 | /** @var Twig_Environment */ |
||
| 54 | private $twig; |
||
| 55 | |||
| 56 | 14 | public function __construct() |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param string|false $template |
||
| 66 | */ |
||
| 67 | public function setRedirectTemplate($template) |
||
| 68 | { |
||
| 69 | $this->redirectTemplate = $template; |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param Folder $folder |
||
| 74 | */ |
||
| 75 | 14 | public function setTargetFolder(Folder $folder) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @param PageView[][] $pageViews |
||
| 82 | * @param PageView[] $pageViewsFlattened |
||
| 83 | */ |
||
| 84 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
| 89 | |||
| 90 | public function setThemeName($themeName) |
||
| 91 | { |
||
| 92 | $this->theme = $themeName; |
||
| 93 | } |
||
| 94 | |||
| 95 | /// |
||
| 96 | // Twig parent templates |
||
| 97 | /// |
||
| 98 | |||
| 99 | public function isParentTemplate($filePath) |
||
| 100 | { |
||
| 101 | return isset($this->templateDependencies[$filePath]); |
||
| 102 | } |
||
| 103 | |||
| 104 | public function refreshParent($filePath) |
||
| 105 | { |
||
| 106 | foreach ($this->templateDependencies[$filePath] as &$parentTemplate) |
||
|
|
|||
| 107 | { |
||
| 108 | $this->compilePageView($parentTemplate); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | /// |
||
| 113 | // IO Functionality |
||
| 114 | /// |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Compile all of the PageViews registered with the compiler. |
||
| 118 | * |
||
| 119 | * @since 0.1.0 |
||
| 120 | */ |
||
| 121 | 14 | public function compileAll() |
|
| 122 | { |
||
| 123 | 14 | foreach ($this->pageViewsFlattened as &$pageView) |
|
| 124 | { |
||
| 125 | 14 | $this->compilePageView($pageView); |
|
| 126 | 14 | } |
|
| 127 | 14 | } |
|
| 128 | |||
| 129 | public function compileSome($filter = array()) |
||
| 130 | { |
||
| 131 | /** @var PageView $pageView */ |
||
| 132 | foreach ($this->pageViewsFlattened as &$pageView) |
||
| 133 | { |
||
| 134 | if ($pageView->hasTwigDependency($filter['namespace'], $filter['dependency'])) |
||
| 135 | { |
||
| 136 | $this->compilePageView($pageView); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Compile an individual PageView item. |
||
| 143 | * |
||
| 144 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 145 | * respective target file. |
||
| 146 | * |
||
| 147 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 148 | * |
||
| 149 | * @since 0.1.1 |
||
| 150 | */ |
||
| 151 | 14 | public function compilePageView(&$pageView) |
|
| 152 | { |
||
| 153 | 14 | $this->output->debug('Compiling {type} PageView: {pageview}', array( |
|
| 154 | 14 | 'pageview' => $pageView->getRelativeFilePath(), |
|
| 155 | 14 | 'type' => $pageView->getType() |
|
| 156 | 14 | )); |
|
| 157 | |||
| 158 | 14 | switch ($pageView->getType()) |
|
| 159 | { |
||
| 160 | 14 | case PageView::STATIC_TYPE: |
|
| 161 | 10 | $this->compileStaticPageView($pageView); |
|
| 162 | 10 | $this->compileStandardRedirects($pageView); |
|
| 163 | 10 | break; |
|
| 164 | |||
| 165 | 4 | case PageView::DYNAMIC_TYPE: |
|
| 166 | 2 | $this->compileDynamicPageViews($pageView); |
|
|
1 ignored issue
–
show
|
|||
| 167 | 2 | $this->compileStandardRedirects($pageView); |
|
| 168 | 2 | break; |
|
| 169 | |||
| 170 | 2 | case PageView::REPEATER_TYPE: |
|
| 171 | 2 | $this->compileRepeaterPageViews($pageView); |
|
|
1 ignored issue
–
show
|
|||
| 172 | 2 | $this->compileExpandedRedirects($pageView); |
|
| 173 | 2 | break; |
|
| 174 | 14 | } |
|
| 175 | 14 | } |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Write the compiled output for a static PageView. |
||
| 179 | * |
||
| 180 | * @param PageView $pageView |
||
| 181 | * |
||
| 182 | * @since 0.1.1 |
||
| 183 | */ |
||
| 184 | 10 | private function compileStaticPageView(&$pageView) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * Write the compiled output for a dynamic PageView. |
||
| 195 | * |
||
| 196 | * @param DynamicPageView $pageView |
||
| 197 | * |
||
| 198 | * @since 0.1.1 |
||
| 199 | */ |
||
| 200 | 2 | private function compileDynamicPageViews(&$pageView) |
|
| 201 | { |
||
| 202 | 2 | $contentItems = $pageView->getContentItems(); |
|
| 203 | 2 | $template = $this->createTwigTemplate($pageView); |
|
| 204 | |||
| 205 | 2 | foreach ($contentItems as &$contentItem) |
|
| 206 | { |
||
| 207 | 2 | if ($contentItem->isDraft() && !Service::getParameter(BuildableCommand::USE_DRAFTS)) |
|
| 208 | 2 | { |
|
| 209 | 1 | $this->output->debug('{file}: marked as a draft', array( |
|
| 210 | 1 | 'file' => $contentItem->getRelativeFilePath() |
|
| 211 | 1 | )); |
|
| 212 | |||
| 213 | 2 | continue; |
|
| 214 | } |
||
| 215 | |||
| 216 | 2 | $targetFile = $contentItem->getTargetFile(); |
|
| 217 | 2 | $output = $this->renderDynamicPageView($template, $pageView, $contentItem); |
|
| 218 | |||
| 219 | 2 | $this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
|
| 220 | 2 | $this->folder->writeFile($targetFile, $output); |
|
| 221 | 2 | } |
|
| 222 | 2 | } |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Write the compiled output for a repeater PageView. |
||
| 226 | * |
||
| 227 | * @param RepeaterPageView $pageView |
||
| 228 | * |
||
| 229 | * @since 0.1.1 |
||
| 230 | */ |
||
| 231 | 2 | View Code Duplication | private function compileRepeaterPageViews(&$pageView) |
| 232 | { |
||
| 233 | 2 | $pageView->rewindPermalink(); |
|
| 234 | |||
| 235 | 2 | $template = $this->createTwigTemplate($pageView); |
|
| 236 | 2 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
| 237 | |||
| 238 | 2 | foreach ($permalinks as $permalink) |
|
| 239 | { |
||
| 240 | 2 | $pageView->bumpPermalink(); |
|
| 241 | 2 | $targetFile = $pageView->getTargetFile(); |
|
| 242 | 2 | $output = $this->renderRepeaterPageView($template, $pageView, $permalink); |
|
| 243 | |||
| 244 | 2 | $this->output->notice('Writing repeater file: {file}', array('file' => $targetFile)); |
|
| 245 | 2 | $this->folder->writeFile($targetFile, $output); |
|
| 246 | 2 | } |
|
| 247 | 2 | } |
|
| 248 | |||
| 249 | /** |
||
| 250 | * @deprecated |
||
| 251 | * |
||
| 252 | * @todo This function needs to be rewritten or removed. Something |
||
| 253 | * |
||
| 254 | * @param ContentItem $contentItem |
||
| 255 | */ |
||
| 256 | View Code Duplication | public function compileContentItem(&$contentItem) |
|
| 269 | |||
| 270 | /// |
||
| 271 | // Redirect handling |
||
| 272 | /// |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Write redirects for standard redirects. |
||
| 276 | * |
||
| 277 | * @param PageView $pageView |
||
| 278 | * |
||
| 279 | * @since 0.1.1 |
||
| 280 | */ |
||
| 281 | 12 | private function compileStandardRedirects(&$pageView) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Write redirects for expanded redirects. |
||
| 299 | * |
||
| 300 | * @param RepeaterPageView $pageView |
||
| 301 | * |
||
| 302 | * @since 0.1.1 |
||
| 303 | */ |
||
| 304 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 326 | |||
| 327 | /// |
||
| 328 | // Twig Functionality |
||
| 329 | /// |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 333 | * |
||
| 334 | * @param Twig_Template $template |
||
| 335 | * @param PageView $pageView |
||
| 336 | * @param ExpandedValue $expandedValue |
||
| 337 | * |
||
| 338 | * @since 0.1.1 |
||
| 339 | * |
||
| 340 | * @return string |
||
| 341 | */ |
||
| 342 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Get the compiled HTML for a specific ContentItem. |
||
| 359 | * |
||
| 360 | * @param Twig_Template $template |
||
| 361 | * @param PageView $pageView |
||
| 362 | * @param ContentItem $contentItem |
||
| 363 | * |
||
| 364 | * @since 0.1.1 |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 2 | private function renderDynamicPageView(&$template, &$pageView, &$contentItem) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Get the compiled HTML for a static PageView. |
||
| 380 | * |
||
| 381 | * @param PageView $pageView |
||
| 382 | * |
||
| 383 | * @since 0.1.1 |
||
| 384 | * |
||
| 385 | * @throws \Exception |
||
| 386 | * @throws \Throwable |
||
| 387 | * @throws Twig_Error_Syntax |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 10 | private function renderStaticPageView(&$pageView) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Create a Twig template that just needs an array to render. |
||
| 404 | * |
||
| 405 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 406 | * |
||
| 407 | * @since 0.1.1 |
||
| 408 | * |
||
| 409 | * @throws \Exception |
||
| 410 | * @throws \Throwable |
||
| 411 | * @throws Twig_Error_Syntax |
||
| 412 | * |
||
| 413 | * @return Twig_Template |
||
| 414 | */ |
||
| 415 | 14 | private function createTwigTemplate(&$pageView) |
|
| 448 | } |
||
| 449 |