| 1 | <?php |
||
| 35 | class Compiler extends BaseManager |
||
| 36 | { |
||
| 37 | /** @var string|false */ |
||
| 38 | private $redirectTemplate; |
||
| 39 | |||
| 40 | /** @var Twig_Template[] */ |
||
| 41 | private $templateDependencies; |
||
| 42 | |||
| 43 | /** @var PageView[] */ |
||
| 44 | private $pageViewsFlattened; |
||
| 45 | |||
| 46 | /** @var string[] */ |
||
| 47 | private $templateMapping; |
||
| 48 | |||
| 49 | /** @var PageView[][] */ |
||
| 50 | private $pageViews; |
||
| 51 | |||
| 52 | /** @var Folder */ |
||
| 53 | private $folder; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | private $theme; |
||
| 57 | |||
| 58 | /** @var Twig_Environment */ |
||
| 59 | private $twig; |
||
| 60 | |||
| 61 | 14 | public function __construct() |
|
| 68 | |||
| 69 | /** |
||
| 70 | * @param string|false $template |
||
| 71 | */ |
||
| 72 | public function setRedirectTemplate($template) |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param Folder $folder |
||
| 79 | */ |
||
| 80 | 14 | public function setTargetFolder(Folder $folder) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param PageView[][] $pageViews |
||
| 87 | * @param PageView[] $pageViewsFlattened |
||
| 88 | */ |
||
| 89 | 14 | public function setPageViews(array &$pageViews, array &$pageViewsFlattened) |
|
| 94 | |||
| 95 | public function setThemeName($themeName) |
||
| 99 | |||
| 100 | /// |
||
| 101 | // Twig parent templates |
||
| 102 | /// |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Check whether a given file path is used as a parent template by a PageView |
||
| 106 | * |
||
| 107 | * @param string $filePath |
||
| 108 | * |
||
| 109 | * @return bool |
||
| 110 | */ |
||
| 111 | public function isParentTemplate($filePath) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Rebuild all of the PageViews that used a given template as a parent |
||
| 118 | * |
||
| 119 | * @param string $filePath The file path to the parent Twig template |
||
| 120 | */ |
||
| 121 | 1 | public function refreshParent($filePath) |
|
| 128 | |||
| 129 | public function getTemplateMappings() |
||
| 133 | |||
| 134 | /// |
||
| 135 | // IO Functionality |
||
| 136 | /// |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Compile all of the PageViews registered with the compiler. |
||
| 140 | * |
||
| 141 | * @since 0.1.0 |
||
| 142 | */ |
||
| 143 | 14 | public function compileAll() |
|
| 150 | |||
| 151 | public function compileSome($filter = array()) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Compile an individual PageView item. |
||
| 165 | * |
||
| 166 | * This function will take care of determining *how* to treat the PageView and write the compiled output to a the |
||
| 167 | * respective target file. |
||
| 168 | * |
||
| 169 | * @param DynamicPageView|RepeaterPageView|PageView $pageView The PageView that needs to be compiled |
||
| 170 | * |
||
| 171 | * @since 0.1.1 |
||
| 172 | */ |
||
| 173 | 14 | public function compilePageView(&$pageView) |
|
| 174 | { |
||
| 175 | 14 | $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); |
|
| 176 | 14 | $this->output->debug('Compiling {type} PageView: {pageview}', array( |
|
| 177 | 14 | 'pageview' => $pageView->getRelativeFilePath(), |
|
| 178 | 14 | 'type' => $pageView->getType() |
|
| 179 | 14 | )); |
|
| 180 | |||
| 181 | try |
||
| 182 | { |
||
| 183 | 14 | switch ($pageView->getType()) |
|
| 184 | { |
||
| 185 | 14 | case PageView::STATIC_TYPE: |
|
| 186 | 10 | $this->compileStaticPageView($pageView); |
|
| 187 | 10 | $this->compileStandardRedirects($pageView); |
|
| 188 | 10 | break; |
|
| 189 | |||
| 190 | 4 | case PageView::DYNAMIC_TYPE: |
|
| 191 | 2 | $this->compileDynamicPageViews($pageView); |
|
|
1 ignored issue
–
show
|
|||
| 192 | 2 | $this->compileStandardRedirects($pageView); |
|
| 193 | 2 | break; |
|
| 194 | |||
| 195 | 2 | case PageView::REPEATER_TYPE: |
|
| 196 | 2 | $this->compileRepeaterPageViews($pageView); |
|
|
1 ignored issue
–
show
|
|||
| 197 | 2 | $this->compileExpandedRedirects($pageView); |
|
| 198 | 2 | break; |
|
| 199 | 14 | } |
|
| 200 | } |
||
| 201 | 14 | catch (Twig_Error_Runtime $e) |
|
| 202 | { |
||
| 203 | throw new FileAwareException( |
||
| 204 | $e->getRawMessage(), |
||
| 205 | $e->getCode(), |
||
| 206 | $e, |
||
| 207 | $pageView->getRelativeFilePath(), |
||
| 208 | $e->getTemplateLine() + $pageView->getLineOffset() |
||
| 209 | ); |
||
| 210 | } |
||
| 211 | 14 | } |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Write the compiled output for a static PageView. |
||
| 215 | * |
||
| 216 | * @param PageView $pageView |
||
| 217 | * |
||
| 218 | * @since 0.1.1 |
||
| 219 | */ |
||
| 220 | 10 | private function compileStaticPageView(&$pageView) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Write the compiled output for a dynamic PageView. |
||
| 231 | * |
||
| 232 | * @param DynamicPageView $pageView |
||
| 233 | * |
||
| 234 | * @since 0.1.1 |
||
| 235 | */ |
||
| 236 | 2 | private function compileDynamicPageViews(&$pageView) |
|
| 237 | { |
||
| 238 | 2 | $contentItems = $pageView->getRepeatableItems(); |
|
| 239 | 2 | $template = $this->createTwigTemplate($pageView); |
|
| 240 | |||
| 241 | 2 | foreach ($contentItems as &$contentItem) |
|
| 242 | { |
||
| 243 | 2 | if ($contentItem->isDraft() && !Service::getParameter(BuildableCommand::USE_DRAFTS)) |
|
| 244 | 2 | { |
|
| 245 | 1 | $this->output->debug('{file}: marked as a draft', array( |
|
| 246 | 1 | 'file' => $contentItem->getRelativeFilePath() |
|
| 247 | 1 | )); |
|
| 248 | |||
| 249 | 1 | continue; |
|
| 250 | } |
||
| 251 | |||
| 252 | 2 | $targetFile = $contentItem->getTargetFile(); |
|
| 253 | 2 | $output = $this->renderDynamicPageView($template, $contentItem); |
|
| 254 | |||
| 255 | 2 | $this->output->notice('Writing file: {file}', array('file' => $targetFile)); |
|
| 256 | 2 | $this->folder->writeFile($targetFile, $output); |
|
| 257 | 2 | } |
|
| 258 | 2 | } |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Write the compiled output for a repeater PageView. |
||
| 262 | * |
||
| 263 | * @param RepeaterPageView $pageView |
||
| 264 | * |
||
| 265 | * @since 0.1.1 |
||
| 266 | */ |
||
| 267 | 2 | private function compileRepeaterPageViews(&$pageView) |
|
| 268 | { |
||
| 269 | 2 | $pageView->rewindPermalink(); |
|
| 270 | |||
| 271 | 2 | $template = $this->createTwigTemplate($pageView); |
|
| 272 | 2 | $permalinks = $pageView->getRepeaterPermalinks(); |
|
| 273 | |||
| 274 | 2 | foreach ($permalinks as $permalink) |
|
| 275 | { |
||
| 276 | 2 | $pageView->bumpPermalink(); |
|
| 277 | 2 | $targetFile = $pageView->getTargetFile(); |
|
| 278 | 2 | $output = $this->renderRepeaterPageView($template, $pageView, $permalink); |
|
| 279 | |||
| 280 | 2 | $this->output->notice('Writing repeater file: {file}', array('file' => $targetFile)); |
|
| 281 | 2 | $this->folder->writeFile($targetFile, $output); |
|
| 282 | 2 | } |
|
| 283 | 2 | } |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @deprecated |
||
| 287 | * |
||
| 288 | * @todo This function needs to be rewritten or removed. Something |
||
| 289 | * |
||
| 290 | * @param ContentItem $contentItem |
||
| 291 | */ |
||
| 292 | public function compileContentItem(&$contentItem) |
||
| 306 | |||
| 307 | /// |
||
| 308 | // Redirect handling |
||
| 309 | /// |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Write redirects for standard redirects. |
||
| 313 | * |
||
| 314 | * @param PageView $pageView |
||
| 315 | * |
||
| 316 | * @since 0.1.1 |
||
| 317 | */ |
||
| 318 | 12 | private function compileStandardRedirects(&$pageView) |
|
| 333 | |||
| 334 | /** |
||
| 335 | * Write redirects for expanded redirects. |
||
| 336 | * |
||
| 337 | * @param RepeaterPageView $pageView |
||
| 338 | * |
||
| 339 | * @since 0.1.1 |
||
| 340 | */ |
||
| 341 | 2 | private function compileExpandedRedirects(&$pageView) |
|
| 363 | |||
| 364 | /// |
||
| 365 | // Twig Functionality |
||
| 366 | /// |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get the compiled HTML for a specific iteration of a repeater PageView. |
||
| 370 | * |
||
| 371 | * @param Twig_Template $template |
||
| 372 | * @param PageView $pageView |
||
| 373 | * @param ExpandedValue $expandedValue |
||
| 374 | * |
||
| 375 | * @since 0.1.1 |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | 2 | private function renderRepeaterPageView(&$template, &$pageView, &$expandedValue) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Get the compiled HTML for a specific ContentItem. |
||
| 394 | * |
||
| 395 | * @param Twig_Template $template |
||
| 396 | * @param ContentItem $contentItem |
||
| 397 | * |
||
| 398 | * @since 0.1.1 |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | 2 | private function renderDynamicPageView(&$template, &$contentItem) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Get the compiled HTML for a static PageView. |
||
| 412 | * |
||
| 413 | * @param PageView $pageView |
||
| 414 | * |
||
| 415 | * @since 0.1.1 |
||
| 416 | * |
||
| 417 | * @throws \Exception |
||
| 418 | * @throws \Throwable |
||
| 419 | * @throws Twig_Error_Syntax |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | 10 | private function renderStaticPageView(&$pageView) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Create a Twig template that just needs an array to render. |
||
| 434 | * |
||
| 435 | * @param PageView $pageView The PageView whose body will be used for Twig compilation |
||
| 436 | * |
||
| 437 | * @since 0.1.1 |
||
| 438 | * |
||
| 439 | * @throws \Exception |
||
| 440 | * @throws \Throwable |
||
| 441 | * @throws Twig_Error_Syntax |
||
| 442 | * |
||
| 443 | * @return Twig_Template |
||
| 444 | */ |
||
| 445 | 14 | private function createTwigTemplate(&$pageView) |
|
| 480 | } |
||
| 481 |