1 | <?php |
||
12 | abstract class AbstractLoader implements LoaderInterface |
||
13 | { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $basePath = ''; |
||
18 | |||
19 | /** |
||
20 | * @var string[] |
||
21 | */ |
||
22 | private $paths = []; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | private $dynamicTemplates = []; |
||
28 | |||
29 | /** |
||
30 | * The cache of searched template files. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | private $fileCache = []; |
||
35 | |||
36 | /** |
||
37 | * Default constructor, if none is provided by the concrete class implementations. |
||
38 | * |
||
39 | * |
||
40 | * @param array $data The class dependencies map. |
||
41 | */ |
||
42 | public function __construct(array $data = null) |
||
47 | |||
48 | /** |
||
49 | * Load a template content |
||
50 | * |
||
51 | * @param string $ident The template ident to load and render. |
||
52 | * @return string |
||
53 | */ |
||
54 | public function load($ident) |
||
76 | |||
77 | /** |
||
78 | * @param string $varName The name of the variable to get template ident from. |
||
79 | * @return string |
||
80 | */ |
||
81 | public function dynamicTemplate(string $varName): string |
||
89 | |||
90 | /** |
||
91 | * @param string $varName The name of the variable to set this template unto. |
||
92 | * @param string|null $templateIdent The "dynamic template" to set or NULL to clear. |
||
93 | * or if the template is not a string (and not null). |
||
94 | * @return void |
||
95 | */ |
||
96 | public function setDynamicTemplate(string $varName, ?string $templateIdent): void |
||
105 | |||
106 | /** |
||
107 | * @param string $varName The name of the variable to remove. |
||
108 | * @return void |
||
109 | */ |
||
110 | public function removeDynamicTemplate(string $varName): void |
||
114 | |||
115 | /** |
||
116 | * @return void |
||
117 | */ |
||
118 | public function clearDynamicTemplates(): void |
||
122 | |||
123 | /** |
||
124 | * @return string |
||
125 | */ |
||
126 | protected function basePath(): string |
||
130 | |||
131 | /** |
||
132 | * @param string $basePath The base path to set. |
||
133 | * @return self |
||
134 | */ |
||
135 | private function setBasePath(string $basePath) |
||
141 | |||
142 | /** |
||
143 | * @return string[] |
||
144 | */ |
||
145 | protected function paths(): array |
||
149 | |||
150 | /** |
||
151 | * @param string[] $paths The list of path to add. |
||
152 | * @return self |
||
153 | */ |
||
154 | private function setPaths(array $paths) |
||
164 | |||
165 | /** |
||
166 | * @param string $path The path to add to the load. |
||
167 | * @return self |
||
168 | */ |
||
169 | private function addPath(string $path) |
||
175 | |||
176 | /** |
||
177 | * @param string $path The path to resolve. |
||
178 | * @return string |
||
179 | */ |
||
180 | private function resolvePath(string $path): string |
||
190 | |||
191 | /** |
||
192 | * Determine if the variable is a template literal. |
||
193 | * |
||
194 | * This method looks for any line-breaks in the given string, |
||
195 | * which a file path would not allow. |
||
196 | * |
||
197 | * @param string $ident The template being evaluated. |
||
198 | * @return boolean Returns TRUE if the given value is most likely the template contents |
||
199 | * as opposed to a template identifier (file path). |
||
200 | */ |
||
201 | protected function isTemplateString(string $ident): bool |
||
205 | |||
206 | /** |
||
207 | * Get the template file (full path + filename) to load from an ident. |
||
208 | * |
||
209 | * This method first generates the filename for an identifier and search for it in all of the loader's paths. |
||
210 | * |
||
211 | * @param string $ident The template identifier to load.. |
||
212 | * @return string|null The full path + filename of the found template. NULL if nothing was found. |
||
213 | */ |
||
214 | protected function findTemplateFile(string $ident): ?string |
||
236 | |||
237 | /** |
||
238 | * @param string $ident The template identifier to convert to a filename. |
||
239 | * @return string |
||
240 | */ |
||
241 | abstract protected function filenameFromIdent(string $ident): string; |
||
242 | } |
||
243 |