1 | <?php |
||
18 | abstract class AbstractLoader implements |
||
19 | LoggerAwareInterface, |
||
20 | LoaderInterface |
||
21 | { |
||
22 | use LoggerAwareTrait; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $basePath = ''; |
||
28 | |||
29 | /** |
||
30 | * @var string[] |
||
31 | */ |
||
32 | private $paths = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | private $dynamicTemplates = []; |
||
38 | |||
39 | /** |
||
40 | * Default constructor, if none is provided by the concrete class implementations. |
||
41 | * |
||
42 | * ## Required dependencies |
||
43 | * - `logger` A PSR-3 logger |
||
44 | * |
||
45 | * @param array $data The class dependencies map. |
||
46 | */ |
||
47 | public function __construct(array $data = null) |
||
53 | |||
54 | /** |
||
55 | * Load a template content |
||
56 | * |
||
57 | * @deprecated $GLOBALS['widget_template'] |
||
58 | * @param string $ident The template ident to load and render. |
||
59 | * @throws InvalidArgumentException If the dynamic template identifier is not a string. |
||
60 | * @return string |
||
61 | */ |
||
62 | public function load($ident) |
||
94 | |||
95 | /** |
||
96 | * @deprecated $GLOBALS['widget_template'] |
||
97 | * @param string $varName The name of the variable to set this template unto. |
||
98 | * @param string|null $templateIdent The "dynamic template" to set or NULL to clear. |
||
99 | * @throws InvalidArgumentException If var name is not a string |
||
100 | * or if the template is not a string (and not null). |
||
101 | * @return void |
||
102 | */ |
||
103 | public function setDynamicTemplate($varName, $templateIdent) |
||
129 | |||
130 | /** |
||
131 | * @param string $varName The name of the variable to get template ident from. |
||
132 | * @throws InvalidArgumentException If the var name is not a string. |
||
133 | * @return string |
||
134 | */ |
||
135 | public function dynamicTemplate($varName) |
||
149 | |||
150 | /** |
||
151 | * @deprecated $GLOBALS['widget_template'] |
||
152 | * @param string $varName The name of the variable to remove. |
||
153 | * @throws InvalidArgumentException If var name is not a string. |
||
154 | * @return void |
||
155 | */ |
||
156 | public function removeDynamicTemplate($varName) |
||
171 | |||
172 | /** |
||
173 | * @deprecated $GLOBALS['widget_template'] |
||
174 | * @return void |
||
175 | */ |
||
176 | public function clearDynamicTemplates() |
||
183 | |||
184 | /** |
||
185 | * @return string |
||
186 | */ |
||
187 | protected function basePath() |
||
191 | |||
192 | /** |
||
193 | * @return string[] |
||
194 | */ |
||
195 | protected function paths() |
||
199 | |||
200 | /** |
||
201 | * Get the template file (full path + filename) to load from an ident. |
||
202 | * |
||
203 | * This method first generates the filename for an identifier and search for it in all of the loader's paths. |
||
204 | * |
||
205 | * @param string $ident The template identifier to load. |
||
206 | * @throws InvalidArgumentException If the template ident is not a string. |
||
207 | * @return string|null The full path + filename of the found template. Null if nothing was found. |
||
208 | */ |
||
209 | protected function findTemplateFile($ident) |
||
229 | |||
230 | /** |
||
231 | * @param string $ident The template identifier to convert to a filename. |
||
232 | * @return string |
||
233 | */ |
||
234 | abstract protected function filenameFromIdent($ident); |
||
235 | |||
236 | /** |
||
237 | * @param string[] $paths The list of path to add. |
||
238 | * @return LoaderInterface Chainable |
||
239 | */ |
||
240 | private function setPaths(array $paths) |
||
250 | |||
251 | /** |
||
252 | * @param string $basePath The base path to set. |
||
253 | * @throws InvalidArgumentException If the base path parameter is not a string. |
||
254 | * @return LoaderInterface Chainable |
||
255 | */ |
||
256 | private function setBasePath($basePath) |
||
267 | |||
268 | /** |
||
269 | * @param string $path The path to add to the load. |
||
270 | * @return LoaderInterface Chainable |
||
271 | */ |
||
272 | private function addPath($path) |
||
278 | |||
279 | /** |
||
280 | * @param string $path The path to resolve. |
||
281 | * @throws InvalidArgumentException If the path argument is not a string. |
||
282 | * @return string |
||
283 | */ |
||
284 | private function resolvePath($path) |
||
300 | } |
||
301 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: