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) |
||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | protected function basePath() |
||
99 | |||
100 | /** |
||
101 | * @see FileLoader::path() |
||
102 | * @return string[] |
||
103 | */ |
||
104 | protected function paths() |
||
108 | |||
109 | /** |
||
110 | * Get the template file (full path + filename) to load from an ident. |
||
111 | * |
||
112 | * This method first generates the filename for an identifier and search for it in all of the loader's paths. |
||
113 | * |
||
114 | * @param string $ident The template identifier to load. |
||
115 | * @throws InvalidArgumentException If the template ident is not a string. |
||
116 | * @return string|null The full path + filename of the found template. Null if nothing was found. |
||
117 | */ |
||
118 | protected function findTemplateFile($ident) |
||
137 | |||
138 | /** |
||
139 | * @param string $ident The template identifier to convert to a filename. |
||
140 | * @return string |
||
141 | */ |
||
142 | abstract protected function filenameFromIdent($ident); |
||
143 | |||
144 | /** |
||
145 | * @param string[] $paths The list of path to add. |
||
146 | * @return LoaderInterface Chainable |
||
147 | */ |
||
148 | private function setPaths(array $paths) |
||
158 | |||
159 | /** |
||
160 | * @param string $basePath The base path to set. |
||
161 | * @throws InvalidArgumentException If the base path parameter is not a string. |
||
162 | * @return LoaderInterface Chainable |
||
163 | */ |
||
164 | private function setBasePath($basePath) |
||
175 | |||
176 | /** |
||
177 | * @param string $path The path to add to the load. |
||
178 | * @return LoaderInterface Chainable |
||
179 | */ |
||
180 | private function addPath($path) |
||
186 | |||
187 | /** |
||
188 | * @param string $path The path to resolve. |
||
189 | * @throws InvalidArgumentException If the path argument is not a string. |
||
190 | * @return string |
||
191 | */ |
||
192 | private function resolvePath($path) |
||
208 | |||
209 | /** |
||
210 | * @deprecated $GLOBALS['widget_template'] |
||
211 | * @param string $varName The name of the variable to set this template unto. |
||
212 | * @param string|null $templateIdent The "dynamic template" to set or NULL to clear. |
||
213 | * @throws InvalidArgumentException If var name is not a string |
||
214 | * or if the template is not a string (and not null). |
||
215 | * @return void |
||
216 | */ |
||
217 | public function setDynamicTemplate($varName, $templateIdent) |
||
243 | |||
244 | /** |
||
245 | * @param string $varName The name of the variable to get template ident from. |
||
246 | * @throws InvalidArgumentException If the var name is not a string. |
||
247 | * @return string |
||
248 | */ |
||
249 | public function dynamicTemplate($varName) |
||
263 | |||
264 | /** |
||
265 | * @deprecated $GLOBALS['widget_template'] |
||
266 | * @param string $varName The name of the variable to remove. |
||
267 | * @throws InvalidArgumentException If var name is not a string. |
||
268 | * @return void |
||
269 | */ |
||
270 | public function removeDynamicTemplate($varName) |
||
285 | |||
286 | /** |
||
287 | * @deprecated $GLOBALS['widget_template'] |
||
288 | * @return void |
||
289 | */ |
||
290 | public function clearDynamicTemplates() |
||
297 | } |
||
298 |
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: