1 | <?php |
||
18 | class Template |
||
19 | { |
||
20 | use SingletonTrait; |
||
21 | use OutputTrait; |
||
22 | use RouteTrait; |
||
23 | const STATUS_OK = 'HTTP/1.0 200 OK'; |
||
24 | /** |
||
25 | * @var \Twig_Environment tpl |
||
26 | */ |
||
27 | protected $tpl; |
||
28 | protected $filters = array(); |
||
29 | |||
30 | /** |
||
31 | * Constructor por defecto |
||
32 | */ |
||
33 | 2 | public function __construct() |
|
40 | |||
41 | /** |
||
42 | * Método que devuelve el loader del Template |
||
43 | * @return \Twig_LoaderInterface |
||
44 | */ |
||
45 | public function getLoader() |
||
49 | |||
50 | /** |
||
51 | * Método que activa la zona pública |
||
52 | * @param bool $public |
||
53 | * |
||
54 | * @return Template |
||
55 | */ |
||
56 | public function setPublicZone($public = true) |
||
61 | |||
62 | /** |
||
63 | * @return bool |
||
64 | */ |
||
65 | public function isPublicZone() { |
||
68 | |||
69 | /** |
||
70 | * Método que procesa la plantilla |
||
71 | * |
||
72 | * @param string $tpl |
||
73 | * @param array $vars |
||
74 | * @param array $cookies |
||
75 | * |
||
76 | * @return string HTML |
||
77 | */ |
||
78 | public function render($tpl, array $vars = array(), array $cookies = array()) |
||
86 | |||
87 | /** |
||
88 | * Método que añade una nueva ruta al path de Twig |
||
89 | * @param $path |
||
90 | * @param $domain |
||
91 | * |
||
92 | * @return Template |
||
93 | */ |
||
94 | public function addPath($path, $domain = '') |
||
99 | |||
100 | /** |
||
101 | * Método que devuelve el contenido de una plantilla |
||
102 | * @param string $tpl |
||
103 | * @param array $vars |
||
104 | * @return string |
||
105 | */ |
||
106 | public function dump($tpl, array $vars = array()) |
||
122 | |||
123 | /** |
||
124 | * Método que añade una función al motor de plantillas |
||
125 | * @param string $templateFunction |
||
126 | * @param $functionName |
||
127 | * |
||
128 | * @return Template |
||
129 | */ |
||
130 | protected function addTemplateFunction($templateFunction, $functionName) |
||
136 | |||
137 | /** |
||
138 | * Servicio que regenera todas las plantillas |
||
139 | * @return array |
||
140 | */ |
||
141 | public function regenerateTemplates() |
||
152 | |||
153 | /** |
||
154 | * @param $tplDir |
||
155 | * @param string $domain |
||
156 | * |
||
157 | * @return mixed |
||
158 | */ |
||
159 | protected function generateTemplate($tplDir, $domain = '') |
||
174 | |||
175 | /** |
||
176 | * Método que extrae el path de un string |
||
177 | * @param $path |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public static function extractPath($path) |
||
190 | |||
191 | /** |
||
192 | * Método que devuelve los dominios de una plataforma |
||
193 | * @param bool $append |
||
194 | * @return array |
||
195 | */ |
||
196 | static public function getDomains($append = false) |
||
208 | |||
209 | /** |
||
210 | * Método que añade todas las funciones de las plantillas |
||
211 | */ |
||
212 | private function addTemplateFunctions() |
||
232 | |||
233 | /** |
||
234 | * Método que devuelve el motod de plantillas |
||
235 | * @return \Twig_Environment |
||
236 | */ |
||
237 | public function getTemplateEngine() |
||
241 | |||
242 | /** |
||
243 | * Method that extract all domains for using them with the templates |
||
244 | */ |
||
245 | 2 | private function loadDomains() |
|
254 | |||
255 | /** |
||
256 | * Método que inicializa el motor de plantillas |
||
257 | */ |
||
258 | 2 | private function setup() |
|
268 | |||
269 | /** |
||
270 | * Método que inyecta los parseadores |
||
271 | */ |
||
272 | private function addTemplateTokens() |
||
278 | |||
279 | /** |
||
280 | * Método que inyecta las optimizaciones al motor de la plantilla |
||
281 | */ |
||
282 | private function optimizeTemplates() |
||
287 | |||
288 | /** |
||
289 | * Method that extract all path tag for extracting translations |
||
290 | * @param array $domains |
||
291 | * |
||
292 | * @return array |
||
293 | */ |
||
294 | private function parsePathTranslations($domains) |
||
308 | |||
309 | /** |
||
310 | * Method that generate all template caches |
||
311 | */ |
||
312 | private function generateTemplatesCache() |
||
323 | } |
||
324 |
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: