1
|
|
|
<?php |
2
|
|
|
namespace PSFS\base; |
3
|
|
|
|
4
|
|
|
use PSFS\base\config\Config; |
5
|
|
|
use PSFS\base\dto\JsonResponse; |
6
|
|
|
use PSFS\base\exception\AccessDeniedException; |
7
|
|
|
use PSFS\base\exception\AdminCredentialsException; |
8
|
|
|
use PSFS\base\exception\ConfigException; |
9
|
|
|
use PSFS\base\exception\RouterException; |
10
|
|
|
use PSFS\base\types\helpers\AdminHelper; |
11
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
12
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
13
|
|
|
use PSFS\base\types\helpers\RouterHelper; |
14
|
|
|
use PSFS\base\types\helpers\SecurityHelper; |
15
|
|
|
use PSFS\base\types\traits\SingletonTrait; |
16
|
|
|
use PSFS\controller\base\Admin; |
17
|
|
|
use PSFS\services\AdminServices; |
18
|
|
|
use Symfony\Component\Finder\Finder; |
19
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class Router |
23
|
|
|
* @package PSFS |
24
|
|
|
*/ |
25
|
|
|
class Router |
26
|
|
|
{ |
27
|
|
|
use SingletonTrait; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $routing = []; |
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $slugs = []; |
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $domains = []; |
41
|
|
|
/** |
42
|
|
|
* @var Finder $finder |
43
|
|
|
*/ |
44
|
|
|
private $finder; |
45
|
|
|
/** |
46
|
|
|
* @var \PSFS\base\Cache $cache |
47
|
|
|
*/ |
48
|
|
|
private $cache; |
49
|
|
|
/** |
50
|
|
|
* @var bool headersSent |
51
|
|
|
*/ |
52
|
|
|
protected $headersSent = false; |
53
|
|
|
/** |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
protected $cacheType = Cache::JSON; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Router constructor. |
60
|
|
|
* @throws exception\GeneratorException |
61
|
|
|
* @throws ConfigException |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
*/ |
64
|
1 |
|
public function __construct() |
65
|
|
|
{ |
66
|
1 |
|
$this->finder = new Finder(); |
67
|
1 |
|
$this->cache = Cache::getInstance(); |
68
|
1 |
|
$this->init(); |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @throws exception\GeneratorException |
73
|
|
|
* @throws ConfigException |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
1 |
|
public function init() |
77
|
|
|
{ |
78
|
1 |
|
list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'urls.json', $this->cacheType, TRUE); |
79
|
1 |
|
if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
80
|
1 |
|
$this->debugLoad(); |
81
|
|
|
} else { |
82
|
|
|
$this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', $this->cacheType, TRUE); |
83
|
|
|
} |
84
|
1 |
|
$this->checkExternalModules(false); |
85
|
1 |
|
$this->setLoaded(); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws exception\GeneratorException |
90
|
|
|
* @throws ConfigException |
91
|
|
|
* @throws \InvalidArgumentException |
92
|
|
|
*/ |
93
|
1 |
|
private function debugLoad() { |
94
|
1 |
|
Logger::log('Begin routes load'); |
95
|
1 |
|
$this->hydrateRouting(); |
96
|
1 |
|
$this->simpatize(); |
97
|
1 |
|
Logger::log('End routes load'); |
98
|
1 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \Exception|NULL $e |
102
|
|
|
* @param bool $isJson |
103
|
|
|
* @return string |
104
|
|
|
* @throws RouterException |
105
|
|
|
*/ |
106
|
|
|
public function httpNotFound(\Exception $e = NULL, $isJson = false) |
107
|
|
|
{ |
108
|
|
|
Logger::log('Throw not found exception'); |
109
|
|
|
if (NULL === $e) { |
110
|
|
|
Logger::log('Not found page thrown without previous exception', LOG_WARNING); |
111
|
|
|
$e = new \Exception(_('Page not found'), 404); |
112
|
|
|
} |
113
|
|
|
$template = Template::getInstance()->setStatus($e->getCode()); |
114
|
|
|
if ($isJson || false !== stripos(Request::getInstance()->getServer('CONTENT_TYPE'), 'json')) { |
115
|
|
|
$response = new JsonResponse(null, false, 0, 0, $e->getMessage()); |
116
|
|
|
return $template->output(json_encode($response), 'application/json'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$not_found_route = Config::getParam('route.404'); |
120
|
|
|
if(null !== $not_found_route) { |
121
|
|
|
Request::getInstance()->redirect($this->getRoute($not_found_route, true)); |
122
|
|
|
} else { |
123
|
|
|
return $template->render('error.html.twig', array( |
124
|
|
|
'exception' => $e, |
125
|
|
|
'trace' => $e->getTraceAsString(), |
126
|
|
|
'error_page' => TRUE, |
127
|
|
|
)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
1 |
|
public function getSlugs() |
135
|
|
|
{ |
136
|
1 |
|
return $this->slugs; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
2 |
|
public function getRoutes() { |
143
|
2 |
|
return $this->routing; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Method that extract all routes in the platform |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
1 |
|
public function getAllRoutes() |
151
|
|
|
{ |
152
|
1 |
|
$routes = []; |
153
|
1 |
|
foreach ($this->getRoutes() as $path => $route) { |
154
|
1 |
|
if (array_key_exists('slug', $route)) { |
155
|
1 |
|
$routes[$route['slug']] = $path; |
156
|
|
|
} |
157
|
|
|
} |
158
|
1 |
|
return $routes; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param string|null $route |
163
|
|
|
* |
164
|
|
|
* @throws \Exception |
165
|
|
|
* @return string HTML |
166
|
|
|
*/ |
167
|
1 |
|
public function execute($route) |
168
|
|
|
{ |
169
|
1 |
|
Logger::log('Executing the request'); |
170
|
|
|
try { |
171
|
|
|
//Search action and execute |
172
|
1 |
|
$this->searchAction($route); |
173
|
1 |
|
} catch (AccessDeniedException $e) { |
174
|
|
|
Logger::log(_('Solicitamos credenciales de acceso a zona restringida'), LOG_WARNING, ['file' => $e->getFile() . '[' . $e->getLine() . ']']); |
175
|
|
|
return Admin::staticAdminLogon($route); |
176
|
1 |
|
} catch (RouterException $r) { |
177
|
1 |
|
Logger::log($r->getMessage(), LOG_WARNING); |
178
|
|
|
} catch (\Exception $e) { |
179
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
180
|
|
|
throw $e; |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
throw new RouterException(_('Página no encontrada'), 404); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param $route |
188
|
|
|
* @throws AccessDeniedException |
189
|
|
|
* @throws AdminCredentialsException |
190
|
|
|
* @throws RouterException |
191
|
|
|
* @throws \Exception |
192
|
|
|
*/ |
193
|
1 |
|
protected function searchAction($route) |
194
|
|
|
{ |
195
|
1 |
|
Logger::log('Searching action to execute: ' . $route, LOG_INFO); |
196
|
|
|
//Revisamos si tenemos la ruta registrada |
197
|
1 |
|
$parts = parse_url($route); |
198
|
1 |
|
$path = array_key_exists('path', $parts) ? $parts['path'] : $route; |
199
|
1 |
|
$httpRequest = Request::getInstance()->getMethod(); |
200
|
1 |
|
foreach ($this->routing as $pattern => $action) { |
201
|
1 |
|
list($httpMethod, $routePattern) = RouterHelper::extractHttpRoute($pattern); |
202
|
1 |
|
$matched = RouterHelper::matchRoutePattern($routePattern, $path); |
203
|
1 |
|
if ($matched && ($httpMethod === 'ALL' || $httpRequest === $httpMethod) && RouterHelper::compareSlashes($routePattern, $path)) { |
204
|
|
|
// Checks restricted access |
205
|
|
|
SecurityHelper::checkRestrictedAccess($route); |
206
|
|
|
$get = RouterHelper::extractComponents($route, $routePattern); |
207
|
|
|
/** @var $class \PSFS\base\types\Controller */ |
208
|
|
|
$class = RouterHelper::getClassToCall($action); |
209
|
|
|
try { |
210
|
|
|
if($this->checkRequirements($action, $get)) { |
211
|
|
|
$this->executeCachedRoute($route, $action, $class, $get); |
|
|
|
|
212
|
|
|
} else { |
213
|
|
|
throw new RouterException(_('La ruta no es válida'), 400); |
214
|
|
|
} |
215
|
|
|
} catch (\Exception $e) { |
216
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
217
|
1 |
|
throw $e; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
1 |
|
throw new RouterException(_('Ruta no encontrada')); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param array $action |
226
|
|
|
* @param array $params |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
private function checkRequirements(array $action, $params = []) { |
230
|
|
|
if(!empty($params) && !empty($action['requirements'])) { |
231
|
|
|
$checked = 0; |
232
|
|
|
foreach(array_keys($params) as $key) { |
233
|
|
|
if(in_array($key, $action['requirements'], true)) { |
234
|
|
|
$checked++; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
$valid = count($action['requirements']) === $checked; |
238
|
|
|
} else { |
239
|
|
|
$valid = true; |
240
|
|
|
} |
241
|
|
|
return $valid; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return string HTML |
246
|
|
|
*/ |
247
|
|
|
protected function sentAuthHeader() |
248
|
|
|
{ |
249
|
|
|
return AdminServices::getInstance()->setAdminHeaders(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return string|null |
254
|
|
|
*/ |
255
|
1 |
|
private function getExternalModules() { |
256
|
1 |
|
$externalModules = Config::getParam('modules.extend', ''); |
257
|
1 |
|
$externalModules .= ',psfs/auth'; |
258
|
1 |
|
return $externalModules; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @param boolean $hydrateRoute |
263
|
|
|
*/ |
264
|
1 |
|
private function checkExternalModules($hydrateRoute = true) |
265
|
|
|
{ |
266
|
1 |
|
$externalModules = $this->getExternalModules(); |
267
|
1 |
|
if ('' !== $externalModules) { |
268
|
1 |
|
$externalModules = explode(',', $externalModules); |
269
|
1 |
|
foreach ($externalModules as &$module) { |
270
|
1 |
|
$module = $this->loadExternalModule($hydrateRoute, $module); |
271
|
|
|
} |
272
|
|
|
} |
273
|
1 |
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @throws exception\GeneratorException |
277
|
|
|
* @throws ConfigException |
278
|
|
|
* @throws \InvalidArgumentException |
279
|
|
|
*/ |
280
|
1 |
|
private function generateRouting() |
281
|
|
|
{ |
282
|
1 |
|
$base = SOURCE_DIR; |
283
|
1 |
|
$modulesPath = realpath(CORE_DIR); |
284
|
1 |
|
$this->routing = $this->inspectDir($base, 'PSFS', array()); |
285
|
1 |
|
$this->checkExternalModules(); |
286
|
1 |
|
if (file_exists($modulesPath)) { |
287
|
|
|
$modules = $this->finder->directories()->in($modulesPath)->depth(0); |
288
|
|
|
if($modules->hasResults()) { |
289
|
|
|
foreach ($modules->getIterator() as $modulePath) { |
290
|
|
|
$module = $modulePath->getBasename(); |
291
|
|
|
$this->routing = $this->inspectDir($modulesPath . DIRECTORY_SEPARATOR . $module, $module, $this->routing); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
1 |
|
$this->cache->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', $this->domains, Cache::JSON, TRUE); |
296
|
1 |
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* @throws exception\GeneratorException |
300
|
|
|
* @throws ConfigException |
301
|
|
|
* @throws \InvalidArgumentException |
302
|
|
|
*/ |
303
|
1 |
|
public function hydrateRouting() |
304
|
|
|
{ |
305
|
1 |
|
$this->generateRouting(); |
306
|
1 |
|
$home = Config::getParam('home.action'); |
307
|
1 |
|
if (NULL !== $home || $home !== '') { |
308
|
1 |
|
$home_params = NULL; |
309
|
1 |
|
foreach ($this->routing as $pattern => $params) { |
310
|
1 |
|
list($method, $route) = RouterHelper::extractHttpRoute($pattern); |
311
|
1 |
|
if (preg_match('/' . preg_quote($route, '/') . '$/i', '/' . $home)) { |
312
|
|
|
$home_params = $params; |
313
|
|
|
} |
314
|
1 |
|
unset($method); |
315
|
|
|
} |
316
|
1 |
|
if (NULL !== $home_params) { |
317
|
|
|
$this->routing['/'] = $home_params; |
318
|
|
|
} |
319
|
|
|
} |
320
|
1 |
|
} |
321
|
|
|
|
322
|
|
|
/** |
323
|
|
|
* @param string $origen |
324
|
|
|
* @param string $namespace |
325
|
|
|
* @param array $routing |
326
|
|
|
* @return array |
327
|
|
|
* @throws ConfigException |
328
|
|
|
* @throws \InvalidArgumentException |
329
|
|
|
*/ |
330
|
1 |
|
private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
331
|
|
|
{ |
332
|
1 |
|
$files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name('*.php'); |
333
|
1 |
|
if($files->hasResults()) { |
334
|
1 |
|
foreach ($files->getIterator() as $file) { |
335
|
1 |
|
if(method_exists($file, 'getRelativePathname') && $namespace !== 'PSFS') { |
336
|
|
|
$filename = '\\' . str_replace('/', '\\', str_replace($origen, '', $file->getRelativePathname())); |
337
|
|
|
} else { |
338
|
1 |
|
$filename = str_replace('/', '\\', str_replace($origen, '', $file->getPathname())); |
339
|
|
|
} |
340
|
1 |
|
$routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace); |
341
|
|
|
} |
342
|
|
|
} |
343
|
1 |
|
$this->finder = new Finder(); |
344
|
|
|
|
345
|
1 |
|
return $routing; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* @param string $namespace |
350
|
|
|
* @return bool |
351
|
|
|
*/ |
352
|
3 |
|
public static function exists($namespace) |
353
|
|
|
{ |
354
|
3 |
|
return (class_exists($namespace) || interface_exists($namespace) || trait_exists($namespace)); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* |
359
|
|
|
* @param string $namespace |
360
|
|
|
* @param array $routing |
361
|
|
|
* @param string $module |
362
|
|
|
* |
363
|
|
|
* @return array |
364
|
|
|
* @throws ConfigException |
365
|
|
|
*/ |
366
|
1 |
|
private function addRouting($namespace, &$routing, $module = 'PSFS') |
367
|
|
|
{ |
368
|
1 |
|
if (self::exists($namespace)) { |
369
|
1 |
|
if(I18nHelper::checkI18Class($namespace)) { |
370
|
|
|
return $routing; |
371
|
|
|
} |
372
|
1 |
|
$reflection = new \ReflectionClass($namespace); |
373
|
1 |
|
if (false === $reflection->isAbstract() && FALSE === $reflection->isInterface()) { |
374
|
1 |
|
$this->extractDomain($reflection); |
375
|
1 |
|
$classComments = $reflection->getDocComment(); |
376
|
1 |
|
preg_match('/@api\ (.*)\n/im', $classComments, $apiPath); |
377
|
1 |
|
$api = ''; |
378
|
1 |
|
if (count($apiPath)) { |
379
|
|
|
$api = array_key_exists(1, $apiPath) ? $apiPath[1] : $api; |
380
|
|
|
} |
381
|
1 |
|
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
382
|
1 |
|
if (preg_match('/@route\ /i', $method->getDocComment())) { |
383
|
1 |
|
list($route, $info) = RouterHelper::extractRouteInfo($method, str_replace('\\', '', $api), str_replace('\\', '', $module)); |
384
|
|
|
|
385
|
1 |
|
if (null !== $route && null !== $info) { |
386
|
1 |
|
$info['class'] = $namespace; |
387
|
1 |
|
$routing[$route] = $info; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
} |
391
|
|
|
} |
392
|
|
|
} |
393
|
|
|
|
394
|
1 |
|
return $routing; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
/** |
398
|
|
|
* |
399
|
|
|
* @param \ReflectionClass $class |
400
|
|
|
* |
401
|
|
|
* @return Router |
402
|
|
|
* @throws ConfigException |
403
|
|
|
*/ |
404
|
1 |
|
protected function extractDomain(\ReflectionClass $class) |
405
|
|
|
{ |
406
|
|
|
//Calculamos los dominios para las plantillas |
407
|
1 |
|
if ($class->hasConstant('DOMAIN') && !$class->isAbstract()) { |
408
|
1 |
|
if (!$this->domains) { |
|
|
|
|
409
|
1 |
|
$this->domains = []; |
410
|
|
|
} |
411
|
1 |
|
$domain = '@' . $class->getConstant('DOMAIN') . '/'; |
412
|
1 |
|
if (!array_key_exists($domain, $this->domains)) { |
413
|
1 |
|
$this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
417
|
1 |
|
return $this; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* @return $this |
422
|
|
|
* @throws exception\GeneratorException |
423
|
|
|
* @throws ConfigException |
424
|
|
|
*/ |
425
|
1 |
|
public function simpatize() |
426
|
|
|
{ |
427
|
1 |
|
$translationFileName = 'translations' . DIRECTORY_SEPARATOR . 'routes_translations.php'; |
428
|
1 |
|
$absoluteTranslationFileName = CACHE_DIR . DIRECTORY_SEPARATOR . $translationFileName; |
429
|
1 |
|
$this->generateSlugs($absoluteTranslationFileName); |
430
|
1 |
|
GeneratorHelper::createDir(CONFIG_DIR); |
431
|
1 |
|
Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'urls.json', array($this->routing, $this->slugs), Cache::JSON, TRUE); |
432
|
|
|
|
433
|
1 |
|
return $this; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
/** |
437
|
|
|
* @param string $slug |
438
|
|
|
* @param boolean $absolute |
439
|
|
|
* @param array $params |
440
|
|
|
* |
441
|
|
|
* @return string|null |
442
|
|
|
* @throws RouterException |
443
|
|
|
*/ |
444
|
3 |
|
public function getRoute($slug = '', $absolute = FALSE, array $params = []) |
445
|
|
|
{ |
446
|
3 |
|
if ('' === $slug) { |
447
|
1 |
|
return $absolute ? Request::getInstance()->getRootUrl() . '/' : '/'; |
448
|
|
|
} |
449
|
3 |
|
if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) { |
|
|
|
|
450
|
1 |
|
throw new RouterException(_('No existe la ruta especificada')); |
451
|
|
|
} |
452
|
3 |
|
$url = $absolute ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug]; |
453
|
3 |
|
if (!empty($params)) { |
454
|
|
|
foreach ($params as $key => $value) { |
455
|
|
|
$url = str_replace('{' . $key . '}', $value, $url); |
456
|
|
|
} |
457
|
3 |
|
} elseif (!empty($this->routing[$this->slugs[$slug]]['default'])) { |
458
|
3 |
|
$url = $absolute ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]['default'] : $this->routing[$this->slugs[$slug]]['default']; |
459
|
|
|
} |
460
|
|
|
|
461
|
3 |
|
return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url); |
462
|
|
|
} |
463
|
|
|
|
464
|
|
|
/** |
465
|
|
|
* @deprecated |
466
|
|
|
* @return array |
467
|
|
|
*/ |
468
|
|
|
public function getAdminRoutes() |
469
|
|
|
{ |
470
|
|
|
return AdminHelper::getAdminRoutes($this->routing); |
471
|
|
|
} |
472
|
|
|
|
473
|
|
|
/** |
474
|
|
|
* @deprecated |
475
|
|
|
* @return Admin |
476
|
|
|
*/ |
477
|
|
|
public function getAdmin() |
478
|
|
|
{ |
479
|
|
|
return Admin::getInstance(); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* @return array |
484
|
|
|
*/ |
485
|
1 |
|
public function getDomains() |
486
|
|
|
{ |
487
|
1 |
|
return $this->domains ?: []; |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* @param string $class |
492
|
|
|
* @param string $method |
493
|
|
|
*/ |
494
|
|
|
private function checkPreActions($class, $method) { |
495
|
|
|
$preAction = 'pre' . ucfirst($method); |
496
|
|
|
if(method_exists($class, $preAction)) { |
497
|
|
|
Logger::log(_('Pre action invoked')); |
498
|
|
|
try { |
499
|
|
|
if(false === call_user_func_array([$class, $preAction])) { |
|
|
|
|
500
|
|
|
Logger::log(_('Pre action failed'), LOG_ERR, [error_get_last()]); |
501
|
|
|
error_clear_last(); |
502
|
|
|
} |
503
|
|
|
} catch (\Exception $e) { |
504
|
|
|
Logger::log($e->getMessage(), LOG_ERR, [$class, $method]); |
505
|
|
|
} |
506
|
|
|
} |
507
|
|
|
} |
508
|
|
|
|
509
|
|
|
/** |
510
|
|
|
* @param string $route |
511
|
|
|
* @param array $action |
512
|
|
|
* @param string $class |
513
|
|
|
* @param array $params |
514
|
|
|
* @throws exception\GeneratorException |
515
|
|
|
* @throws ConfigException |
516
|
|
|
*/ |
517
|
|
|
protected function executeCachedRoute($route, $action, $class, $params = NULL) |
518
|
|
|
{ |
519
|
|
|
Logger::log('Executing route ' . $route, LOG_INFO); |
520
|
|
|
$action['params'] = array_merge($action['params'], $params, Request::getInstance()->getQueryParams()); |
521
|
|
|
Security::getInstance()->setSessionKey(Cache::CACHE_SESSION_VAR, $action); |
522
|
|
|
$cache = Cache::needCache(); |
523
|
|
|
$execute = TRUE; |
524
|
|
|
if (FALSE !== $cache && $action['http'] === 'GET' && Config::getParam('debug') === FALSE) { |
525
|
|
|
list($path, $cacheDataName) = $this->cache->getRequestCacheHash(); |
526
|
|
|
$cachedData = $this->cache->readFromCache('json' . DIRECTORY_SEPARATOR . $path . $cacheDataName, $cache); |
|
|
|
|
527
|
|
|
if (NULL !== $cachedData) { |
528
|
|
|
$headers = $this->cache->readFromCache('json' . DIRECTORY_SEPARATOR . $path . $cacheDataName . '.headers', $cache, null, Cache::JSON); |
529
|
|
|
Template::getInstance()->renderCache($cachedData, $headers); |
530
|
|
|
$execute = FALSE; |
531
|
|
|
} |
532
|
|
|
} |
533
|
|
|
if ($execute) { |
534
|
|
|
Logger::log(_('Start executing action')); |
535
|
|
|
$this->checkPreActions($class, $action['method']); |
536
|
|
|
if (false === call_user_func_array([$class, $action['method']], $params)) { |
|
|
|
|
537
|
|
|
Logger::log(_('An error occurred trying to execute the action'), LOG_ERR, [error_get_last()]); |
538
|
|
|
} |
539
|
|
|
} |
540
|
|
|
} |
541
|
|
|
|
542
|
|
|
/** |
543
|
|
|
* Parse slugs to create translations |
544
|
|
|
* |
545
|
|
|
* @param string $absoluteTranslationFileName |
546
|
|
|
*/ |
547
|
1 |
|
private function generateSlugs($absoluteTranslationFileName) |
548
|
|
|
{ |
549
|
1 |
|
$translations = I18nHelper::generateTranslationsFile($absoluteTranslationFileName); |
550
|
1 |
|
foreach ($this->routing as $key => &$info) { |
551
|
1 |
|
$keyParts = explode('#|#', $key); |
552
|
1 |
|
$keyParts = array_key_exists(1, $keyParts) ? $keyParts[1] : $keyParts[0]; |
553
|
1 |
|
$slug = RouterHelper::slugify($keyParts); |
554
|
1 |
|
if (NULL !== $slug && !array_key_exists($slug, $translations)) { |
555
|
1 |
|
$translations[$slug] = $info['label']; |
556
|
1 |
|
file_put_contents($absoluteTranslationFileName, "\$translations[\"{$slug}\"] = _(\"{$info['label']}\");\n", FILE_APPEND); |
557
|
|
|
} |
558
|
1 |
|
$this->slugs[$slug] = $key; |
559
|
1 |
|
$info['slug'] = $slug; |
560
|
|
|
} |
561
|
1 |
|
} |
562
|
|
|
|
563
|
|
|
/** |
564
|
|
|
* @param bool $hydrateRoute |
565
|
|
|
* @param $modulePath |
566
|
|
|
* @param $externalModulePath |
567
|
|
|
*/ |
568
|
|
|
private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
569
|
|
|
{ |
570
|
|
|
$extModule = $modulePath->getBasename(); |
571
|
|
|
$moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php'); |
572
|
|
|
if(file_exists($moduleAutoloader)) { |
573
|
|
|
include_once $moduleAutoloader; |
574
|
|
|
if ($hydrateRoute) { |
575
|
|
|
$this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, '\\' . $extModule, $this->routing); |
576
|
|
|
} |
577
|
|
|
} |
578
|
|
|
} |
579
|
|
|
|
580
|
|
|
/** |
581
|
|
|
* @param $hydrateRoute |
582
|
|
|
* @param $module |
583
|
|
|
* @return mixed |
584
|
|
|
*/ |
585
|
1 |
|
private function loadExternalModule($hydrateRoute, $module) |
586
|
|
|
{ |
587
|
|
|
try { |
588
|
1 |
|
$module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module); |
589
|
1 |
|
$externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src'; |
590
|
1 |
|
if(file_exists($externalModulePath)) { |
591
|
|
|
$externalModule = $this->finder->directories()->in($externalModulePath)->depth(0); |
592
|
|
|
if($externalModule->hasResults()) { |
593
|
|
|
foreach ($externalModule->getIterator() as $modulePath) { |
594
|
1 |
|
$this->loadExternalAutoloader($hydrateRoute, $modulePath, $externalModulePath); |
595
|
|
|
} |
596
|
|
|
} |
597
|
|
|
} |
598
|
|
|
} catch (\Exception $e) { |
599
|
|
|
Logger::log($e->getMessage(), LOG_WARNING); |
600
|
|
|
$module = null; |
601
|
|
|
} |
602
|
1 |
|
return $module; |
603
|
|
|
} |
604
|
|
|
|
605
|
|
|
} |
606
|
|
|
|