| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of TwigView. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  ** (c) 2014 Cees-Jan Kiewiet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace WyriHaximus\TwigView\Lib\Twig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Cake\Core\App; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Cake\Core\Plugin; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use WyriHaximus\TwigView\View\TwigView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * Class Loader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @package WyriHaximus\TwigView\Lib\Twig | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class Loader implements \Twig_LoaderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Get the file contents of a template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param string $name Template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 | 1 |  |     public function getSource($name) | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 | 1 |  |         $name = $this->resolveFileName($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         return file_get_contents($name); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Get cache key for template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param string $name Template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |     public function getCacheKey($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |         return $this->resolveFileName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Check if template is still fresh. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string  $name Template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param integer $time Timestamp. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 1 |  |     public function isFresh($name, $time) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |         $name = $this->resolveFileName($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         return filemtime($name) < $time; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * Resolve template name to filename. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param string $name Template. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @throws \Twig_Error_Loader Thrown when template file isn't found. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     // @codingStandardsIgnoreStart | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 4 |  |     protected function resolveFileName($name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         // @codingStandardsIgnoreEnd | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (file_exists($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return $name; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         list($plugin, $file) = pluginSplit($name); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         foreach ([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 4 |  |             null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $plugin, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |         ] as $scope) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $paths = $this->getPaths($scope); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             foreach ($paths as $path) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |                 $filePath = $path . $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 if (file_exists($filePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 2 |  |                     return $filePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |                 $filePath = $path . $file . TwigView::EXT; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 if (file_exists($filePath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  |                     return $filePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         throw new \Twig_Error_Loader(sprintf('Template "%s" is not defined.', $name)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * Check if $plugin is active and return it's template paths or return the aps template paths. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param string|null $plugin The plugin in question. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |     protected function getPaths($plugin) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |         if ($plugin === null || !Plugin::loaded($plugin)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             return App::path('Template'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return App::path('Template', $plugin); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 118 |  |  |  |