| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright 2017 Vladimir Jimenez | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | namespace allejo\stakx\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use allejo\stakx\Exception\FileAwareException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use allejo\stakx\Exception\TrackedItemNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use allejo\stakx\FrontMatter\ExpandedValue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use allejo\stakx\Object\ContentItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use allejo\stakx\Object\DynamicPageView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use allejo\stakx\Object\JailObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use allejo\stakx\Object\PageView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use allejo\stakx\Object\RepeaterPageView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use allejo\stakx\System\FileExplorer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use allejo\stakx\System\Folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Twig_Error_Syntax; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Twig_Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * This class is responsible for handling all of the PageViews within a website. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * PageManager will parse all available dynamic and static PageViews. After, dynamic PageViews will be prepared by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * setting the appropriate values for each ContentItem such as permalinks. Lastly, this class will compile all of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * PageViews and write them to the target directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * @package allejo\stakx\Manager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | class PageManager extends TrackingManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * The relative (to the stakx project) file path to the redirect template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @var string|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     private $redirectTemplate; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @var PageView[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     private $twigExtendsDeps; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @var ContentItem[][] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private $collections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @var Folder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     private $targetDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @var PageView[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     private $flatPages; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     private $twigOpts; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @var \Twig_Environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private $twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * PageManager constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |         $this->redirectTemplate = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 2 |  |         $this->twigExtendsDeps = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 2 |  |         $this->collections = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 2 |  |         $this->flatPages = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * Give this manager the collections we'll be using for dynamic PageViews | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param ContentItem[][] $collections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |     public function setCollections (&$collections) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 2 |  |         $this->collections = &$collections; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Set the template used for redirects | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param false|string $filePath The path to the redirect template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function setRedirectTemplate ($filePath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->redirectTemplate = $filePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * The location where the compiled website will be written to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param Folder $folder The relative target directory as specified from the configuration file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |     public function setTargetFolder (&$folder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         $this->targetDir = &$folder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 2 |  |     public function configureTwig ($configuration, $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 2 |  |         $this->twigOpts['configuration'] = $configuration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |         $this->twigOpts['options']       = $options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 2 |  |         $this->createTwigManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |     public function getStaticPages () | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |         return $this->flatPages; | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 127 | 1 | View Code Duplication |     public function getJailedStaticPages () | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 1 |  |         $jailedObjects = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 1 |  |         foreach ($this->flatPages as $key => $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             // If it's an array, it means the parent is hidden from the site menu therefore its children should be too | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 1 |  |             if (is_array($value)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 1 |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  |             $jailedObjects[$key] = $value->createJail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 1 |  |         return $jailedObjects; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * Go through all of the PageView directories and create a respective PageView for each and classify them as a | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * dynamic or static PageView. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param $pageViewFolders | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 2 |  |     public function parsePageViews ($pageViewFolders) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 2 |  |         if (empty($pageViewFolders)) { return; } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |          * The name of the folder where PageViews are located | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |          * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |          * @var $pageViewFolder string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 2 |  |         foreach ($pageViewFolders as $pageViewFolderName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 2 |  |             $pageViewFolder = $this->fs->absolutePath($pageViewFolderName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 | 2 |  |             if (!$this->fs->exists($pageViewFolder)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 | 2 |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 2 |  |             $this->scanTrackableItems($pageViewFolder, array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                 'fileExplorer' => FileExplorer::INCLUDE_ONLY_FILES | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 | 2 |  |             ), array('/.html$/', '/.twig$/')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 2 |  |             $this->saveFolderDefinition($pageViewFolderName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * Compile dynamic and static PageViews | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 2 |  |     public function compileAll () | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 2 |  |         foreach (array_keys($this->trackedItemsFlattened) as $filePath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 2 |  |             $this->compileFromFilePath($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     public function compileSome ($filter = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         /** @var PageView $pageView */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         foreach ($this->trackedItemsFlattened as $pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             if ($pageView->hasTwigDependency($filter['namespace'], $filter['dependency'])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |                 $this->compilePageView($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @param ContentItem $contentItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     public function compileContentItem (&$contentItem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         $pageView = $contentItem->getPageView(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |         // This ContentItem doesn't have an individual PageView dedicated to displaying this item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         if (is_null($pageView)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |         $template = $this->createTemplate($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |         $contentItem->evaluateFrontMatter( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |             $pageView->getFrontMatter(false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |         $output = $template->render(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 |  |  |             'this' => $contentItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |         $this->targetDir->writeFile($contentItem->getTargetFile(), $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      * Add a new ContentItem to the respective parent PageView of the ContentItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |      * @param ContentItem $contentItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     public function updatePageView ($contentItem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 231 |  |  |         /** @var DynamicPageView $pageView */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 232 |  |  |         foreach ($this->trackedItems['dynamic'] as &$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 233 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 234 |  |  |             $fm = $pageView->getFrontMatter(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 235 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 236 |  |  |             if ($fm['collection'] == $contentItem->getCollection()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 237 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 238 |  |  |                 $pageView->addContentItem($contentItem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 239 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 240 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 241 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 242 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 243 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 244 |  |  |      * Update an existing Twig variable that's injected globally | 
            
                                                                                                            
                            
            
                                    
            
            
                | 245 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 246 |  |  |      * @param string $variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 247 |  |  |      * @param string $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 248 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 249 |  |  |     public function updateTwigVariable ($variable, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 250 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 251 |  |  |         $this->twig->addGlobal($variable, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 252 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 253 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 254 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 255 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 256 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 257 | 2 |  |     public function isTracked($filePath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 258 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 259 | 2 |  |         return (parent::isTracked($filePath) || isset($this->twigExtendsDeps[$filePath])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 260 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 261 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 262 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 263 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 264 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 265 | 2 |  |     public function refreshItem($filePath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 266 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 267 |  |  |         if (parent::isTracked($filePath)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 268 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 269 |  |  |             $this->compileFromFilePath($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 270 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 271 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 272 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 273 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 274 |  |  |         $this->createTwigManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 275 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 276 | 2 |  |         foreach ($this->twigExtendsDeps[$filePath] as $pageView) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 277 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 278 |  |  |             $this->compilePageView($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 279 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 280 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 281 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 282 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 283 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 284 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 285 | 2 |  |     protected function handleTrackableItem($filePath, $options = array()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 286 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 287 | 2 |  |         $pageView  = PageView::create($filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 288 | 2 |  |         $namespace = $pageView->getType(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 289 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 290 |  |  |         switch ($namespace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 291 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 292 | 2 |  |             case PageView::DYNAMIC_TYPE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 293 | 2 |  |                 $this->handleTrackableDynamicPageView($pageView); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 294 | 2 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 295 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 296 | 2 |  |             case PageView::STATIC_TYPE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 297 | 2 |  |                 $this->handleTrackableStaticPageView($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 298 | 2 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 299 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 300 | 2 |  |             default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 301 | 2 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 302 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 303 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 304 | 2 |  |         $this->addObjectToTracker($pageView, $pageView->getRelativeFilePath(), $namespace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 305 | 2 |  |         $this->saveTrackerOptions($pageView->getRelativeFilePath(), array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 306 |  |  |             'viewType' => $namespace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 307 | 2 |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 308 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 309 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 310 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 311 |  |  |      * @param DynamicPageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 312 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 313 | 2 |  |     private function handleTrackableDynamicPageView ($pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 314 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 315 | 2 |  |         $frontMatter = $pageView->getFrontMatter(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 316 | 2 |  |         $collection = $frontMatter['collection']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 317 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 318 | 2 |  |         if (!isset($this->collections[$collection])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 319 | 2 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 320 |  |  |             throw new \RuntimeException("The '$collection' collection is not defined"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 321 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 322 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 323 | 2 |  |         foreach ($this->collections[$collection] as &$item) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 324 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 325 | 2 |  |             $item->evaluateFrontMatter($frontMatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 326 | 2 |  |             $pageView->addContentItem($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 327 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 328 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 329 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 330 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 331 |  |  |      * @param PageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 332 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 333 | 2 |  |     private function handleTrackableStaticPageView ($pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 334 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 335 | 2 |  |         if (empty($pageView['title'])) { return; } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 336 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 337 | 2 |  |         $this->flatPages[$pageView['title']] = $pageView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 338 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 339 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 340 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 341 |  |  |      * Create a Twig environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 342 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 343 | 2 |  |     private function createTwigManager () | 
            
                                                                                                            
                            
            
                                    
            
            
                | 344 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 345 | 2 |  |         $twig = new TwigManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 346 | 2 |  |         $twig->configureTwig( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 347 | 2 |  |             $this->twigOpts['configuration'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 348 | 2 |  |             $this->twigOpts['options'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 349 | 2 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 350 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 351 | 2 |  |         $this->twig = TwigManager::getInstance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 352 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 353 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 354 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 355 |  |  |      * Compile a given PageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 356 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 357 |  |  |      * @param string $filePath The file path to the PageView to compile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 358 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 359 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 360 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 361 | 2 |  |     private function compileFromFilePath ($filePath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 362 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 363 | 2 |  |         if (!$this->isTracked($filePath)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 364 | 2 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 365 |  |  |             throw new TrackedItemNotFoundException('PageView not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 366 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 367 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 368 |  |  |         /** @var DynamicPageView|PageView|RepeaterPageView $pageView */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 369 | 2 |  |         $pageView = &$this->trackedItemsFlattened[$filePath]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 370 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 371 |  |  |         try | 
            
                                                                                                            
                            
            
                                    
            
            
                | 372 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 373 | 2 |  |             $pageView->refreshFileContent(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 374 | 2 |  |             $this->compilePageView($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 375 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 376 | 2 |  |         catch (\Exception $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 377 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 378 |  |  |             throw FileAwareException::castException($e, $filePath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 379 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 380 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 381 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 382 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 383 |  |  |      * @param DynamicPageView|RepeaterPageView|PageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 384 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 385 | 2 |  |     private function compilePageView ($pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 386 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 387 | 2 |  |         switch ($pageView->getType()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 388 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 389 | 2 |  |             case PageView::REPEATER_TYPE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 390 | 2 |  |                 $this->compileRepeaterPageView($pageView); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 391 | 2 |  |                 $this->compileExpandedRedirects($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 392 | 2 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 393 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 394 | 2 |  |             case PageView::DYNAMIC_TYPE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 395 | 2 |  |                 $this->compileDynamicPageView($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 396 | 2 |  |                 $this->compileNormalRedirects($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 397 | 2 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 398 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 399 | 2 |  |             case PageView::STATIC_TYPE: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 400 | 2 |  |                 $this->compileStaticPageView($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 401 | 2 |  |                 $this->compileNormalRedirects($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 402 | 2 |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 403 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 404 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 405 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 406 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 407 |  |  |      * @param RepeaterPageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 408 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 409 | 2 |  |     private function compileRepeaterPageView (&$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 410 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 411 | 2 |  |         $template = $this->createTemplate($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 412 | 2 |  |         $pageView->rewindPermalink(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 413 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 414 | 2 |  |         foreach ($pageView->getRepeaterPermalinks() as $permalink) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 415 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 416 | 2 |  |             $pageView->bumpPermalink(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 417 | 2 |  |             $pageView->setFrontMatter(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 418 | 2 |  |                 'permalink' => $permalink->getEvaluated(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 419 | 2 |  |                 'iterators' => $permalink->getIterators() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 420 | 2 |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 421 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 422 | 2 |  |             $output = $template->render(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 423 | 2 |  |                 'this' => $pageView->createJail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 424 | 2 |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 425 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 426 | 2 |  |             $this->output->notice("Writing repeater file: {file}", array('file' => $pageView->getTargetFile())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 427 | 2 |  |             $this->targetDir->writeFile($pageView->getTargetFile(), $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 428 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 429 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 430 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 431 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 432 |  |  |      * @param PageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 433 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 434 | 2 |  |     private function compileDynamicPageView (&$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 435 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 436 | 2 |  |         $template = $this->createTemplate($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 437 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 438 | 2 |  |         $pageViewFrontMatter = $pageView->getFrontMatter(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 439 | 2 |  |         $collection = $pageViewFrontMatter['collection']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 440 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 441 | 2 |  |         if (!isset($this->collections[$collection])) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 442 | 2 |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 443 |  |  |             throw new \RuntimeException("The '$collection' collection is not defined"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 444 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 445 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 446 |  |  |         /** @var ContentItem $contentItem */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 447 | 2 |  |         foreach ($this->collections[$collection] as &$contentItem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 448 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 449 | 2 |  |             $output = $template->render(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 450 | 2 |  |                 'this' => $contentItem->createJail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 451 | 2 |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 452 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 453 | 2 |  |             $this->output->notice("Writing file: {file}", array('file' => $contentItem->getTargetFile())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 454 | 2 |  |             $this->targetDir->writeFile($contentItem->getTargetFile(), $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 455 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 456 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 457 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 458 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 459 |  |  |      * @param PageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 460 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 461 | 2 |  |     private function compileStaticPageView (&$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 462 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 463 | 2 |  |         $this->twig->addGlobal('__currentTemplate', $pageView->getFilePath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 464 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 465 | 2 |  |         $template = $this->createTemplate($pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 466 | 2 |  |         $output = $template->render(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 467 | 2 |  |             'this' => $pageView->createJail() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 468 | 2 |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 469 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 470 | 2 |  |         $this->output->notice("Writing file: {file}", array('file' => $pageView->getTargetFile())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 471 | 2 |  |         $this->targetDir->writeFile($pageView->getTargetFile(), $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 472 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 473 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 474 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 475 |  |  |      * @param DynamicPageView|PageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 476 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 477 | 2 |  |     private function compileNormalRedirects (&$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 478 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 479 | 2 |  |         foreach ($pageView->getRedirects() as $redirect) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 480 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 481 |  |  |             $redirectPageView = PageView::createRedirect( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 482 |  |  |                 $redirect, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 483 |  |  |                 $pageView->getPermalink(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 484 |  |  |                 $this->redirectTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 485 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 486 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 487 |  |  |             $this->compilePageView($redirectPageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 488 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 489 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 490 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 491 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 492 |  |  |      * @param RepeaterPageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 493 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 494 | 2 |  |     private function compileExpandedRedirects (&$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 495 | 1 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 496 | 2 |  |         $permalinks = $pageView->getRepeaterPermalinks(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 497 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 498 |  |  |         /** @var ExpandedValue[] $repeaterRedirect */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 499 | 2 |  |         foreach ($pageView->getRepeaterRedirects() as $repeaterRedirect) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 500 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 501 |  |  |             /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 502 |  |  |              * @var int           $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 503 |  |  |              * @var ExpandedValue $redirect | 
            
                                                                                                            
                            
            
                                    
            
            
                | 504 |  |  |              */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 505 |  |  |             foreach ($repeaterRedirect as $index => $redirect) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 506 |  |  |             { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 507 |  |  |                 $redirectPageView = PageView::createRedirect( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 508 |  |  |                     $redirect->getEvaluated(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 509 |  |  |                     $permalinks[$index]->getEvaluated(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 510 |  |  |                     $this->redirectTemplate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 511 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 512 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 513 |  |  |                 $this->compilePageView($redirectPageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 514 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 515 | 2 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 516 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 517 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 518 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 519 |  |  |      * @param PageView $pageView | 
            
                                                                                                            
                            
            
                                    
            
            
                | 520 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 521 |  |  |      * @return Twig_Template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 522 |  |  |      * @throws Twig_Error_Syntax | 
            
                                                                                                            
                            
            
                                    
            
            
                | 523 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 524 | 2 |  |     private function createTemplate (&$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 525 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 526 |  |  |         try | 
            
                                                                                                            
                            
            
                                    
            
            
                | 527 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 528 | 2 |  |             $template = $this->twig->createTemplate($pageView->getContent()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 529 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 530 | 2 |  |             $this->trackParentTwigTemplate($template, $pageView); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 531 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 532 | 2 |  |             return $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 533 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 534 |  |  |         catch (Twig_Error_Syntax $e) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 535 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 536 |  |  |             $e->setTemplateLine($e->getTemplateLine() + $pageView->getLineOffset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 537 |  |  |             $e->setTemplateName($pageView->getRelativeFilePath()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 538 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 539 |  |  |             throw $e; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 540 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 541 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 542 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 543 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 544 |  |  |      * Find the parent Twig templates of the given template and keep a list of it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 545 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 546 |  |  |      * @param Twig_Template $template The template created from the PageView's content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 547 |  |  |      * @param PageView      $pageView The PageView that has this content. Used to keep a reference of PageViews | 
            
                                                                                                            
                            
            
                                    
            
            
                | 548 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 549 | 2 |  |     private function trackParentTwigTemplate ($template, &$pageView) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 550 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 551 | 2 |  |         if (!$this->tracking) { return; } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 552 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 553 |  |  |         /** @var Twig_Template $parent */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 554 |  |  |         $parent = $template->getParent(array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 555 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 556 |  |  |         while ($parent !== false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 557 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 558 |  |  |             $filePath = $this->fs->getRelativePath($parent->getSourceContext()->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 559 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 560 |  |  |             $this->twigExtendsDeps[$filePath][(string)$pageView->getFilePath()] = &$pageView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 561 |  |  |             $parent = $parent->getParent(array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 562 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 563 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 564 |  |  | } | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.