1 | <?php |
||
20 | class Engine |
||
21 | { |
||
22 | /** |
||
23 | * Default template directory. |
||
24 | * @var Directory |
||
25 | */ |
||
26 | protected $directory; |
||
27 | |||
28 | /** |
||
29 | * Template file extension. |
||
30 | * @var FileExtension |
||
31 | */ |
||
32 | protected $fileExtension; |
||
33 | |||
34 | /** |
||
35 | * Collection of template folders. |
||
36 | * @var Folders |
||
37 | */ |
||
38 | protected $folders; |
||
39 | |||
40 | /** |
||
41 | * Collection of template functions. |
||
42 | * @var Functions |
||
43 | */ |
||
44 | protected $functions; |
||
45 | |||
46 | /** |
||
47 | * Collection of preassigned template data. |
||
48 | * @var Data |
||
49 | */ |
||
50 | protected $data; |
||
51 | |||
52 | /** @var ResolveTemplatePath */ |
||
53 | private $resolveTemplatePath; |
||
54 | |||
55 | 158 | /** |
|
56 | * Create new Engine instance. |
||
57 | 158 | * @param string $directory |
|
58 | 158 | * @param string $fileExtension |
|
59 | 158 | */ |
|
60 | 158 | public function __construct($directory = null, $fileExtension = 'php') |
|
69 | 8 | ||
70 | public static function fromTheme(Theme $theme, string $fileExtension = 'php'): self { |
||
75 | |||
76 | public function setResolveTemplatePath(ResolveTemplatePath $resolveTemplatePath) |
||
82 | 76 | ||
83 | public function getResolveTemplatePath(): ResolveTemplatePath |
||
87 | |||
88 | /** |
||
89 | * Set path to templates directory. |
||
90 | 6 | * @param string|null $directory Pass null to disable the default directory. |
|
91 | * @return Engine |
||
92 | 6 | */ |
|
93 | public function setDirectory($directory) |
||
99 | |||
100 | /** |
||
101 | 100 | * Get path to templates directory. |
|
102 | * @return string |
||
103 | 100 | */ |
|
104 | public function getDirectory() |
||
108 | |||
109 | /** |
||
110 | * Set the template file extension. |
||
111 | * @param string|null $fileExtension Pass null to manually set it. |
||
112 | * @return Engine |
||
113 | 42 | */ |
|
114 | public function setFileExtension($fileExtension) |
||
120 | |||
121 | /** |
||
122 | * Get the template file extension. |
||
123 | * @return string |
||
124 | */ |
||
125 | 2 | public function getFileExtension() |
|
129 | 2 | ||
130 | /** |
||
131 | * Add a new template folder for grouping templates under different namespaces. |
||
132 | * @param string $name |
||
133 | * @param string $directory |
||
134 | * @param boolean $fallback |
||
135 | * @return Engine |
||
136 | 16 | */ |
|
137 | public function addFolder($name, $directory, $fallback = false) |
||
145 | |||
146 | /** |
||
147 | 6 | * Remove a template folder. |
|
148 | * @param string $name |
||
149 | 6 | * @return Engine |
|
150 | */ |
||
151 | 6 | public function removeFolder($name) |
|
157 | |||
158 | /** |
||
159 | 68 | * Get collection of all template folders. |
|
160 | * @return Folders |
||
161 | 68 | */ |
|
162 | public function getFolders() |
||
166 | |||
167 | /** |
||
168 | * Add preassigned template data. |
||
169 | * @param array $data; |
||
170 | 78 | * @param null|string|array $templates; |
|
171 | * @return Engine |
||
172 | 78 | */ |
|
173 | public function addData(array $data, $templates = null) |
||
179 | |||
180 | /** |
||
181 | * Get all preassigned template data. |
||
182 | 4 | * @param null|string $template; |
|
183 | * @return array |
||
184 | 4 | */ |
|
185 | public function getData($template = null) |
||
189 | |||
190 | /** |
||
191 | * Register a new template function. |
||
192 | * @param string $name; |
||
193 | * @param callback $callback; |
||
194 | 10 | * @return Engine |
|
195 | */ |
||
196 | 10 | public function registerFunction($name, $callback) |
|
202 | |||
203 | /** |
||
204 | 20 | * Remove a template function. |
|
205 | * @param string $name; |
||
206 | 20 | * @return Engine |
|
207 | */ |
||
208 | public function dropFunction($name) |
||
214 | 4 | ||
215 | /** |
||
216 | 4 | * Get a template function. |
|
217 | * @param string $name |
||
218 | 4 | * @return Func |
|
219 | */ |
||
220 | public function getFunction($name) |
||
224 | |||
225 | /** |
||
226 | 2 | * Check if a template function exists. |
|
227 | * @param string $name |
||
228 | 2 | * @return boolean |
|
229 | 2 | */ |
|
230 | 2 | public function doesFunctionExist($name) |
|
234 | |||
235 | /** |
||
236 | * Load an extension. |
||
237 | * @param ExtensionInterface $extension |
||
238 | * @return Engine |
||
239 | */ |
||
240 | 2 | public function loadExtension(ExtensionInterface $extension) |
|
246 | |||
247 | /** |
||
248 | * Load multiple extensions. |
||
249 | * @param array $extensions |
||
250 | * @return Engine |
||
251 | */ |
||
252 | 2 | public function loadExtensions(array $extensions = array()) |
|
260 | |||
261 | /** |
||
262 | * Get a template path. |
||
263 | * @param string|Name $name |
||
264 | 20 | * @return string |
|
265 | */ |
||
266 | 20 | public function path($name) |
|
272 | |||
273 | /** |
||
274 | * Check if a template exists. |
||
275 | 6 | * @param string|Name $name |
|
276 | * @return boolean |
||
277 | 6 | */ |
|
278 | public function exists($name) |
||
284 | |||
285 | /** |
||
286 | * Create a new template. |
||
287 | * @param string $name |
||
288 | * @return Template |
||
289 | */ |
||
290 | public function make($name) |
||
294 | |||
295 | /** |
||
296 | * Create a new template and render it. |
||
297 | * @param string $name |
||
298 | * @param array $data |
||
299 | * @return string |
||
300 | */ |
||
301 | public function render($name, array $data = array()) |
||
305 | } |
||
306 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: