1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base; |
3
|
|
|
|
4
|
|
|
use PSFS\base\config\Config; |
5
|
|
|
use PSFS\base\exception\AccessDeniedException; |
6
|
|
|
use PSFS\base\exception\ConfigException; |
7
|
|
|
use PSFS\base\exception\RouterException; |
8
|
|
|
use PSFS\base\types\helpers\AdminHelper; |
9
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
10
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
11
|
|
|
use PSFS\base\types\helpers\RequestHelper; |
12
|
|
|
use PSFS\base\types\helpers\RouterHelper; |
13
|
|
|
use PSFS\base\types\helpers\SecurityHelper; |
14
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
15
|
|
|
use PSFS\controller\base\Admin; |
16
|
|
|
use PSFS\services\AdminServices; |
17
|
|
|
use Symfony\Component\Finder\Finder; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Router |
22
|
|
|
* @package PSFS |
23
|
|
|
*/ |
24
|
|
|
class Router |
25
|
|
|
{ |
26
|
|
|
use SingletonTrait; |
27
|
|
|
|
28
|
|
|
protected $routing; |
29
|
|
|
protected $slugs; |
30
|
|
|
private $domains; |
31
|
|
|
/** |
32
|
|
|
* @var Finder $finder |
33
|
|
|
*/ |
34
|
|
|
private $finder; |
35
|
|
|
/** |
36
|
|
|
* @var \PSFS\base\Cache $cache |
37
|
|
|
*/ |
38
|
|
|
private $cache; |
39
|
|
|
/** |
40
|
|
|
* @var bool headersSent |
41
|
|
|
*/ |
42
|
|
|
protected $headersSent = false; |
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
protected $cacheType = Cache::JSON; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor Router |
50
|
|
|
* @throws ConfigException |
51
|
|
|
*/ |
52
|
1 |
|
public function __construct() |
53
|
|
|
{ |
54
|
1 |
|
$this->finder = new Finder(); |
55
|
1 |
|
$this->cache = Cache::getInstance(); |
56
|
1 |
|
$this->init(); |
57
|
1 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Inicializador Router |
61
|
|
|
* @throws ConfigException |
62
|
|
|
*/ |
63
|
1 |
|
public function init() |
64
|
|
|
{ |
65
|
1 |
|
list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", $this->cacheType, TRUE); |
|
|
|
|
66
|
1 |
|
$this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
|
|
|
|
67
|
1 |
|
if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
68
|
1 |
|
$this->debugLoad(); |
69
|
|
|
} |
70
|
1 |
|
$this->checkExternalModules(false); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Load routes and domains and store them |
75
|
|
|
*/ |
76
|
1 |
|
private function debugLoad() { |
77
|
1 |
|
Logger::log('Begin routes load', LOG_DEBUG); |
78
|
1 |
|
$this->hydrateRouting(); |
79
|
1 |
|
$this->simpatize(); |
80
|
1 |
|
Logger::log('End routes load', LOG_DEBUG); |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Método que deriva un error HTTP de página no encontrada |
85
|
|
|
* |
86
|
|
|
* @param \Exception $e |
|
|
|
|
87
|
|
|
* @param boolean $isJson |
88
|
|
|
* |
89
|
|
|
* @return string HTML |
90
|
|
|
*/ |
91
|
|
|
public function httpNotFound(\Exception $e = NULL, $isJson = false) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
Logger::log('Throw not found exception'); |
94
|
|
|
if (NULL === $e) { |
95
|
|
|
Logger::log('Not found page throwed without previous exception', LOG_WARNING); |
96
|
|
|
$e = new \Exception(_('Page not found'), 404); |
97
|
|
|
} |
98
|
|
|
$template = Template::getInstance()->setStatus($e->getCode()); |
99
|
|
|
if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE')) || $isJson) { |
100
|
|
|
return $template->output(json_encode(array( |
101
|
|
|
"success" => FALSE, |
102
|
|
|
"error" => $e->getMessage(), |
103
|
|
|
)), 'application/json'); |
104
|
|
|
} else { |
105
|
|
|
$not_found_rouote = Config::getParam('route.404'); |
106
|
|
|
if(null !== $not_found_rouote) { |
107
|
|
|
Request::getInstance()->redirect($this->getRoute($not_found_rouote, true)); |
108
|
|
|
} else { |
109
|
|
|
return $template->render('error.html.twig', array( |
110
|
|
|
'exception' => $e, |
111
|
|
|
'trace' => $e->getTraceAsString(), |
112
|
|
|
'error_page' => TRUE, |
113
|
|
|
)); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Método que devuelve las rutas |
120
|
|
|
* @return string|null |
121
|
|
|
*/ |
122
|
1 |
|
public function getSlugs() |
123
|
|
|
{ |
124
|
1 |
|
return $this->slugs; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return mixed |
129
|
|
|
*/ |
130
|
2 |
|
public function getRoutes() { |
131
|
2 |
|
return $this->routing; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Method that extract all routes in the platform |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
1 |
|
public function getAllRoutes() |
139
|
|
|
{ |
140
|
1 |
|
$routes = []; |
141
|
1 |
|
foreach ($this->getRoutes() as $path => $route) { |
142
|
1 |
|
if (array_key_exists('slug', $route)) { |
143
|
1 |
|
$routes[$route['slug']] = $path; |
144
|
|
|
} |
145
|
|
|
} |
146
|
1 |
|
return $routes; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Método que calcula el objeto a enrutar |
151
|
|
|
* |
152
|
|
|
* @param string|null $route |
153
|
|
|
* |
154
|
|
|
* @throws \Exception |
155
|
|
|
* @return string HTML |
156
|
|
|
*/ |
157
|
1 |
|
public function execute($route) |
158
|
|
|
{ |
159
|
1 |
|
Logger::log('Executing the request'); |
160
|
|
|
try { |
161
|
|
|
//Check CORS for requests |
162
|
1 |
|
RequestHelper::checkCORS(); |
163
|
|
|
// Checks restricted access |
164
|
1 |
|
SecurityHelper::checkRestrictedAccess($route); |
165
|
|
|
//Search action and execute |
166
|
1 |
|
$this->searchAction($route); |
167
|
1 |
|
} catch (AccessDeniedException $e) { |
168
|
|
|
Logger::log(_('Solicitamos credenciales de acceso a zona restringida')); |
169
|
|
|
return Admin::staticAdminLogon($route); |
170
|
1 |
|
} catch (RouterException $r) { |
171
|
1 |
|
Logger::log($r->getMessage(), LOG_WARNING); |
172
|
|
|
} catch (\Exception $e) { |
173
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
174
|
|
|
throw $e; |
175
|
|
|
} |
176
|
|
|
|
177
|
1 |
|
throw new RouterException(_("Página no encontrada"), 404); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Método que busca el componente que ejecuta la ruta |
182
|
|
|
* |
183
|
|
|
* @param string $route |
184
|
|
|
* |
185
|
|
|
* @throws \PSFS\base\exception\RouterException |
186
|
|
|
*/ |
187
|
1 |
|
protected function searchAction($route) |
188
|
|
|
{ |
189
|
1 |
|
Logger::log('Searching action to execute: ' . $route, LOG_INFO); |
190
|
|
|
//Revisamos si tenemos la ruta registrada |
191
|
1 |
|
$parts = parse_url($route); |
192
|
1 |
|
$path = (array_key_exists('path', $parts)) ? $parts['path'] : $route; |
193
|
1 |
|
$httpRequest = Request::getInstance()->getMethod(); |
194
|
1 |
|
foreach ($this->routing as $pattern => $action) { |
195
|
1 |
|
list($httpMethod, $routePattern) = RouterHelper::extractHttpRoute($pattern); |
196
|
1 |
|
$matched = RouterHelper::matchRoutePattern($routePattern, $path); |
197
|
1 |
|
if ($matched && ($httpMethod === "ALL" || $httpRequest === $httpMethod) && RouterHelper::compareSlashes($routePattern, $path)) { |
|
|
|
|
198
|
|
|
$get = RouterHelper::extractComponents($route, $routePattern); |
199
|
|
|
/** @var $class \PSFS\base\types\Controller */ |
200
|
|
|
$class = RouterHelper::getClassToCall($action); |
201
|
|
|
try { |
202
|
|
|
$this->executeCachedRoute($route, $action, $class, $get); |
203
|
|
|
} catch (\Exception $e) { |
204
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
205
|
|
|
throw new \RuntimeException($e->getMessage(), 404, $e); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
1 |
|
throw new RouterException(_("Ruta no encontrada")); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Método que manda las cabeceras de autenticación |
214
|
|
|
* @return string HTML |
215
|
|
|
*/ |
216
|
|
|
protected function sentAuthHeader() |
217
|
|
|
{ |
218
|
|
|
return AdminServices::getInstance()->setAdminHeaders(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return string|null |
223
|
|
|
*/ |
224
|
1 |
|
private function getExternalModules() { |
225
|
1 |
|
$externalModules = Config::getParam('modules.extend', ''); |
226
|
1 |
|
if(Config::getParam('psfs.auth', false)) { |
227
|
|
|
$externalModules .= ',psfs/auth'; |
228
|
|
|
} |
229
|
1 |
|
return $externalModules; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Method that check if the proyect has sub project to include |
234
|
|
|
* @param boolean $hydrateRoute |
235
|
|
|
*/ |
236
|
1 |
|
private function checkExternalModules($hydrateRoute = true) |
237
|
|
|
{ |
238
|
1 |
|
$externalModules = $this->getExternalModules(); |
239
|
1 |
|
if (strlen($externalModules)) { |
240
|
|
|
$externalModules = explode(',', $externalModules); |
241
|
|
|
foreach ($externalModules as &$module) { |
242
|
|
|
$module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module); |
243
|
|
|
$externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src'; |
244
|
|
|
if (file_exists($externalModulePath)) { |
245
|
|
|
$externalModule = $this->finder->directories()->in($externalModulePath)->depth(0); |
246
|
|
|
if (!empty($externalModule)) { |
247
|
|
|
foreach ($externalModule as $modulePath) { |
248
|
|
|
$extModule = $modulePath->getBasename(); |
249
|
|
|
$moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php'); |
|
|
|
|
250
|
|
|
if (file_exists($moduleAutoloader)) { |
251
|
|
|
@include $moduleAutoloader; |
252
|
|
|
if ($hydrateRoute) { |
253
|
|
|
$this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, '\\' . $extModule, $this->routing); |
|
|
|
|
254
|
|
|
} |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
} |
260
|
|
|
} |
261
|
1 |
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* Method that gather all the routes in the project |
265
|
|
|
*/ |
266
|
1 |
|
private function generateRouting() |
267
|
|
|
{ |
268
|
1 |
|
$base = SOURCE_DIR; |
269
|
1 |
|
$modulesPath = realpath(CORE_DIR); |
270
|
1 |
|
$this->routing = $this->inspectDir($base, "PSFS", array()); |
271
|
1 |
|
$this->checkExternalModules(); |
272
|
1 |
|
if (file_exists($modulesPath)) { |
273
|
|
|
$modules = $this->finder->directories()->in($modulesPath)->depth(0); |
274
|
|
|
foreach ($modules as $modulePath) { |
275
|
|
|
$module = $modulePath->getBasename(); |
276
|
|
|
$this->routing = $this->inspectDir($modulesPath . DIRECTORY_SEPARATOR . $module, $module, $this->routing); |
|
|
|
|
277
|
|
|
} |
278
|
|
|
} |
279
|
1 |
|
$this->cache->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->domains, Cache::JSON, TRUE); |
280
|
1 |
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Método que regenera el fichero de rutas |
284
|
|
|
* @throws ConfigException |
285
|
|
|
*/ |
286
|
1 |
|
public function hydrateRouting() |
287
|
|
|
{ |
288
|
1 |
|
$this->generateRouting(); |
289
|
1 |
|
$home = Config::getInstance()->get('home.action'); |
290
|
1 |
|
if (NULL !== $home || $home !== '') { |
291
|
1 |
|
$home_params = NULL; |
292
|
1 |
|
foreach ($this->routing as $pattern => $params) { |
293
|
1 |
|
list($method, $route) = RouterHelper::extractHttpRoute($pattern); |
294
|
1 |
|
if (preg_match("/" . preg_quote($route, "/") . "$/i", "/" . $home)) { |
295
|
|
|
$home_params = $params; |
296
|
|
|
} |
297
|
|
|
} |
298
|
1 |
|
if (NULL !== $home_params) { |
299
|
|
|
$this->routing['/'] = $home_params; |
300
|
|
|
} |
301
|
|
|
} |
302
|
1 |
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Método que inspecciona los directorios en busca de clases que registren rutas |
306
|
|
|
* |
307
|
|
|
* @param string $origen |
308
|
|
|
* @param string $namespace |
309
|
|
|
* @param array $routing |
310
|
|
|
* |
311
|
|
|
* @return array |
312
|
|
|
* @throws ConfigException |
313
|
|
|
*/ |
314
|
1 |
|
private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
315
|
|
|
{ |
316
|
1 |
|
$files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name("*.php"); |
317
|
1 |
|
foreach ($files as $file) { |
318
|
1 |
|
$filename = str_replace("/", '\\', str_replace($origen, '', $file->getPathname())); |
319
|
1 |
|
$routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace); |
320
|
|
|
} |
321
|
1 |
|
$this->finder = new Finder(); |
322
|
|
|
|
323
|
1 |
|
return $routing; |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* Checks that a namespace exists |
328
|
|
|
* @param string $namespace |
329
|
|
|
* @return bool |
330
|
|
|
*/ |
331
|
1 |
|
public static function exists($namespace) |
332
|
|
|
{ |
333
|
1 |
|
return (class_exists($namespace) || interface_exists($namespace) || trait_exists($namespace)); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Método que añade nuevas rutas al array de referencia |
338
|
|
|
* |
339
|
|
|
* @param string $namespace |
340
|
|
|
* @param array $routing |
341
|
|
|
* @param string $module |
342
|
|
|
* |
343
|
|
|
* @return array |
344
|
|
|
* @throws ConfigException |
345
|
|
|
*/ |
346
|
1 |
|
private function addRouting($namespace, &$routing, $module = 'PSFS') |
347
|
|
|
{ |
348
|
1 |
|
if (self::exists($namespace)) { |
349
|
1 |
|
$reflection = new \ReflectionClass($namespace); |
350
|
1 |
|
if (FALSE === $reflection->isAbstract() && FALSE === $reflection->isInterface()) { |
351
|
1 |
|
$this->extractDomain($reflection); |
352
|
1 |
|
$classComments = $reflection->getDocComment(); |
353
|
1 |
|
preg_match('/@api\ (.*)\n/im', $classComments, $apiPath); |
354
|
1 |
|
$api = ''; |
355
|
1 |
|
if (count($apiPath)) { |
356
|
|
|
$api = array_key_exists(1, $apiPath) ? $apiPath[1] : $api; |
357
|
|
|
} |
358
|
1 |
|
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
359
|
1 |
|
if (preg_match('/@route\ /i', $method->getDocComment())) { |
360
|
1 |
|
list($route, $info) = RouterHelper::extractRouteInfo($method, str_replace('\\', '', $api), str_replace('\\', '', $module)); |
|
|
|
|
361
|
|
|
|
362
|
1 |
|
if (null !== $route && null !== $info) { |
363
|
1 |
|
$info['class'] = $namespace; |
364
|
1 |
|
$routing[$route] = $info; |
365
|
|
|
} |
366
|
|
|
} |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
} |
370
|
|
|
|
371
|
1 |
|
return $routing; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
376
|
|
|
* |
377
|
|
|
* @param \ReflectionClass $class |
378
|
|
|
* |
379
|
|
|
* @return Router |
380
|
|
|
* @throws ConfigException |
381
|
|
|
*/ |
382
|
1 |
|
protected function extractDomain(\ReflectionClass $class) |
383
|
|
|
{ |
384
|
|
|
//Calculamos los dominios para las plantillas |
385
|
1 |
|
if ($class->hasConstant("DOMAIN") && !$class->isAbstract()) { |
386
|
1 |
|
if (!$this->domains) { |
387
|
1 |
|
$this->domains = []; |
388
|
|
|
} |
389
|
1 |
|
$domain = "@" . $class->getConstant("DOMAIN") . "/"; |
390
|
1 |
|
if (!array_key_exists($domain, $this->domains)) { |
391
|
1 |
|
$this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain); |
392
|
|
|
} |
393
|
|
|
} |
394
|
|
|
|
395
|
1 |
|
return $this; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Método que genera las urls amigables para usar dentro del framework |
400
|
|
|
* @return Router |
401
|
|
|
*/ |
402
|
1 |
|
public function simpatize() |
403
|
|
|
{ |
404
|
1 |
|
$translationFileName = "translations" . DIRECTORY_SEPARATOR . "routes_translations.php"; |
405
|
1 |
|
$absoluteTranslationFileName = CACHE_DIR . DIRECTORY_SEPARATOR . $translationFileName; |
406
|
1 |
|
$this->generateSlugs($absoluteTranslationFileName); |
407
|
1 |
|
GeneratorHelper::createDir(CONFIG_DIR); |
408
|
1 |
|
Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", array($this->routing, $this->slugs), Cache::JSON, TRUE); |
|
|
|
|
409
|
|
|
|
410
|
1 |
|
return $this; |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
/** |
414
|
|
|
* Método que devuelve una ruta del framework |
415
|
|
|
* |
416
|
|
|
* @param string $slug |
417
|
|
|
* @param boolean $absolute |
418
|
|
|
* @param array $params |
419
|
|
|
* |
420
|
|
|
* @return string|null |
421
|
|
|
* @throws RouterException |
422
|
|
|
*/ |
423
|
1 |
|
public function getRoute($slug = '', $absolute = FALSE, $params = []) |
424
|
|
|
{ |
425
|
1 |
|
if (strlen($slug) === 0) { |
426
|
|
|
return ($absolute) ? Request::getInstance()->getRootUrl() . '/' : '/'; |
427
|
|
|
} |
428
|
1 |
|
if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) { |
429
|
|
|
throw new RouterException(_("No existe la ruta especificada")); |
430
|
|
|
} |
431
|
1 |
|
$url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug]; |
432
|
1 |
|
if (!empty($params)) foreach ($params as $key => $value) { |
433
|
|
|
$url = str_replace("{" . $key . "}", $value, $url); |
434
|
1 |
|
} elseif (!empty($this->routing[$this->slugs[$slug]]["default"])) { |
435
|
1 |
|
$url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"]; |
|
|
|
|
436
|
|
|
} |
437
|
|
|
|
438
|
1 |
|
return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
/** |
442
|
|
|
* Método que devuelve las rutas de administración |
443
|
|
|
* @deprecated |
444
|
|
|
* @return array |
445
|
|
|
*/ |
446
|
|
|
public function getAdminRoutes() |
447
|
|
|
{ |
448
|
|
|
return AdminHelper::getAdminRoutes($this->routing); |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Método que devuelve le controlador del admin |
453
|
|
|
* @deprecated |
454
|
|
|
* @return Admin |
455
|
|
|
*/ |
456
|
|
|
public function getAdmin() |
457
|
|
|
{ |
458
|
|
|
return Admin::getInstance(); |
459
|
|
|
} |
460
|
|
|
|
461
|
|
|
/** |
462
|
|
|
* Método que extrae los dominios |
463
|
|
|
* @return array |
464
|
|
|
*/ |
465
|
1 |
|
public function getDomains() |
466
|
|
|
{ |
467
|
1 |
|
return $this->domains ?: []; |
468
|
|
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
472
|
|
|
* |
473
|
|
|
* @param string $route |
474
|
|
|
* @param array|null $action |
475
|
|
|
* @param types\Controller $class |
476
|
|
|
* @param array $params |
|
|
|
|
477
|
|
|
*/ |
478
|
|
|
protected function executeCachedRoute($route, $action, $class, $params = NULL) |
479
|
|
|
{ |
480
|
|
|
Logger::log('Executing route ' . $route, LOG_INFO); |
481
|
|
|
Security::getInstance()->setSessionKey("__CACHE__", $action); |
482
|
|
|
$cache = Cache::needCache(); |
483
|
|
|
$execute = TRUE; |
484
|
|
|
if (FALSE !== $cache && Config::getInstance()->getDebugMode() === FALSE) { |
485
|
|
|
list($path, $cacheDataName) = $this->cache->getRequestCacheHash(); |
486
|
|
|
$cachedData = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $path . $cacheDataName, |
487
|
|
|
$cache, function () { |
|
|
|
|
488
|
|
|
}); |
489
|
|
|
if (NULL !== $cachedData) { |
490
|
|
|
$headers = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $path . $cacheDataName . ".headers", |
|
|
|
|
491
|
|
|
$cache, function () { |
|
|
|
|
492
|
|
|
}, Cache::JSON); |
493
|
|
|
Template::getInstance()->renderCache($cachedData, $headers); |
494
|
|
|
$execute = FALSE; |
495
|
|
|
} |
496
|
|
|
} |
497
|
|
|
if ($execute) { |
498
|
|
|
Logger::log(_('Start executing action'), LOG_DEBUG); |
499
|
|
|
if (false === call_user_func_array(array($class, $action['method']), $params)) { |
500
|
|
|
Logger::log(_('An error ocurred trying to execute the action'), LOG_ERR, [error_get_last()]); |
501
|
|
|
} |
502
|
|
|
} |
503
|
|
|
} |
504
|
|
|
|
505
|
|
|
/** |
506
|
|
|
* Parse slugs to create translations |
507
|
|
|
* |
508
|
|
|
* @param string $absoluteTranslationFileName |
509
|
|
|
*/ |
510
|
1 |
|
private function generateSlugs($absoluteTranslationFileName) |
511
|
|
|
{ |
512
|
1 |
|
$translations = I18nHelper::generateTranslationsFile($absoluteTranslationFileName); |
513
|
1 |
|
foreach ($this->routing as $key => &$info) { |
514
|
1 |
|
$keyParts = $key; |
515
|
1 |
|
if (FALSE === strstr("#|#", $key)) { |
516
|
1 |
|
$keyParts = explode("#|#", $key); |
517
|
1 |
|
$keyParts = array_key_exists(1, $keyParts) ? $keyParts[1] : ''; |
518
|
|
|
} |
519
|
1 |
|
$slug = RouterHelper::slugify($keyParts); |
520
|
1 |
|
if (NULL !== $slug && !array_key_exists($slug, $translations)) { |
521
|
1 |
|
$translations[$slug] = $key; |
522
|
1 |
|
file_put_contents($absoluteTranslationFileName, "\$translations[\"{$slug}\"] = _(\"{$slug}\");\n", FILE_APPEND); |
|
|
|
|
523
|
|
|
} |
524
|
1 |
|
$this->slugs[$slug] = $key; |
525
|
1 |
|
$info["slug"] = $slug; |
526
|
|
|
} |
527
|
1 |
|
} |
528
|
|
|
|
529
|
|
|
} |
530
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.