1 | <?php |
||
17 | class Template |
||
18 | { |
||
19 | use SingletonTrait; |
||
20 | use OutputTrait; |
||
21 | use RouteTrait; |
||
22 | const STATUS_OK = 'HTTP/1.0 200 OK'; |
||
23 | /** |
||
24 | * @var \Twig_Environment tpl |
||
25 | */ |
||
26 | protected $tpl; |
||
27 | protected $filters = array(); |
||
28 | |||
29 | /** |
||
30 | * Constructor por defecto |
||
31 | */ |
||
32 | 1 | public function __construct() |
|
39 | |||
40 | /** |
||
41 | * Método que devuelve el loader del Template |
||
42 | * @return \Twig_LoaderInterface |
||
43 | */ |
||
44 | 1 | public function getLoader() |
|
48 | |||
49 | /** |
||
50 | * Método que activa la zona pública |
||
51 | * @param bool $public |
||
52 | * |
||
53 | * @return Template |
||
54 | */ |
||
55 | 1 | public function setPublicZone($public = true) |
|
60 | |||
61 | /** |
||
62 | * @return bool |
||
63 | */ |
||
64 | 1 | public function isPublicZone() { |
|
67 | |||
68 | /** |
||
69 | * Método que procesa la plantilla |
||
70 | * |
||
71 | * @param string $tpl |
||
72 | * @param array $vars |
||
73 | * @param array $cookies |
||
74 | * |
||
75 | * @return string HTML |
||
76 | */ |
||
77 | public function render($tpl, array $vars = array(), array $cookies = array()) |
||
78 | { |
||
79 | Logger::log('Start render response'); |
||
80 | $vars = ResponseHelper::setDebugHeaders($vars); |
||
81 | $output = $this->dump($tpl, $vars); |
||
82 | |||
83 | return $this->output($output, 'text/html', $cookies); |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Método que añade una nueva ruta al path de Twig |
||
88 | * @param $path |
||
89 | * @param $domain |
||
90 | * |
||
91 | * @return Template |
||
92 | */ |
||
93 | public function addPath($path, $domain = '') |
||
94 | { |
||
95 | $this->tpl->getLoader()->addPath($path, $domain); |
||
|
|||
96 | return $this; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Método que devuelve el contenido de una plantilla |
||
101 | * @param string $tpl |
||
102 | * @param array $vars |
||
103 | * @return string |
||
104 | */ |
||
105 | 1 | public function dump($tpl, array $vars = array()) |
|
106 | { |
||
107 | 1 | $vars["__user__"] = Security::getInstance()->getUser(); |
|
108 | 1 | $vars["__admin__"] = Security::getInstance()->getAdmin(); |
|
109 | 1 | $vars["__profiles__"] = Security::getCleanProfiles(); |
|
110 | 1 | $vars["__flash__"] = Security::getInstance()->getFlashes(); |
|
111 | 1 | $vars["__get__"] = Request::getInstance()->getQueryParams(); |
|
112 | 1 | $vars["__post__"] = Request::getInstance()->getData(); |
|
113 | 1 | $dump = ''; |
|
114 | try { |
||
115 | 1 | $dump = $this->tpl->render($tpl, $vars); |
|
116 | } catch (\Exception $e) { |
||
117 | Logger::log($e->getMessage(), LOG_ERR); |
||
118 | } |
||
119 | 1 | return $dump; |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Método que añade una función al motor de plantillas |
||
124 | * @param string $templateFunction |
||
125 | * @param $functionName |
||
126 | * |
||
127 | * @return Template |
||
128 | */ |
||
129 | 1 | protected function addTemplateFunction($templateFunction, $functionName) |
|
135 | |||
136 | /** |
||
137 | * Servicio que regenera todas las plantillas |
||
138 | * @return array |
||
139 | */ |
||
140 | 1 | public function regenerateTemplates() |
|
151 | |||
152 | /** |
||
153 | * @param $tplDir |
||
154 | * @param string $domain |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | 1 | protected function generateTemplate($tplDir, $domain = '') |
|
173 | |||
174 | /** |
||
175 | * Método que extrae el path de un string |
||
176 | * @param $path |
||
177 | * |
||
178 | * @return string |
||
179 | */ |
||
180 | 1 | public static function extractPath($path) |
|
189 | |||
190 | /** |
||
191 | * Método que devuelve los dominios de una plataforma |
||
192 | * @param bool $append |
||
193 | * @return array |
||
194 | */ |
||
195 | 1 | static public function getDomains($append = false) |
|
196 | { |
||
197 | 1 | $domains = Router::getInstance()->getDomains(); |
|
198 | 1 | if ($append) { |
|
199 | 1 | foreach ($domains as &$domain) { |
|
200 | foreach ($domain as &$path) { |
||
201 | $path .= DIRECTORY_SEPARATOR; |
||
202 | } |
||
203 | } |
||
204 | } |
||
205 | 1 | return $domains; |
|
206 | } |
||
207 | |||
208 | /** |
||
209 | * Método que añade todas las funciones de las plantillas |
||
210 | */ |
||
211 | 1 | private function addTemplateFunctions() |
|
231 | |||
232 | /** |
||
233 | * Método que devuelve el motod de plantillas |
||
234 | * @return \Twig_Environment |
||
235 | */ |
||
236 | 1 | public function getTemplateEngine() |
|
240 | |||
241 | /** |
||
242 | * Method that extract all domains for using them with the templates |
||
243 | */ |
||
244 | 1 | private function loadDomains() |
|
245 | { |
||
246 | 1 | $domains = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', Cache::JSON, true); |
|
247 | 1 | if (null !== $domains) { |
|
248 | 1 | foreach ($domains as $domain => $paths) { |
|
249 | $this->addPath($paths['template'], preg_replace('/(@|\/)/', '', $domain)); |
||
250 | } |
||
251 | } |
||
252 | 1 | } |
|
253 | |||
254 | /** |
||
255 | * Método que inicializa el motor de plantillas |
||
256 | */ |
||
257 | 1 | private function setup() |
|
267 | |||
268 | /** |
||
269 | * Método que inyecta los parseadores |
||
270 | */ |
||
271 | 1 | private function addTemplateTokens() |
|
277 | |||
278 | /** |
||
279 | * Método que inyecta las optimizaciones al motor de la plantilla |
||
280 | */ |
||
281 | 1 | private function optimizeTemplates() |
|
286 | |||
287 | /** |
||
288 | * Method that extract all path tag for extracting translations |
||
289 | * @param array $domains |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | 1 | private function parsePathTranslations($domains) |
|
294 | { |
||
295 | 1 | $translations = array(); |
|
296 | 1 | if (!empty($domains)) { |
|
297 | foreach ($domains as $domain => $paths) { |
||
298 | if (strlen($domain) && array_key_exists("template", $paths)) { |
||
299 | $this->addPath($paths["template"], $domain); |
||
300 | $translations[] = $this->generateTemplate($paths["template"], $domain); |
||
301 | } |
||
302 | } |
||
303 | } |
||
304 | |||
305 | 1 | return $translations; |
|
306 | } |
||
307 | |||
308 | /** |
||
309 | * Method that generate all template caches |
||
310 | */ |
||
311 | 1 | private function generateTemplatesCache() |
|
322 | } |
||
323 |
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: