| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * View Templating System | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * Copyright (c) 2008-2015 James Ekow Abaka Ainooson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Permission is hereby granted, free of charge, to any person obtaining | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * a copy of this software and associated documentation files (the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * "Software"), to deal in the Software without restriction, including | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * without limitation the rights to use, copy, modify, merge, publish, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * distribute, sublicense, and/or sell copies of the Software, and to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * permit persons to whom the Software is furnished to do so, subject to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * The above copyright notice and this permission notice shall be | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * included in all copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | namespace ntentan\honam; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use ntentan\honam\exceptions\TemplateEngineNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use ntentan\honam\exceptions\TemplateResolutionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * The TemplateEngine class does the work of resolving templates, loading template files, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  * loading template engines and rendering templates. The `ntentan/views` package takes a reference to a  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  * template and tries to find a specific template file to be used for the rendering.  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  * `ntentan/views` does not expect to find all view templates in a single directory. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  * It uses a heirachy of directories which it searches for the template to render. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * Templates in directories closer to the beginning of the array have a higher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  * priority over those closer to the end of the array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | class TemplateRenderer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * An array of loaded template engine instances. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @var array<\ntentan\honam\TemplateEngine> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     private $loadedInstances; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * Instance of the engine registry for loading template engines. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @var EngineRegistry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     private $registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Instance of the template file resolver for finding template files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @var TemplateFileResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     private $templateFileResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     private $tempDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * TemplateRenderer constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param EngineRegistry $registry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param TemplateFileResolver $templateFileResolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     public function __construct(EngineRegistry $registry, TemplateFileResolver $templateFileResolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $this->registry = $registry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->templateFileResolver = $templateFileResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->tempDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "honam_temp"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $registry->setTemplateRenderer($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * @param $templateFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return string|null | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @throws TemplateEngineNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     private function getTemplateExtension($templateFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $supportedExtensions = $this->registry->getSupportedExtensions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         foreach($supportedExtensions as $extension) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             if($extension == substr($templateFile, -strlen($extension))) return $extension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         throw new TemplateEngineNotFoundException("There are currently no registered engines that can render $templateFile"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Check if an engine exists that can render the given file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * @param $templateFile | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @throws TemplateEngineNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     public function canRender($templateFile) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         return $this->getTemplateExtension($templateFile) !== null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Load and cache an instance of the a template engine. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @param $extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * @throws exceptions\TemplateEngineNotFoundException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     private function loadEngine($extension) | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |         if(!isset($this->loadedInstances[$extension])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             $this->loadedInstances[$extension] = $this->registry->getEngineInstance($extension); | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         return $this->loadedInstances[$extension]; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function setTempDirectory(string $tempDirectory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->tempDirectory = $tempDirectory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      * Renders a given template reference with associated template data. This render | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |      * function combs through the template directory heirachy to find a template | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |      * file which matches the given template reference and uses it for the purpose | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |      * of rendering the view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @param string $template The template reference file. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param bool $fromString | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param null $extension | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @throws TemplateResolutionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @throws exceptions\TemplateEngineNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function render($template, $data, $fromString = false, $extension=null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         if($fromString) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             $engine = $this->loadEngine($extension); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |             return $engine->renderFromFileTemplate($template, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             $templateFile = $this->templateFileResolver->resolveTemplateFile($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             $engine = $this->loadEngine($this->getTemplateExtension($templateFile)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             return $engine->renderFromFileTemplate($templateFile, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 151 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 152 |  |  |  | 
            
                        
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.