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) |
||
143 | |||
144 | /** |
||
145 | * Remove a template folder. |
||
146 | * @param string $name |
||
147 | 6 | * @return Engine |
|
148 | */ |
||
149 | 6 | public function removeFolder($name) |
|
155 | |||
156 | /** |
||
157 | * Get collection of all template folders. |
||
158 | * @return Folders |
||
159 | 68 | */ |
|
160 | public function getFolders() |
||
164 | |||
165 | /** |
||
166 | * Add preassigned template data. |
||
167 | * @param array $data; |
||
168 | * @param null|string|array $templates; |
||
169 | * @return Engine |
||
170 | 78 | */ |
|
171 | public function addData(array $data, $templates = null) |
||
177 | |||
178 | /** |
||
179 | * Get all preassigned template data. |
||
180 | * @param null|string $template; |
||
181 | * @return array |
||
182 | 4 | */ |
|
183 | public function getData($template = null) |
||
187 | |||
188 | /** |
||
189 | * Register a new template function. |
||
190 | * @param string $name; |
||
191 | * @param callback $callback; |
||
192 | * @return Engine |
||
193 | */ |
||
194 | 10 | public function registerFunction($name, $callback) |
|
200 | |||
201 | /** |
||
202 | * Remove a template function. |
||
203 | * @param string $name; |
||
204 | 20 | * @return Engine |
|
205 | */ |
||
206 | 20 | public function dropFunction($name) |
|
212 | |||
213 | /** |
||
214 | 4 | * Get a template function. |
|
215 | * @param string $name |
||
216 | 4 | * @return Func |
|
217 | */ |
||
218 | 4 | public function getFunction($name) |
|
222 | |||
223 | /** |
||
224 | * Check if a template function exists. |
||
225 | * @param string $name |
||
226 | 2 | * @return boolean |
|
227 | */ |
||
228 | 2 | public function doesFunctionExist($name) |
|
232 | 2 | ||
233 | /** |
||
234 | * Load an extension. |
||
235 | * @param ExtensionInterface $extension |
||
236 | * @return Engine |
||
237 | */ |
||
238 | public function loadExtension(ExtensionInterface $extension) |
||
244 | 2 | ||
245 | /** |
||
246 | * Load multiple extensions. |
||
247 | * @param array $extensions |
||
248 | * @return Engine |
||
249 | */ |
||
250 | public function loadExtensions(array $extensions = array()) |
||
258 | |||
259 | /** |
||
260 | * Get a template path. |
||
261 | * @param string|Name $name |
||
262 | * @return string |
||
263 | */ |
||
264 | 20 | public function path($name) |
|
270 | |||
271 | /** |
||
272 | * Check if a template exists. |
||
273 | * @param string|Name $name |
||
274 | * @return boolean |
||
275 | 6 | */ |
|
276 | public function exists($name) |
||
282 | |||
283 | /** |
||
284 | * Create a new template. |
||
285 | * @param string $name |
||
286 | * @return Template |
||
287 | */ |
||
288 | public function make($name) |
||
292 | |||
293 | /** |
||
294 | * Create a new template and render it. |
||
295 | * @param string $name |
||
296 | * @param array $data |
||
297 | * @return string |
||
298 | */ |
||
299 | public function render($name, array $data = array()) |
||
303 | } |
||
304 |
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: