1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base; |
3
|
|
|
|
4
|
|
|
use PSFS\base\config\Config; |
5
|
|
|
use PSFS\base\extension\AssetsTokenParser; |
6
|
|
|
use PSFS\base\extension\TemplateFunctions; |
7
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
8
|
|
|
use PSFS\base\types\helpers\ResponseHelper; |
9
|
|
|
use PSFS\base\types\traits\OutputTrait; |
10
|
|
|
use PSFS\base\types\traits\RouteTrait; |
11
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Template |
15
|
|
|
* @package PSFS\base |
16
|
|
|
*/ |
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() |
33
|
|
|
{ |
34
|
1 |
|
$this->setup(); |
35
|
1 |
|
$this->addTemplateFunctions(); |
36
|
1 |
|
$this->addTemplateTokens(); |
37
|
1 |
|
$this->optimizeTemplates(); |
38
|
1 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Método que devuelve el loader del Template |
42
|
|
|
* @return \Twig_LoaderInterface |
43
|
|
|
*/ |
44
|
|
|
public function getLoader() |
45
|
|
|
{ |
46
|
|
|
return $this->tpl->getLoader(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Método que activa la zona pública |
51
|
|
|
* @param bool $public |
52
|
|
|
* |
53
|
|
|
* @return Template |
54
|
|
|
*/ |
55
|
|
|
public function setPublicZone($public = true) |
56
|
|
|
{ |
57
|
|
|
$this->public_zone = $public; |
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Método que procesa la plantilla |
63
|
|
|
* |
64
|
|
|
* @param string $tpl |
65
|
|
|
* @param array $vars |
66
|
|
|
* @param array $cookies |
67
|
|
|
* |
68
|
|
|
* @return string HTML |
|
|
|
|
69
|
|
|
*/ |
70
|
1 |
|
public function render($tpl, array $vars = array(), array $cookies = array()) |
71
|
|
|
{ |
72
|
1 |
|
Logger::log('Start render response'); |
73
|
1 |
|
$vars = ResponseHelper::setDebugHeaders($vars); |
74
|
|
|
$output = $this->dump($tpl, $vars); |
75
|
|
|
|
76
|
|
|
return $this->output($output, 'text/html', $cookies); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Método que añade una nueva ruta al path de Twig |
81
|
|
|
* @param $path |
82
|
|
|
* @param $domain |
83
|
|
|
* |
84
|
|
|
* @return Template |
85
|
|
|
*/ |
86
|
1 |
|
public function addPath($path, $domain = '') |
87
|
|
|
{ |
88
|
1 |
|
$this->tpl->getLoader()->addPath($path, $domain); |
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Método que devuelve el contenido de una plantilla |
94
|
|
|
* @param string $tpl |
95
|
|
|
* @param array $vars |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function dump($tpl, array $vars = array()) |
99
|
|
|
{ |
100
|
|
|
$vars["__user__"] = Security::getInstance()->getUser(); |
101
|
|
|
$vars["__admin__"] = Security::getInstance()->getAdmin(); |
102
|
|
|
$vars["__profiles__"] = Security::getCleanProfiles(); |
103
|
|
|
$vars["__flash__"] = Security::getInstance()->getFlashes(); |
104
|
|
|
$dump = ''; |
105
|
|
|
try { |
106
|
|
|
$dump = $this->tpl->render($tpl, $vars); |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
109
|
|
|
} |
110
|
|
|
return $dump; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Método que añade una función al motor de plantillas |
115
|
|
|
* @param string $templateFunction |
116
|
|
|
* @param $functionName |
117
|
|
|
* |
118
|
|
|
* @return Template |
119
|
|
|
*/ |
120
|
1 |
|
protected function addTemplateFunction($templateFunction, $functionName) |
121
|
|
|
{ |
122
|
1 |
|
$function = new \Twig_SimpleFunction($templateFunction, $functionName); |
123
|
1 |
|
$this->tpl->addFunction($function); |
124
|
1 |
|
return $this; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Servicio que regenera todas las plantillas |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
1 |
|
public function regenerateTemplates() |
132
|
|
|
{ |
133
|
1 |
|
$this->generateTemplatesCache(); |
134
|
|
|
$domains = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", Cache::JSON, true); |
|
|
|
|
135
|
|
|
$translations = []; |
136
|
|
|
if (is_array($domains)) { |
137
|
|
|
$translations = $this->parsePathTranslations($domains); |
138
|
|
|
} |
139
|
|
|
$translations[] = _("Plantillas regeneradas correctamente"); |
140
|
1 |
|
return $translations; |
141
|
|
|
} |
142
|
1 |
|
|
143
|
|
|
/** |
144
|
|
|
* @param $tplDir |
145
|
|
|
* @param string $domain |
146
|
|
|
* |
147
|
|
|
* @return mixed |
148
|
|
|
*/ |
149
|
1 |
|
protected function generateTemplate($tplDir, $domain = '') |
150
|
|
|
{ |
151
|
1 |
|
$templatesDir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($tplDir), \RecursiveIteratorIterator::LEAVES_ONLY); |
|
|
|
|
152
|
|
|
foreach ($templatesDir as $file) { |
153
|
|
|
// force compilation |
154
|
|
|
if ($file->isFile()) { |
155
|
|
|
try { |
156
|
|
|
$this->tpl->load(str_replace($tplDir . '/', '', $file)); |
157
|
|
|
} catch (\Exception $e) { |
158
|
1 |
|
Logger::log($e->getMessage(), LOG_ERR, ['file' => $e->getFile(), 'line' => $e->getLine()]); |
159
|
|
|
} |
160
|
1 |
|
} |
161
|
|
|
} |
162
|
|
|
return str_replace("%d", $domain, str_replace("%s", $tplDir, _("Generando plantillas en path '%s' para el dominio '%d'"))); |
|
|
|
|
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Método que extrae el path de un string |
167
|
1 |
|
* @param $path |
168
|
|
|
* |
169
|
1 |
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public static function extractPath($path) |
172
|
|
|
{ |
173
|
|
|
$explodePath = explode(DIRECTORY_SEPARATOR, $path); |
174
|
|
|
$realPath = array(); |
175
|
|
|
for ($i = 0, $parts = count($explodePath) - 1; $i < $parts; $i++) { |
176
|
1 |
|
$realPath[] = $explodePath[$i]; |
177
|
|
|
} |
178
|
1 |
|
return implode(DIRECTORY_SEPARATOR, $realPath); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Método que devuelve los dominios de una plataforma |
183
|
|
|
* @param bool $append |
184
|
|
|
* @return array |
185
|
1 |
|
*/ |
186
|
|
|
static public function getDomains($append = false) |
187
|
1 |
|
{ |
188
|
|
|
$domains = Router::getInstance()->getDomains(); |
189
|
|
|
if ($append) { |
190
|
|
|
foreach ($domains as &$domain) { |
191
|
|
|
foreach ($domain as &$path) { |
192
|
|
|
$path .= DIRECTORY_SEPARATOR; |
193
|
1 |
|
} |
194
|
|
|
} |
195
|
1 |
|
} |
196
|
|
|
return $domains; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Método que añade todas las funciones de las plantillas |
201
|
1 |
|
*/ |
202
|
|
|
private function addTemplateFunctions() |
203
|
1 |
|
{ |
204
|
|
|
//Asignamos las funciones especiales |
205
|
|
|
$functions = [ |
206
|
|
|
'asset' => TemplateFunctions::ASSETS_FUNCTION, |
207
|
|
|
'form' => TemplateFunctions::FORM_FUNCTION, |
208
|
|
|
'form_widget' => TemplateFunctions::WIDGET_FUNCTION, |
209
|
1 |
|
'form_button' => TemplateFunctions::BUTTON_FUNCTION, |
210
|
|
|
'get_config' => TemplateFunctions::CONFIG_FUNCTION, |
211
|
1 |
|
'path' => TemplateFunctions::ROUTE_FUNCTION, |
212
|
|
|
'resource' => TemplateFunctions::RESOURCE_FUNCTION, |
213
|
|
|
'session' => TemplateFunctions::SESSION_FUNCTION, |
214
|
|
|
'existsFlash' => TemplateFunctions::EXISTS_FLASH_FUNCTION, |
215
|
|
|
'getFlash' => TemplateFunctions::GET_FLASH_FUNCTION, |
216
|
|
|
]; |
217
|
|
|
foreach($functions as $name => $function) { |
218
|
|
|
$this->addTemplateFunction($name, $function); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Método que devuelve el motod de plantillas |
224
|
|
|
* @return \Twig_Environment |
225
|
|
|
*/ |
226
|
|
|
public function getTemplateEngine() |
227
|
|
|
{ |
228
|
|
|
return $this->tpl; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Method that extract all domains for using them with the templates |
233
|
|
|
*/ |
234
|
|
|
private function loadDomains() |
235
|
|
|
{ |
236
|
|
|
$domains = Cache::getInstance()->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', Cache::JSON, true); |
|
|
|
|
237
|
|
|
if (null !== $domains) { |
238
|
|
|
foreach ($domains as $domain => $paths) { |
239
|
|
|
$this->addPath($paths['template'], preg_replace('/(@|\/)/', '', $domain)); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Método que inicializa el motor de plantillas |
246
|
|
|
*/ |
247
|
|
|
private function setup() |
248
|
|
|
{ |
249
|
|
|
$loader = new \Twig_Loader_Filesystem(GeneratorHelper::getTemplatePath()); |
250
|
|
|
$this->tpl = new \Twig_Environment($loader, array( |
251
|
|
|
'cache' => CACHE_DIR . DIRECTORY_SEPARATOR . 'twig', |
252
|
|
|
'debug' => (bool)$this->debug, |
253
|
|
|
'auto_reload' => Config::getParam('twig.auto_reload', TRUE), |
254
|
|
|
)); |
255
|
|
|
$this->loadDomains(); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Método que inyecta los parseadores |
260
|
|
|
*/ |
261
|
|
|
private function addTemplateTokens() |
262
|
|
|
{ |
263
|
|
|
//Añadimos las extensiones de los tags |
264
|
|
|
$this->tpl->addTokenParser(new AssetsTokenParser("css")); |
265
|
|
|
$this->tpl->addTokenParser(new AssetsTokenParser("js")); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Método que inyecta las optimizaciones al motor de la plantilla |
270
|
|
|
*/ |
271
|
|
|
private function optimizeTemplates() |
272
|
|
|
{ |
273
|
|
|
//Optimizamos |
274
|
|
|
$this->tpl->addExtension(new \Twig_Extensions_Extension_I18n()); |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
/** |
278
|
|
|
* Method that extract all path tag for extracting translations |
279
|
|
|
* @param array $domains |
280
|
|
|
* |
281
|
|
|
* @return array |
282
|
|
|
*/ |
283
|
|
|
private function parsePathTranslations($domains) |
284
|
|
|
{ |
285
|
|
|
$translations = array(); |
286
|
|
|
if (!empty($domains)) { |
287
|
|
|
foreach ($domains as $domain => $paths) { |
288
|
|
|
if (strlen($domain) && array_key_exists("template", $paths)) { |
289
|
1 |
|
$this->addPath($paths["template"], $domain); |
290
|
|
|
$translations[] = $this->generateTemplate($paths["template"], $domain); |
291
|
|
|
} |
292
|
1 |
|
} |
293
|
1 |
|
} |
294
|
1 |
|
|
295
|
1 |
|
return $translations; |
296
|
1 |
|
} |
297
|
1 |
|
|
298
|
1 |
|
/** |
299
|
1 |
|
* Method that generate all template caches |
300
|
1 |
|
*/ |
301
|
1 |
|
private function generateTemplatesCache() |
302
|
1 |
|
{ |
303
|
|
|
/** @var \Twig_Loader_Filesystem $loader */ |
304
|
|
|
$loader = $this->tpl->getLoader(); |
305
|
|
|
$availablePaths = $loader->getPaths(); |
306
|
|
|
if (!empty($availablePaths)) { |
307
|
|
|
foreach ($availablePaths as $path) { |
308
|
|
|
$this->generateTemplate($path); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.