Passed
Push — master ( 418e0a...e36210 )
by Fran
04:04
created
src/base/Router.php 1 patch
Spacing   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -59,12 +59,12 @@  discard block
 block discarded – undo
59 59
      */
60 60
     public function init()
61 61
     {
62
-        if (!file_exists(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json") || Config::getInstance()->getDebugMode()) {
62
+        if (!file_exists(CONFIG_DIR.DIRECTORY_SEPARATOR."urls.json") || Config::getInstance()->getDebugMode()) {
63 63
             $this->hydrateRouting();
64 64
             $this->simpatize();
65
-        } else {
66
-            list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", Cache::JSON, TRUE);
67
-            $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", Cache::JSON, TRUE);
65
+        }else {
66
+            list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR.DIRECTORY_SEPARATOR."urls.json", Cache::JSON, TRUE);
67
+            $this->domains = $this->cache->getDataFromFile(CONFIG_DIR.DIRECTORY_SEPARATOR."domains.json", Cache::JSON, TRUE);
68 68
         }
69 69
     }
70 70
 
@@ -88,7 +88,7 @@  discard block
 block discarded – undo
88 88
                 "success" => FALSE,
89 89
                 "error" => $e->getMessage(),
90 90
             )), 'application/json');
91
-        } else {
91
+        }else {
92 92
             return $template->render('error.html.twig', array(
93 93
                 'exception' => $e,
94 94
                 'trace' => $e->getTraceAsString(),
@@ -112,8 +112,8 @@  discard block
 block discarded – undo
112 112
      */
113 113
     public function getAllRoutes() {
114 114
         $routes = [];
115
-        foreach($this->routing as $path => $route) {
116
-            if(array_key_exists('slug', $route)) {
115
+        foreach ($this->routing as $path => $route) {
116
+            if (array_key_exists('slug', $route)) {
117 117
                 $routes[$route['slug']] = $path;
118 118
             }
119 119
         }
@@ -138,12 +138,12 @@  discard block
 block discarded – undo
138 138
             SecurityHelper::checkRestrictedAccess($route);
139 139
             //Search action and execute
140 140
             $this->searchAction($route);
141
-        } catch (AccessDeniedException $e) {
141
+        }catch (AccessDeniedException $e) {
142 142
             Logger::log(_('Solicitamos credenciales de acceso a zona restringida'));
143 143
             return Admin::staticAdminLogon($route);
144
-        } catch (RouterException $r) {
144
+        }catch (RouterException $r) {
145 145
             Logger::log($r->getMessage(), LOG_WARNING);
146
-        } catch (\Exception $e) {
146
+        }catch (\Exception $e) {
147 147
             Logger::log($e->getMessage(), LOG_ERR);
148 148
             throw $e;
149 149
         }
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
      */
161 161
     protected function searchAction($route)
162 162
     {
163
-        Logger::log('Searching action to execute: ' . $route, LOG_INFO);
163
+        Logger::log('Searching action to execute: '.$route, LOG_INFO);
164 164
         //Revisamos si tenemos la ruta registrada
165 165
         $parts = parse_url($route);
166 166
         $path = (array_key_exists('path', $parts)) ? $parts['path'] : $route;
@@ -174,7 +174,7 @@  discard block
 block discarded – undo
174 174
                 $class = RouterHelper::getClassToCall($action);
175 175
                 try {
176 176
                     $this->executeCachedRoute($route, $action, $class, $get);
177
-                } catch (\Exception $e) {
177
+                }catch (\Exception $e) {
178 178
                     Logger::log($e->getMessage(), LOG_ERR);
179 179
                     throw new RouterException($e->getMessage(), 404, $e);
180 180
                 }
@@ -194,20 +194,20 @@  discard block
 block discarded – undo
194 194
 
195 195
     private function checkExternalModules() {
196 196
         $externalModules = Config::getParam('modules.extend');
197
-        if(null !== $externalModules) {
197
+        if (null !== $externalModules) {
198 198
             $externalModules = explode(',', $externalModules);
199
-            foreach($externalModules as &$module) {
199
+            foreach ($externalModules as &$module) {
200 200
                 $module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module);
201
-                $externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src';
202
-                if(file_exists($externalModulePath)) {
201
+                $externalModulePath = VENDOR_DIR.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'src';
202
+                if (file_exists($externalModulePath)) {
203 203
                     $externalModule = $this->finder->directories()->in($externalModulePath)->depth(0);
204
-                    if(!empty($externalModule)) {
205
-                        foreach($externalModule as $modulePath) {
204
+                    if (!empty($externalModule)) {
205
+                        foreach ($externalModule as $modulePath) {
206 206
                             $extModule = $modulePath->getBasename();
207
-                            $moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php');
208
-                            if(file_exists($moduleAutoloader)) {
207
+                            $moduleAutoloader = realpath($externalModulePath.DIRECTORY_SEPARATOR.$extModule.DIRECTORY_SEPARATOR.'autoload.php');
208
+                            if (file_exists($moduleAutoloader)) {
209 209
                                 @include $moduleAutoloader;
210
-                                $this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, $extModule, $this->routing);
210
+                                $this->routing = $this->inspectDir($externalModulePath.DIRECTORY_SEPARATOR.$extModule, $extModule, $this->routing);
211 211
                             }
212 212
                         }
213 213
                     }
@@ -226,13 +226,13 @@  discard block
 block discarded – undo
226 226
         $this->routing = $this->inspectDir($base, "PSFS", array());
227 227
         if (file_exists($modulesPath)) {
228 228
             $modules = $this->finder->directories()->in($modulesPath)->depth(0);
229
-            foreach($modules as $modulePath) {
229
+            foreach ($modules as $modulePath) {
230 230
                 $module = $modulePath->getBasename();
231
-                $this->routing = $this->inspectDir($modulesPath . DIRECTORY_SEPARATOR . $module, $module, $this->routing);
231
+                $this->routing = $this->inspectDir($modulesPath.DIRECTORY_SEPARATOR.$module, $module, $this->routing);
232 232
             }
233 233
         }
234 234
         $this->checkExternalModules();
235
-        $this->cache->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->domains, Cache::JSON, TRUE);
235
+        $this->cache->storeData(CONFIG_DIR.DIRECTORY_SEPARATOR."domains.json", $this->domains, Cache::JSON, TRUE);
236 236
     }
237 237
 
238 238
     /**
@@ -247,7 +247,7 @@  discard block
 block discarded – undo
247 247
             $home_params = NULL;
248 248
             foreach ($this->routing as $pattern => $params) {
249 249
                 list($method, $route) = RouterHelper::extractHttpRoute($pattern);
250
-                if (preg_match("/" . preg_quote($route, "/") . "$/i", "/" . $home)) {
250
+                if (preg_match("/".preg_quote($route, "/")."$/i", "/".$home)) {
251 251
                     $home_params = $params;
252 252
                 }
253 253
             }
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
         $files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name("*.php");
273 273
         foreach ($files as $file) {
274 274
             $filename = str_replace("/", '\\', str_replace($origen, '', $file->getPathname()));
275
-            $routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace);
275
+            $routing = $this->addRouting($namespace.str_replace('.php', '', $filename), $routing, $namespace);
276 276
         }
277 277
         $this->finder = new Finder();
278 278
 
@@ -311,9 +311,9 @@  discard block
 block discarded – undo
311 311
                     $api = array_key_exists(1, $apiPath) ? $apiPath[1] : $api;
312 312
                 }
313 313
                 foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
314
-                    if(preg_match('/@route\ /i', $method->getDocComment())) {
314
+                    if (preg_match('/@route\ /i', $method->getDocComment())) {
315 315
                         list($route, $info) = RouterHelper::extractRouteInfo($method, $api, $module);
316
-                        if(null !== $route && null !== $info) {
316
+                        if (null !== $route && null !== $info) {
317 317
                             $info['class'] = $namespace;
318 318
                             $routing[$route] = $info;
319 319
                         }
@@ -337,11 +337,11 @@  discard block
 block discarded – undo
337 337
     {
338 338
         //Calculamos los dominios para las plantillas
339 339
         if ($class->hasConstant("DOMAIN") && !$class->isAbstract()) {
340
-            if(!$this->domains) {
340
+            if (!$this->domains) {
341 341
                 $this->domains = [];
342 342
             }
343
-            $domain = "@" . $class->getConstant("DOMAIN") . "/";
344
-            if(!array_key_exists($domain, $this->domains)) {
343
+            $domain = "@".$class->getConstant("DOMAIN")."/";
344
+            if (!array_key_exists($domain, $this->domains)) {
345 345
                 $this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain);
346 346
             }
347 347
         }
@@ -355,11 +355,11 @@  discard block
 block discarded – undo
355 355
      */
356 356
     public function simpatize()
357 357
     {
358
-        $translationFileName = "translations" . DIRECTORY_SEPARATOR . "routes_translations.php";
359
-        $absoluteTranslationFileName = CACHE_DIR . DIRECTORY_SEPARATOR . $translationFileName;
358
+        $translationFileName = "translations".DIRECTORY_SEPARATOR."routes_translations.php";
359
+        $absoluteTranslationFileName = CACHE_DIR.DIRECTORY_SEPARATOR.$translationFileName;
360 360
         $this->generateSlugs($absoluteTranslationFileName);
361 361
         Config::createDir(CONFIG_DIR);
362
-        Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", array($this->routing, $this->slugs), Cache::JSON, TRUE);
362
+        Cache::getInstance()->storeData(CONFIG_DIR.DIRECTORY_SEPARATOR."urls.json", array($this->routing, $this->slugs), Cache::JSON, TRUE);
363 363
 
364 364
         return $this;
365 365
     }
@@ -377,16 +377,16 @@  discard block
 block discarded – undo
377 377
     public function getRoute($slug = '', $absolute = FALSE, $params = [])
378 378
     {
379 379
         if (strlen($slug) === 0) {
380
-            return ($absolute) ? Request::getInstance()->getRootUrl() . '/' : '/';
380
+            return ($absolute) ? Request::getInstance()->getRootUrl().'/' : '/';
381 381
         }
382 382
         if (NULL === $slug || !array_key_exists($slug, $this->slugs)) {
383 383
             throw new RouterException(_("No existe la ruta especificada"));
384 384
         }
385
-        $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug];
385
+        $url = ($absolute) ? Request::getInstance()->getRootUrl().$this->slugs[$slug] : $this->slugs[$slug];
386 386
         if (!empty($params)) foreach ($params as $key => $value) {
387
-            $url = str_replace("{" . $key . "}", $value, $url);
387
+            $url = str_replace("{".$key."}", $value, $url);
388 388
         } elseif (!empty($this->routing[$this->slugs[$slug]]["default"])) {
389
-            $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"];
389
+            $url = ($absolute) ? Request::getInstance()->getRootUrl().$this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"];
390 390
         }
391 391
 
392 392
         return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url);
@@ -428,8 +428,8 @@  discard block
 block discarded – undo
428 428
     private function checkAction($class, $method, array $params) {
429 429
         $action = new \ReflectionMethod($class, $method);
430 430
 
431
-        foreach($action->getParameters() as $parameter) {
432
-            if(!$parameter->isOptional() && !array_key_exists($parameter->getName(), $params)) {
431
+        foreach ($action->getParameters() as $parameter) {
432
+            if (!$parameter->isOptional() && !array_key_exists($parameter->getName(), $params)) {
433 433
                 throw new RouterException('Required parameters not sent');
434 434
             }
435 435
         }
@@ -446,25 +446,25 @@  discard block
 block discarded – undo
446 446
      */
447 447
     protected function executeCachedRoute($route, $action, $class, $params = NULL)
448 448
     {
449
-        Logger::log('Executing route ' . $route, LOG_INFO);
449
+        Logger::log('Executing route '.$route, LOG_INFO);
450 450
         Security::getInstance()->setSessionKey("__CACHE__", $action);
451 451
         $cache = Cache::needCache();
452 452
         $execute = TRUE;
453 453
         if (FALSE !== $cache && Config::getInstance()->getDebugMode() === FALSE) {
454 454
             $cacheDataName = $this->cache->getRequestCacheHash();
455
-            $tmpDir = substr($cacheDataName, 0, 2) . DIRECTORY_SEPARATOR . substr($cacheDataName, 2, 2) . DIRECTORY_SEPARATOR;
456
-            $cachedData = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $tmpDir . $cacheDataName,
457
-                $cache, function () {});
455
+            $tmpDir = substr($cacheDataName, 0, 2).DIRECTORY_SEPARATOR.substr($cacheDataName, 2, 2).DIRECTORY_SEPARATOR;
456
+            $cachedData = $this->cache->readFromCache("json".DIRECTORY_SEPARATOR.$tmpDir.$cacheDataName,
457
+                $cache, function() {});
458 458
             if (NULL !== $cachedData) {
459
-                $headers = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $tmpDir . $cacheDataName . ".headers",
460
-                    $cache, function () {}, Cache::JSON);
459
+                $headers = $this->cache->readFromCache("json".DIRECTORY_SEPARATOR.$tmpDir.$cacheDataName.".headers",
460
+                    $cache, function() {}, Cache::JSON);
461 461
                 Template::getInstance()->renderCache($cachedData, $headers);
462 462
                 $execute = FALSE;
463 463
             }
464 464
         }
465 465
         if ($execute) {
466 466
             Logger::log(_('Start executing action'), LOG_DEBUG);
467
-            if(false === call_user_func_array(array($class, $action['method']), $params)) {
467
+            if (false === call_user_func_array(array($class, $action['method']), $params)) {
468 468
                 Logger::log(_('An error ocurred trying to execute the action'), LOG_ERR, [error_get_last()]);
469 469
             }
470 470
         }
Please login to merge, or discard this patch.