|
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
|
1 |
|
$home_params = $params; |
|
313
|
|
|
} |
|
314
|
|
|
} |
|
315
|
1 |
|
if (NULL !== $home_params) { |
|
316
|
|
|
$this->routing['/'] = $home_params; |
|
317
|
|
|
} |
|
318
|
|
|
} |
|
319
|
1 |
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @param string $origen |
|
323
|
|
|
* @param string $namespace |
|
324
|
|
|
* @param array $routing |
|
325
|
|
|
* @return array |
|
326
|
|
|
* @throws ConfigException |
|
327
|
|
|
* @throws \InvalidArgumentException |
|
328
|
|
|
*/ |
|
329
|
1 |
|
private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
330
|
|
|
{ |
|
331
|
1 |
|
$files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name('*.php'); |
|
332
|
1 |
|
if($files->hasResults()) { |
|
333
|
1 |
|
foreach ($files->getIterator() as $file) { |
|
334
|
1 |
|
if(method_exists($file, 'getRelativePathname') && $namespace !== 'PSFS') { |
|
335
|
|
|
$filename = '\\' . str_replace('/', '\\', str_replace($origen, '', $file->getRelativePathname())); |
|
336
|
|
|
} else { |
|
337
|
1 |
|
$filename = str_replace('/', '\\', str_replace($origen, '', $file->getPathname())); |
|
338
|
|
|
} |
|
339
|
1 |
|
$routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace); |
|
340
|
|
|
} |
|
341
|
|
|
} |
|
342
|
1 |
|
$this->finder = new Finder(); |
|
343
|
|
|
|
|
344
|
1 |
|
return $routing; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* @param string $namespace |
|
349
|
|
|
* @return bool |
|
350
|
|
|
*/ |
|
351
|
3 |
|
public static function exists($namespace) |
|
352
|
|
|
{ |
|
353
|
3 |
|
return (class_exists($namespace) || interface_exists($namespace) || trait_exists($namespace)); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* |
|
358
|
|
|
* @param string $namespace |
|
359
|
|
|
* @param array $routing |
|
360
|
|
|
* @param string $module |
|
361
|
|
|
* |
|
362
|
|
|
* @return array |
|
363
|
|
|
* @throws ConfigException |
|
364
|
|
|
*/ |
|
365
|
1 |
|
private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
366
|
|
|
{ |
|
367
|
1 |
|
if (self::exists($namespace)) { |
|
368
|
1 |
|
if(I18nHelper::checkI18Class($namespace)) { |
|
369
|
|
|
return $routing; |
|
370
|
|
|
} |
|
371
|
1 |
|
$reflection = new \ReflectionClass($namespace); |
|
372
|
1 |
|
if (FALSE === $reflection->isAbstract() && FALSE === $reflection->isInterface()) { |
|
373
|
1 |
|
$this->extractDomain($reflection); |
|
374
|
1 |
|
$classComments = $reflection->getDocComment(); |
|
375
|
1 |
|
preg_match('/@api\ (.*)\n/im', $classComments, $apiPath); |
|
376
|
1 |
|
$api = ''; |
|
377
|
1 |
|
if (count($apiPath)) { |
|
378
|
|
|
$api = array_key_exists(1, $apiPath) ? $apiPath[1] : $api; |
|
379
|
|
|
} |
|
380
|
1 |
|
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
381
|
1 |
|
if (preg_match('/@route\ /i', $method->getDocComment())) { |
|
382
|
1 |
|
list($route, $info) = RouterHelper::extractRouteInfo($method, str_replace('\\', '', $api), str_replace('\\', '', $module)); |
|
|
|
|
|
|
383
|
|
|
|
|
384
|
1 |
|
if (null !== $route && null !== $info) { |
|
385
|
1 |
|
$info['class'] = $namespace; |
|
386
|
1 |
|
$routing[$route] = $info; |
|
387
|
|
|
} |
|
388
|
|
|
} |
|
389
|
|
|
} |
|
390
|
|
|
} |
|
391
|
|
|
} |
|
392
|
|
|
|
|
393
|
1 |
|
return $routing; |
|
394
|
|
|
} |
|
395
|
|
|
|
|
396
|
|
|
/** |
|
397
|
|
|
* |
|
398
|
|
|
* @param \ReflectionClass $class |
|
399
|
|
|
* |
|
400
|
|
|
* @return Router |
|
401
|
|
|
* @throws ConfigException |
|
402
|
|
|
*/ |
|
403
|
1 |
|
protected function extractDomain(\ReflectionClass $class) |
|
404
|
|
|
{ |
|
405
|
|
|
//Calculamos los dominios para las plantillas |
|
406
|
1 |
|
if ($class->hasConstant('DOMAIN') && !$class->isAbstract()) { |
|
407
|
1 |
|
if (!$this->domains) { |
|
|
|
|
|
|
408
|
1 |
|
$this->domains = []; |
|
409
|
|
|
} |
|
410
|
1 |
|
$domain = '@' . $class->getConstant('DOMAIN') . '/'; |
|
411
|
1 |
|
if (!array_key_exists($domain, $this->domains)) { |
|
412
|
1 |
|
$this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain); |
|
413
|
|
|
} |
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
1 |
|
return $this; |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* @return $this |
|
421
|
|
|
* @throws exception\GeneratorException |
|
422
|
|
|
* @throws ConfigException |
|
423
|
|
|
*/ |
|
424
|
1 |
|
public function simpatize() |
|
425
|
|
|
{ |
|
426
|
1 |
|
$translationFileName = 'translations' . DIRECTORY_SEPARATOR . 'routes_translations.php'; |
|
427
|
1 |
|
$absoluteTranslationFileName = CACHE_DIR . DIRECTORY_SEPARATOR . $translationFileName; |
|
428
|
1 |
|
$this->generateSlugs($absoluteTranslationFileName); |
|
429
|
1 |
|
GeneratorHelper::createDir(CONFIG_DIR); |
|
430
|
1 |
|
Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'urls.json', array($this->routing, $this->slugs), Cache::JSON, TRUE); |
|
|
|
|
|
|
431
|
|
|
|
|
432
|
1 |
|
return $this; |
|
433
|
|
|
} |
|
434
|
|
|
|
|
435
|
|
|
/** |
|
436
|
|
|
* @param string $slug |
|
437
|
|
|
* @param boolean $absolute |
|
438
|
|
|
* @param array $params |
|
439
|
|
|
* |
|
440
|
|
|
* @return string|null |
|
441
|
|
|
* @throws RouterException |
|
442
|
|
|
*/ |
|
443
|
3 |
|
public function getRoute($slug = '', $absolute = FALSE, array $params = []) |
|
444
|
|
|
{ |
|
445
|
3 |
|
if ('' === $slug) { |
|
446
|
1 |
|
return $absolute ? Request::getInstance()->getRootUrl() . '/' : '/'; |
|
447
|
|
|
} |
|
448
|
3 |
|
if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) { |
|
449
|
1 |
|
throw new RouterException(_('No existe la ruta especificada')); |
|
450
|
|
|
} |
|
451
|
3 |
|
$url = $absolute ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug]; |
|
452
|
3 |
|
if (!empty($params)) { |
|
453
|
|
|
foreach ($params as $key => $value) { |
|
454
|
|
|
$url = str_replace('{' . $key . '}', $value, $url); |
|
455
|
|
|
} |
|
456
|
3 |
|
} elseif (!empty($this->routing[$this->slugs[$slug]]['default'])) { |
|
457
|
3 |
|
$url = $absolute ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]['default'] : $this->routing[$this->slugs[$slug]]['default']; |
|
|
|
|
|
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
3 |
|
return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url); |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/** |
|
464
|
|
|
* @deprecated |
|
465
|
|
|
* @return array |
|
466
|
|
|
*/ |
|
467
|
|
|
public function getAdminRoutes() |
|
468
|
|
|
{ |
|
469
|
|
|
return AdminHelper::getAdminRoutes($this->routing); |
|
470
|
|
|
} |
|
471
|
|
|
|
|
472
|
|
|
/** |
|
473
|
|
|
* @deprecated |
|
474
|
|
|
* @return Admin |
|
475
|
|
|
*/ |
|
476
|
|
|
public function getAdmin() |
|
477
|
|
|
{ |
|
478
|
|
|
return Admin::getInstance(); |
|
479
|
|
|
} |
|
480
|
|
|
|
|
481
|
|
|
/** |
|
482
|
|
|
* @return array |
|
483
|
|
|
*/ |
|
484
|
1 |
|
public function getDomains() |
|
485
|
|
|
{ |
|
486
|
1 |
|
return $this->domains ?: []; |
|
487
|
|
|
} |
|
488
|
|
|
|
|
489
|
|
|
/** |
|
490
|
|
|
* @param string $route |
|
491
|
|
|
* @param array $action |
|
492
|
|
|
* @param string $class |
|
493
|
|
|
* @param array $params |
|
|
|
|
|
|
494
|
|
|
* @throws exception\GeneratorException |
|
495
|
|
|
* @throws ConfigException |
|
496
|
|
|
*/ |
|
497
|
|
|
protected function executeCachedRoute($route, $action, $class, $params = NULL) |
|
498
|
|
|
{ |
|
499
|
|
|
Logger::log('Executing route ' . $route, LOG_INFO); |
|
500
|
|
|
$action['params'] = array_merge($action['params'], $params, Request::getInstance()->getQueryParams()); |
|
501
|
|
|
Security::getInstance()->setSessionKey(Cache::CACHE_SESSION_VAR, $action); |
|
502
|
|
|
$cache = Cache::needCache(); |
|
503
|
|
|
$execute = TRUE; |
|
504
|
|
|
if (FALSE !== $cache && $action['http'] === 'GET' && Config::getParam('debug') === FALSE) { |
|
505
|
|
|
list($path, $cacheDataName) = $this->cache->getRequestCacheHash(); |
|
506
|
|
|
$cachedData = $this->cache->readFromCache('json' . DIRECTORY_SEPARATOR . $path . $cacheDataName, |
|
507
|
|
|
$cache); |
|
|
|
|
|
|
508
|
|
|
if (NULL !== $cachedData) { |
|
509
|
|
|
$headers = $this->cache->readFromCache('json' . DIRECTORY_SEPARATOR . $path . $cacheDataName . '.headers', |
|
|
|
|
|
|
510
|
|
|
$cache, null, Cache::JSON); |
|
|
|
|
|
|
511
|
|
|
Template::getInstance()->renderCache($cachedData, $headers); |
|
512
|
|
|
$execute = FALSE; |
|
513
|
|
|
} |
|
514
|
|
|
} |
|
515
|
|
|
if ($execute) { |
|
516
|
|
|
Logger::log(_('Start executing action')); |
|
517
|
|
|
if (false === call_user_func_array(array($class, $action['method']), $params)) { |
|
518
|
|
|
Logger::log(_('An error occurred trying to execute the action'), LOG_ERR, [error_get_last()]); |
|
519
|
|
|
} |
|
520
|
|
|
} |
|
521
|
|
|
} |
|
522
|
|
|
|
|
523
|
|
|
/** |
|
524
|
|
|
* Parse slugs to create translations |
|
525
|
|
|
* |
|
526
|
|
|
* @param string $absoluteTranslationFileName |
|
527
|
|
|
*/ |
|
528
|
1 |
|
private function generateSlugs($absoluteTranslationFileName) |
|
529
|
|
|
{ |
|
530
|
1 |
|
$translations = I18nHelper::generateTranslationsFile($absoluteTranslationFileName); |
|
531
|
1 |
|
foreach ($this->routing as $key => &$info) { |
|
532
|
1 |
|
$keyParts = explode('#|#', $key); |
|
533
|
1 |
|
$keyParts = array_key_exists(1, $keyParts) ? $keyParts[1] : $keyParts[0]; |
|
534
|
1 |
|
$slug = RouterHelper::slugify($keyParts); |
|
535
|
1 |
|
if (NULL !== $slug && !array_key_exists($slug, $translations)) { |
|
536
|
1 |
|
$translations[$slug] = $info['label']; |
|
537
|
1 |
|
file_put_contents($absoluteTranslationFileName, "\$translations[\"{$slug}\"] = _(\"{$info['label']}\");\n", FILE_APPEND); |
|
|
|
|
|
|
538
|
|
|
} |
|
539
|
1 |
|
$this->slugs[$slug] = $key; |
|
540
|
1 |
|
$info['slug'] = $slug; |
|
541
|
|
|
} |
|
542
|
1 |
|
} |
|
543
|
|
|
|
|
544
|
|
|
/** |
|
545
|
|
|
* @param bool $hydrateRoute |
|
546
|
|
|
* @param $modulePath |
|
547
|
|
|
* @param $externalModulePath |
|
548
|
|
|
*/ |
|
549
|
|
|
private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
|
550
|
|
|
{ |
|
551
|
|
|
$extModule = $modulePath->getBasename(); |
|
552
|
|
|
$moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php'); |
|
|
|
|
|
|
553
|
|
|
if(file_exists($moduleAutoloader)) { |
|
554
|
|
|
include_once $moduleAutoloader; |
|
555
|
|
|
if ($hydrateRoute) { |
|
556
|
|
|
$this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, '\\' . $extModule, $this->routing); |
|
|
|
|
|
|
557
|
|
|
} |
|
558
|
|
|
} |
|
559
|
|
|
} |
|
560
|
|
|
|
|
561
|
|
|
/** |
|
562
|
|
|
* @param $hydrateRoute |
|
563
|
|
|
* @param $module |
|
564
|
|
|
* @return mixed |
|
565
|
|
|
*/ |
|
566
|
1 |
|
private function loadExternalModule($hydrateRoute, $module) |
|
567
|
|
|
{ |
|
568
|
|
|
try { |
|
569
|
1 |
|
$module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module); |
|
570
|
1 |
|
$externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src'; |
|
571
|
1 |
|
if(file_exists($externalModulePath)) { |
|
572
|
|
|
$externalModule = $this->finder->directories()->in($externalModulePath)->depth(0); |
|
573
|
|
|
if($externalModule->hasResults()) { |
|
574
|
|
|
foreach ($externalModule->getIterator() as $modulePath) { |
|
575
|
|
|
$this->loadExternalAutoloader($hydrateRoute, $modulePath, $externalModulePath); |
|
576
|
|
|
} |
|
577
|
1 |
|
} |
|
578
|
1 |
|
} |
|
579
|
1 |
|
} catch (\Exception $e) { |
|
580
|
|
|
Logger::log($e->getMessage(), LOG_WARNING); |
|
581
|
1 |
|
$module = null; |
|
582
|
|
|
} |
|
583
|
|
|
return $module; |
|
584
|
|
|
} |
|
585
|
|
|
|
|
586
|
|
|
} |
|
587
|
|
|
|
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.