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 | 1 | public function render($tpl, array $vars = array(), array $cookies = array()) |
|
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 | 2 | public function addPath($path, $domain = '') |
|
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() |
|
141 | { |
||
142 | 1 | $this->generateTemplatesCache(); |
|
143 | 1 | $domains = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", Cache::JSON, true); |
|
144 | 1 | $translations = []; |
|
145 | 1 | if (is_array($domains)) { |
|
146 | 1 | $translations = $this->parsePathTranslations($domains); |
|
147 | } |
||
148 | 1 | $translations[] = _("Plantillas regeneradas correctamente"); |
|
149 | 1 | return $translations; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param $tplDir |
||
154 | * @param string $domain |
||
155 | * |
||
156 | * @return mixed |
||
157 | */ |
||
158 | 1 | protected function generateTemplate($tplDir, $domain = '') |
|
159 | { |
||
160 | 1 | $templatesDir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($tplDir), \RecursiveIteratorIterator::LEAVES_ONLY); |
|
161 | 1 | foreach ($templatesDir as $file) { |
|
162 | // force compilation |
||
163 | 1 | if ($file->isFile()) { |
|
164 | try { |
||
165 | 1 | $this->tpl->load(str_replace($tplDir . '/', '', $file)); |
|
166 | 1 | } catch (\Exception $e) { |
|
167 | 1 | Logger::log($e->getMessage(), LOG_ERR, ['file' => $e->getFile(), 'line' => $e->getLine()]); |
|
168 | } |
||
169 | } |
||
170 | } |
||
171 | 1 | return str_replace("%d", $domain, str_replace("%s", $tplDir, _("Generando plantillas en path '%s' para el dominio '%d'"))); |
|
172 | } |
||
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) |
|
181 | { |
||
182 | 1 | $explodePath = explode(DIRECTORY_SEPARATOR, $path); |
|
183 | 1 | $realPath = array(); |
|
184 | 1 | for ($i = 0, $parts = count($explodePath) - 1; $i < $parts; $i++) { |
|
185 | 1 | $realPath[] = $explodePath[$i]; |
|
186 | } |
||
187 | 1 | return implode(DIRECTORY_SEPARATOR, $realPath); |
|
188 | } |
||
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 | 1 | foreach ($domain as &$path) { |
|
201 | 1 | $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() |
|
212 | { |
||
213 | //Asignamos las funciones especiales |
||
214 | $functions = [ |
||
215 | 1 | 'asset' => TemplateFunctions::ASSETS_FUNCTION, |
|
216 | 1 | 'form' => TemplateFunctions::FORM_FUNCTION, |
|
217 | 1 | 'form_widget' => TemplateFunctions::WIDGET_FUNCTION, |
|
218 | 1 | 'form_button' => TemplateFunctions::BUTTON_FUNCTION, |
|
219 | 1 | 'get_config' => TemplateFunctions::CONFIG_FUNCTION, |
|
220 | 1 | 'path' => TemplateFunctions::ROUTE_FUNCTION, |
|
221 | 1 | 'resource' => TemplateFunctions::RESOURCE_FUNCTION, |
|
222 | 1 | 'session' => TemplateFunctions::SESSION_FUNCTION, |
|
223 | 1 | 'existsFlash' => TemplateFunctions::EXISTS_FLASH_FUNCTION, |
|
224 | 1 | 'getFlash' => TemplateFunctions::GET_FLASH_FUNCTION, |
|
225 | ]; |
||
226 | 1 | foreach($functions as $name => $function) { |
|
227 | 1 | $this->addTemplateFunction($name, $function); |
|
228 | } |
||
229 | 1 | } |
|
230 | |||
231 | /** |
||
232 | * Método que devuelve el motod de plantillas |
||
233 | * @return \Twig_Environment |
||
234 | */ |
||
235 | 1 | public function getTemplateEngine() |
|
239 | |||
240 | /** |
||
241 | * Method that extract all domains for using them with the templates |
||
242 | */ |
||
243 | 1 | private function loadDomains() |
|
244 | { |
||
245 | 1 | $domains = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', Cache::JSON, true); |
|
246 | 1 | if (null !== $domains) { |
|
247 | 1 | foreach ($domains as $domain => $paths) { |
|
248 | 1 | $this->addPath($paths['template'], preg_replace('/(@|\/)/', '', $domain)); |
|
249 | } |
||
250 | } |
||
251 | 1 | } |
|
252 | |||
253 | /** |
||
254 | * Método que inicializa el motor de plantillas |
||
255 | */ |
||
256 | 1 | private function setup() |
|
257 | { |
||
258 | 1 | $loader = new \Twig_Loader_Filesystem(GeneratorHelper::getTemplatePath()); |
|
259 | 1 | $this->tpl = new \Twig_Environment($loader, array( |
|
260 | 1 | 'cache' => CACHE_DIR . DIRECTORY_SEPARATOR . 'twig', |
|
261 | 1 | 'debug' => (bool)$this->debug, |
|
262 | 1 | 'auto_reload' => Config::getParam('twig.autoreload', TRUE), |
|
263 | )); |
||
264 | 1 | $this->loadDomains(); |
|
265 | 1 | } |
|
266 | |||
267 | /** |
||
268 | * Método que inyecta los parseadores |
||
269 | */ |
||
270 | 1 | private function addTemplateTokens() |
|
276 | |||
277 | /** |
||
278 | * Método que inyecta las optimizaciones al motor de la plantilla |
||
279 | */ |
||
280 | 1 | private function optimizeTemplates() |
|
285 | |||
286 | /** |
||
287 | * Method that extract all path tag for extracting translations |
||
288 | * @param array $domains |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | 1 | private function parsePathTranslations($domains) |
|
306 | |||
307 | /** |
||
308 | * Method that generate all template caches |
||
309 | */ |
||
310 | 1 | private function generateTemplatesCache() |
|
321 | } |
||
322 |
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: