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\TemplateResolutionException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The TemplateEngine class does the work of resolving templates, loading template files, |
33
|
|
|
* loading template engines and rendering templates. The `ntentan/views` package takes a reference to a |
34
|
|
|
* template and tries to find a specific template file to be used for the rendering. |
35
|
|
|
* `ntentan/views` does not expect to find all view templates in a single directory. |
36
|
|
|
* It uses a heirachy of directories which it searches for the template to render. |
37
|
|
|
* Templates in directories closer to the beginning of the array have a higher |
38
|
|
|
* priority over those closer to the end of the array. |
39
|
|
|
* |
40
|
|
|
*/ |
41
|
|
|
class TemplateRenderer |
42
|
|
|
{ |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* An array of loaded template engine instances. |
46
|
|
|
* @var array<\ntentan\honam\TemplateEngine> |
47
|
|
|
*/ |
48
|
|
|
private $loadedInstances; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Instance of the engine registry for loading template engines. |
52
|
|
|
* @var EngineRegistry |
53
|
|
|
*/ |
54
|
|
|
private $registry; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Instance of the template file resolver for finding template files. |
58
|
|
|
* @var TemplateFileResolver |
59
|
|
|
*/ |
60
|
|
|
private $templateFileResolver; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* TemplateRenderer constructor. |
64
|
|
|
* |
65
|
|
|
* @param EngineRegistry $registry |
66
|
|
|
*/ |
67
|
|
|
public function __construct(EngineRegistry $registry, TemplateFileResolver $templateFileResolver) |
68
|
|
|
{ |
69
|
|
|
$this->registry = $registry; |
70
|
|
|
$this->templateFileResolver = $templateFileResolver; |
71
|
|
|
$registry->setTemplateRenderer($this); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string|null |
|
|
|
|
76
|
|
|
*/ |
77
|
|
|
private function getTemplateExtension($templateFile) |
78
|
|
|
{ |
79
|
|
|
foreach($this->registry->getSupportedExtensions() as $extension) { |
80
|
|
|
if($extension == substr($templateFile, -strlen($extension))) return $extension; |
81
|
|
|
} |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Check if an engine exists that can render the given file. |
88
|
|
|
* @param $file |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function canRender($templateFile) |
92
|
|
|
{ |
93
|
|
|
return $this->getTemplateExtension($templateFile) !== null; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Load and cache an instance of the a template engine. |
99
|
|
|
* |
100
|
|
|
* @param $extension |
101
|
|
|
* @return mixed |
102
|
|
|
* @throws exceptions\TemplateEngineNotFoundException |
103
|
|
|
*/ |
104
|
|
|
private function loadEngine($extension) |
105
|
|
|
{ |
106
|
|
|
if(!isset($this->loadedInstances[$extension])) { |
107
|
|
|
$this->loadedInstances[$extension] = $this->registry->getEngineInstance($extension); |
108
|
|
|
} |
109
|
|
|
return $this->loadedInstances[$extension]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Renders a given template reference with associated template data. This render |
115
|
|
|
* function combs through the template directory heirachy to find a template |
116
|
|
|
* file which matches the given template reference and uses it for the purpose |
117
|
|
|
* of rendering the view. |
118
|
|
|
* |
119
|
|
|
* @param string $template The template reference file. |
120
|
|
|
* @param array $templateData The data to be passed to the template. |
|
|
|
|
121
|
|
|
* @return string |
122
|
|
|
* @throws \ntentan\honam\exceptions\FileNotFoundException |
123
|
|
|
*/ |
124
|
|
|
public function render($template, $data, $fromString = false, $extension=null) |
125
|
|
|
{ |
126
|
|
|
if($fromString) { |
127
|
|
|
$engine = $this->loadEngine($extension); |
128
|
|
|
return $engine->renderFromFileTemplate($template, $data); |
129
|
|
|
} else { |
130
|
|
|
$templateFile = $this->templateFileResolver->resolveTemplateFile($template); |
131
|
|
|
$engine = $this->loadEngine($this->getTemplateExtension($templateFile)); |
132
|
|
|
return $engine->renderFromFileTemplate($templateFile, $data); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.