|
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\ConfigException; |
|
8
|
|
|
use PSFS\base\exception\RouterException; |
|
9
|
|
|
use PSFS\base\types\helpers\AdminHelper; |
|
10
|
|
|
use PSFS\base\types\helpers\GeneratorHelper; |
|
11
|
|
|
use PSFS\base\types\helpers\I18nHelper; |
|
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
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
|
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
|
|
|
* Initializer 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 |
|
if (empty($this->routing) || Config::getInstance()->getDebugMode()) { |
|
67
|
1 |
|
$this->debugLoad(); |
|
68
|
|
|
} else { |
|
69
|
|
|
$this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->cacheType, TRUE); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
1 |
|
$this->checkExternalModules(false); |
|
72
|
1 |
|
$this->setLoaded(true); |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Load routes and domains and store them |
|
77
|
|
|
*/ |
|
78
|
1 |
|
private function debugLoad() { |
|
79
|
1 |
|
Logger::log('Begin routes load', LOG_DEBUG); |
|
80
|
1 |
|
$this->hydrateRouting(); |
|
81
|
1 |
|
$this->simpatize(); |
|
82
|
1 |
|
Logger::log('End routes load', LOG_DEBUG); |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Método que deriva un error HTTP de página no encontrada |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Exception $e |
|
|
|
|
|
|
89
|
|
|
* @param boolean $isJson |
|
90
|
|
|
* |
|
91
|
|
|
* @return string HTML |
|
92
|
|
|
*/ |
|
93
|
|
|
public function httpNotFound(\Exception $e = NULL, $isJson = false) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
Logger::log('Throw not found exception'); |
|
96
|
|
|
if (NULL === $e) { |
|
97
|
|
|
Logger::log('Not found page throwed without previous exception', LOG_WARNING); |
|
98
|
|
|
$e = new \Exception(_('Page not found'), 404); |
|
99
|
|
|
} |
|
100
|
|
|
$template = Template::getInstance()->setStatus($e->getCode()); |
|
101
|
|
|
if (preg_match('/json/i', Request::getInstance()->getServer('CONTENT_TYPE')) || $isJson) { |
|
102
|
|
|
$response = new JsonResponse(null, false, 0, 0, $e->getMessage()); |
|
|
|
|
|
|
103
|
|
|
return $template->output(json_encode($response), 'application/json'); |
|
104
|
|
|
} else { |
|
105
|
|
|
$not_found_route = Config::getParam('route.404'); |
|
106
|
|
|
if(null !== $not_found_route) { |
|
107
|
|
|
Request::getInstance()->redirect($this->getRoute($not_found_route, 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
|
|
|
//Search action and execute |
|
162
|
1 |
|
$this->searchAction($route); |
|
163
|
1 |
|
} catch (AccessDeniedException $e) { |
|
164
|
|
|
Logger::log(_('Solicitamos credenciales de acceso a zona restringida')); |
|
165
|
|
|
return Admin::staticAdminLogon($route); |
|
166
|
1 |
|
} catch (RouterException $r) { |
|
167
|
1 |
|
Logger::log($r->getMessage(), LOG_WARNING); |
|
168
|
|
|
} catch (\Exception $e) { |
|
169
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
|
170
|
|
|
throw $e; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
1 |
|
throw new RouterException(_("Página no encontrada"), 404); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Método que busca el componente que ejecuta la ruta |
|
178
|
|
|
* |
|
179
|
|
|
* @param string $route |
|
180
|
|
|
* |
|
181
|
|
|
* @throws \PSFS\base\exception\RouterException |
|
182
|
|
|
*/ |
|
183
|
1 |
|
protected function searchAction($route) |
|
184
|
|
|
{ |
|
185
|
1 |
|
Logger::log('Searching action to execute: ' . $route, LOG_INFO); |
|
186
|
|
|
//Revisamos si tenemos la ruta registrada |
|
187
|
1 |
|
$parts = parse_url($route); |
|
188
|
1 |
|
$path = (array_key_exists('path', $parts)) ? $parts['path'] : $route; |
|
189
|
1 |
|
$httpRequest = Request::getInstance()->getMethod(); |
|
190
|
1 |
|
foreach ($this->routing as $pattern => $action) { |
|
191
|
1 |
|
list($httpMethod, $routePattern) = RouterHelper::extractHttpRoute($pattern); |
|
192
|
1 |
|
$matched = RouterHelper::matchRoutePattern($routePattern, $path); |
|
193
|
1 |
|
if ($matched && ($httpMethod === "ALL" || $httpRequest === $httpMethod) && RouterHelper::compareSlashes($routePattern, $path)) { |
|
|
|
|
|
|
194
|
|
|
// Checks restricted access |
|
195
|
|
|
SecurityHelper::checkRestrictedAccess($route); |
|
196
|
|
|
$get = RouterHelper::extractComponents($route, $routePattern); |
|
197
|
|
|
/** @var $class \PSFS\base\types\Controller */ |
|
198
|
|
|
$class = RouterHelper::getClassToCall($action); |
|
199
|
|
|
try { |
|
200
|
|
|
if($this->checkRequirements($action, $get)) { |
|
201
|
|
|
$this->executeCachedRoute($route, $action, $class, $get); |
|
202
|
|
|
} else { |
|
203
|
|
|
throw new RouterException(_('La ruta no es válida'), 400); |
|
204
|
|
|
} |
|
205
|
|
|
} catch (\Exception $e) { |
|
206
|
|
|
Logger::log($e->getMessage(), LOG_ERR); |
|
207
|
|
|
throw new \RuntimeException($e->getMessage(), 404, $e); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
1 |
|
throw new RouterException(_("Ruta no encontrada")); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* @param array $action |
|
216
|
|
|
* @param array $params |
|
217
|
|
|
* @return bool |
|
218
|
|
|
*/ |
|
219
|
|
|
private function checkRequirements(array $action, array $params = []) { |
|
220
|
|
|
if(!empty($action['requirements']) && !empty($params)) { |
|
221
|
|
|
$checked = 0; |
|
222
|
|
|
foreach(array_keys($params) as $key) { |
|
223
|
|
|
if(in_array($key, $action['requirements'])) { |
|
224
|
|
|
$checked++; |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
$valid = count($action['requirements']) == $checked; |
|
228
|
|
|
} else { |
|
229
|
|
|
$valid = true; |
|
230
|
|
|
} |
|
231
|
|
|
return $valid; |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Método que manda las cabeceras de autenticación |
|
236
|
|
|
* @return string HTML |
|
237
|
|
|
*/ |
|
238
|
|
|
protected function sentAuthHeader() |
|
239
|
|
|
{ |
|
240
|
|
|
return AdminServices::getInstance()->setAdminHeaders(); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return string|null |
|
245
|
|
|
*/ |
|
246
|
1 |
|
private function getExternalModules() { |
|
247
|
1 |
|
$externalModules = Config::getParam('modules.extend', ''); |
|
248
|
1 |
|
if(Config::getParam('psfs.auth', false)) { |
|
249
|
|
|
$externalModules .= ',psfs/auth'; |
|
250
|
|
|
} |
|
251
|
1 |
|
return $externalModules; |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Method that check if the project has sub project to include |
|
256
|
|
|
* @param boolean $hydrateRoute |
|
257
|
|
|
*/ |
|
258
|
1 |
|
private function checkExternalModules($hydrateRoute = true) |
|
259
|
|
|
{ |
|
260
|
1 |
|
$externalModules = $this->getExternalModules(); |
|
261
|
1 |
|
if (strlen($externalModules)) { |
|
262
|
|
|
$externalModules = explode(',', $externalModules); |
|
263
|
|
|
foreach ($externalModules as &$module) { |
|
264
|
|
|
$module = $this->loadExternalModule($hydrateRoute, $module); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
1 |
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Method that gather all the routes in the project |
|
271
|
|
|
*/ |
|
272
|
1 |
|
private function generateRouting() |
|
273
|
|
|
{ |
|
274
|
1 |
|
$base = SOURCE_DIR; |
|
275
|
1 |
|
$modulesPath = realpath(CORE_DIR); |
|
276
|
1 |
|
$this->routing = $this->inspectDir($base, "PSFS", array()); |
|
277
|
1 |
|
$this->checkExternalModules(); |
|
278
|
1 |
|
if (file_exists($modulesPath)) { |
|
279
|
|
|
$modules = $this->finder->directories()->in($modulesPath)->depth(0); |
|
280
|
|
|
foreach ($modules as $modulePath) { |
|
281
|
|
|
$module = $modulePath->getBasename(); |
|
282
|
|
|
$this->routing = $this->inspectDir($modulesPath . DIRECTORY_SEPARATOR . $module, $module, $this->routing); |
|
|
|
|
|
|
283
|
|
|
} |
|
284
|
|
|
} |
|
285
|
1 |
|
$this->cache->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->domains, Cache::JSON, TRUE); |
|
286
|
1 |
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Método que regenera el fichero de rutas |
|
290
|
|
|
* @throws ConfigException |
|
291
|
|
|
*/ |
|
292
|
1 |
|
public function hydrateRouting() |
|
293
|
|
|
{ |
|
294
|
1 |
|
$this->generateRouting(); |
|
295
|
1 |
|
$home = Config::getInstance()->get('home.action'); |
|
296
|
1 |
|
if (NULL !== $home || $home !== '') { |
|
297
|
1 |
|
$home_params = NULL; |
|
298
|
1 |
|
foreach ($this->routing as $pattern => $params) { |
|
299
|
1 |
|
list($method, $route) = RouterHelper::extractHttpRoute($pattern); |
|
300
|
1 |
|
if (preg_match("/" . preg_quote($route, "/") . "$/i", "/" . $home)) { |
|
301
|
|
|
$home_params = $params; |
|
302
|
|
|
} |
|
303
|
|
|
} |
|
304
|
1 |
|
if (NULL !== $home_params) { |
|
305
|
|
|
$this->routing['/'] = $home_params; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
1 |
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* Método que inspecciona los directorios en busca de clases que registren rutas |
|
312
|
|
|
* |
|
313
|
|
|
* @param string $origen |
|
314
|
|
|
* @param string $namespace |
|
315
|
|
|
* @param array $routing |
|
316
|
|
|
* |
|
317
|
|
|
* @return array |
|
318
|
|
|
* @throws ConfigException |
|
319
|
|
|
*/ |
|
320
|
1 |
|
private function inspectDir($origen, $namespace = 'PSFS', $routing = []) |
|
321
|
|
|
{ |
|
322
|
1 |
|
$files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name("*.php"); |
|
323
|
1 |
|
foreach ($files as $file) { |
|
324
|
1 |
|
$filename = str_replace("/", '\\', str_replace($origen, '', $file->getPathname())); |
|
325
|
1 |
|
$routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace); |
|
326
|
|
|
} |
|
327
|
1 |
|
$this->finder = new Finder(); |
|
328
|
|
|
|
|
329
|
1 |
|
return $routing; |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* Checks that a namespace exists |
|
334
|
|
|
* @param string $namespace |
|
335
|
|
|
* @return bool |
|
336
|
|
|
*/ |
|
337
|
2 |
|
public static function exists($namespace) |
|
338
|
|
|
{ |
|
339
|
2 |
|
return (class_exists($namespace) || interface_exists($namespace) || trait_exists($namespace)); |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Método que añade nuevas rutas al array de referencia |
|
344
|
|
|
* |
|
345
|
|
|
* @param string $namespace |
|
346
|
|
|
* @param array $routing |
|
347
|
|
|
* @param string $module |
|
348
|
|
|
* |
|
349
|
|
|
* @return array |
|
350
|
|
|
* @throws ConfigException |
|
351
|
|
|
*/ |
|
352
|
1 |
|
private function addRouting($namespace, &$routing, $module = 'PSFS') |
|
353
|
|
|
{ |
|
354
|
1 |
|
if (self::exists($namespace)) { |
|
355
|
1 |
|
if(I18nHelper::checkI18Class($namespace)) { |
|
356
|
|
|
return $routing; |
|
357
|
|
|
} |
|
358
|
1 |
|
$reflection = new \ReflectionClass($namespace); |
|
359
|
1 |
|
if (FALSE === $reflection->isAbstract() && FALSE === $reflection->isInterface()) { |
|
360
|
1 |
|
$this->extractDomain($reflection); |
|
361
|
1 |
|
$classComments = $reflection->getDocComment(); |
|
362
|
1 |
|
preg_match('/@api\ (.*)\n/im', $classComments, $apiPath); |
|
363
|
1 |
|
$api = ''; |
|
364
|
1 |
|
if (count($apiPath)) { |
|
365
|
|
|
$api = array_key_exists(1, $apiPath) ? $apiPath[1] : $api; |
|
366
|
|
|
} |
|
367
|
1 |
|
foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { |
|
368
|
1 |
|
if (preg_match('/@route\ /i', $method->getDocComment())) { |
|
369
|
1 |
|
list($route, $info) = RouterHelper::extractRouteInfo($method, str_replace('\\', '', $api), str_replace('\\', '', $module)); |
|
|
|
|
|
|
370
|
|
|
|
|
371
|
1 |
|
if (null !== $route && null !== $info) { |
|
372
|
1 |
|
$info['class'] = $namespace; |
|
373
|
1 |
|
$routing[$route] = $info; |
|
374
|
|
|
} |
|
375
|
|
|
} |
|
376
|
|
|
} |
|
377
|
|
|
} |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
1 |
|
return $routing; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
/** |
|
384
|
|
|
* Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates |
|
385
|
|
|
* |
|
386
|
|
|
* @param \ReflectionClass $class |
|
387
|
|
|
* |
|
388
|
|
|
* @return Router |
|
389
|
|
|
* @throws ConfigException |
|
390
|
|
|
*/ |
|
391
|
1 |
|
protected function extractDomain(\ReflectionClass $class) |
|
392
|
|
|
{ |
|
393
|
|
|
//Calculamos los dominios para las plantillas |
|
394
|
1 |
|
if ($class->hasConstant("DOMAIN") && !$class->isAbstract()) { |
|
395
|
1 |
|
if (!$this->domains) { |
|
|
|
|
|
|
396
|
1 |
|
$this->domains = []; |
|
397
|
|
|
} |
|
398
|
1 |
|
$domain = "@" . $class->getConstant("DOMAIN") . "/"; |
|
399
|
1 |
|
if (!array_key_exists($domain, $this->domains)) { |
|
400
|
1 |
|
$this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain); |
|
401
|
|
|
} |
|
402
|
|
|
} |
|
403
|
|
|
|
|
404
|
1 |
|
return $this; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* Método que genera las urls amigables para usar dentro del framework |
|
409
|
|
|
* @return Router |
|
410
|
|
|
*/ |
|
411
|
1 |
|
public function simpatize() |
|
412
|
|
|
{ |
|
413
|
1 |
|
$translationFileName = "translations" . DIRECTORY_SEPARATOR . "routes_translations.php"; |
|
414
|
1 |
|
$absoluteTranslationFileName = CACHE_DIR . DIRECTORY_SEPARATOR . $translationFileName; |
|
415
|
1 |
|
$this->generateSlugs($absoluteTranslationFileName); |
|
416
|
1 |
|
GeneratorHelper::createDir(CONFIG_DIR); |
|
417
|
1 |
|
Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", array($this->routing, $this->slugs), Cache::JSON, TRUE); |
|
|
|
|
|
|
418
|
|
|
|
|
419
|
1 |
|
return $this; |
|
420
|
|
|
} |
|
421
|
|
|
|
|
422
|
|
|
/** |
|
423
|
|
|
* Método que devuelve una ruta del framework |
|
424
|
|
|
* |
|
425
|
|
|
* @param string $slug |
|
426
|
|
|
* @param boolean $absolute |
|
427
|
|
|
* @param array $params |
|
428
|
|
|
* |
|
429
|
|
|
* @return string|null |
|
430
|
|
|
* @throws RouterException |
|
431
|
|
|
*/ |
|
432
|
3 |
|
public function getRoute($slug = '', $absolute = FALSE, $params = []) |
|
433
|
|
|
{ |
|
434
|
3 |
|
if (strlen($slug) === 0) { |
|
435
|
1 |
|
return ($absolute) ? Request::getInstance()->getRootUrl() . '/' : '/'; |
|
436
|
|
|
} |
|
437
|
3 |
|
if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) { |
|
438
|
1 |
|
throw new RouterException(_("No existe la ruta especificada")); |
|
439
|
|
|
} |
|
440
|
3 |
|
$url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug]; |
|
441
|
3 |
|
if (!empty($params)) foreach ($params as $key => $value) { |
|
442
|
|
|
$url = str_replace("{" . $key . "}", $value, $url); |
|
443
|
3 |
|
} elseif (!empty($this->routing[$this->slugs[$slug]]["default"])) { |
|
444
|
3 |
|
$url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"]; |
|
|
|
|
|
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
3 |
|
return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url); |
|
448
|
|
|
} |
|
449
|
|
|
|
|
450
|
|
|
/** |
|
451
|
|
|
* Método que devuelve las rutas de administración |
|
452
|
|
|
* @deprecated |
|
453
|
|
|
* @return array |
|
454
|
|
|
*/ |
|
455
|
|
|
public function getAdminRoutes() |
|
456
|
|
|
{ |
|
457
|
|
|
return AdminHelper::getAdminRoutes($this->routing); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* Método que devuelve le controlador del admin |
|
462
|
|
|
* @deprecated |
|
463
|
|
|
* @return Admin |
|
464
|
|
|
*/ |
|
465
|
|
|
public function getAdmin() |
|
466
|
|
|
{ |
|
467
|
|
|
return Admin::getInstance(); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* Método que extrae los dominios |
|
472
|
|
|
* @return array |
|
473
|
|
|
*/ |
|
474
|
1 |
|
public function getDomains() |
|
475
|
|
|
{ |
|
476
|
1 |
|
return $this->domains ?: []; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
/** |
|
480
|
|
|
* Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no |
|
481
|
|
|
* |
|
482
|
|
|
* @param string $route |
|
483
|
|
|
* @param array|null $action |
|
484
|
|
|
* @param types\Controller $class |
|
485
|
|
|
* @param array $params |
|
|
|
|
|
|
486
|
|
|
*/ |
|
487
|
|
|
protected function executeCachedRoute($route, $action, $class, $params = NULL) |
|
488
|
|
|
{ |
|
489
|
|
|
Logger::log('Executing route ' . $route, LOG_INFO); |
|
490
|
|
|
$action['params'] = array_merge($action['params'], $params, Request::getInstance()->getQueryParams()); |
|
491
|
|
|
Security::getInstance()->setSessionKey("__CACHE__", $action); |
|
492
|
|
|
$cache = Cache::needCache(); |
|
493
|
|
|
$execute = TRUE; |
|
494
|
|
|
if (FALSE !== $cache && Config::getParam('debug') === FALSE && $action['http'] === 'GET') { |
|
495
|
|
|
list($path, $cacheDataName) = $this->cache->getRequestCacheHash(); |
|
496
|
|
|
$cachedData = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $path . $cacheDataName, |
|
497
|
|
|
$cache, null); |
|
|
|
|
|
|
498
|
|
|
if (NULL !== $cachedData) { |
|
499
|
|
|
$headers = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $path . $cacheDataName . ".headers", |
|
|
|
|
|
|
500
|
|
|
$cache, null, Cache::JSON); |
|
|
|
|
|
|
501
|
|
|
Template::getInstance()->renderCache($cachedData, $headers); |
|
502
|
|
|
$execute = FALSE; |
|
503
|
|
|
} |
|
504
|
|
|
} |
|
505
|
|
|
if ($execute) { |
|
506
|
|
|
Logger::log(_('Start executing action'), LOG_DEBUG); |
|
507
|
|
|
if (false === call_user_func_array(array($class, $action['method']), $params)) { |
|
508
|
|
|
Logger::log(_('An error ocurred trying to execute the action'), LOG_ERR, [error_get_last()]); |
|
509
|
|
|
} |
|
510
|
|
|
} |
|
511
|
|
|
} |
|
512
|
|
|
|
|
513
|
|
|
/** |
|
514
|
|
|
* Parse slugs to create translations |
|
515
|
|
|
* |
|
516
|
|
|
* @param string $absoluteTranslationFileName |
|
517
|
|
|
*/ |
|
518
|
1 |
|
private function generateSlugs($absoluteTranslationFileName) |
|
519
|
|
|
{ |
|
520
|
1 |
|
$translations = I18nHelper::generateTranslationsFile($absoluteTranslationFileName); |
|
521
|
1 |
|
foreach ($this->routing as $key => &$info) { |
|
522
|
1 |
|
$keyParts = explode("#|#", $key); |
|
523
|
1 |
|
$keyParts = array_key_exists(1, $keyParts) ? $keyParts[1] : $keyParts[0]; |
|
524
|
1 |
|
$slug = RouterHelper::slugify($keyParts); |
|
525
|
1 |
|
if (NULL !== $slug && !array_key_exists($slug, $translations)) { |
|
526
|
1 |
|
$translations[$slug] = $info['label']; |
|
527
|
1 |
|
file_put_contents($absoluteTranslationFileName, "\$translations[\"{$slug}\"] = _(\"{$slug}\");\n", FILE_APPEND); |
|
|
|
|
|
|
528
|
|
|
} |
|
529
|
1 |
|
$this->slugs[$slug] = $key; |
|
530
|
1 |
|
$info["slug"] = $slug; |
|
531
|
|
|
} |
|
532
|
1 |
|
} |
|
533
|
|
|
|
|
534
|
|
|
/** |
|
535
|
|
|
* @param bool $hydrateRoute |
|
536
|
|
|
* @param $modulePath |
|
537
|
|
|
* @param $externalModulePath |
|
538
|
|
|
*/ |
|
539
|
|
|
private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath) |
|
540
|
|
|
{ |
|
541
|
|
|
$extModule = $modulePath->getBasename(); |
|
542
|
|
|
$moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php'); |
|
|
|
|
|
|
543
|
|
|
@include $moduleAutoloader; |
|
544
|
|
|
if ($hydrateRoute) { |
|
545
|
|
|
$this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, '\\' . $extModule, $this->routing); |
|
|
|
|
|
|
546
|
|
|
} |
|
547
|
|
|
} |
|
548
|
|
|
|
|
549
|
|
|
/** |
|
550
|
|
|
* @param $hydrateRoute |
|
551
|
|
|
* @param $module |
|
552
|
|
|
* @return mixed |
|
553
|
|
|
*/ |
|
554
|
|
|
private function loadExternalModule($hydrateRoute, $module) |
|
555
|
|
|
{ |
|
556
|
|
|
try { |
|
557
|
|
|
$module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module); |
|
558
|
|
|
$externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src'; |
|
559
|
|
|
$externalModule = $this->finder->directories()->in($externalModulePath)->depth(0); |
|
560
|
|
|
foreach ($externalModule as $modulePath) { |
|
561
|
|
|
$this->loadExternalAutoloader($hydrateRoute, $modulePath, $externalModulePath); |
|
562
|
|
|
} |
|
563
|
|
|
} catch (\Exception $e) { |
|
564
|
|
|
Logger::log($e->getMessage(), LOG_WARNING); |
|
565
|
|
|
$module = null; |
|
566
|
|
|
} |
|
567
|
|
|
return $module; |
|
568
|
|
|
} |
|
569
|
|
|
|
|
570
|
|
|
} |
|
571
|
|
|
|
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.