Passed
Push — master ( fdc4d2...a04f2e )
by Fran
03:08
created
src/base/Router.php 1 patch
Spacing   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -75,11 +75,11 @@  discard block
 block discarded – undo
75 75
      */
76 76
     public function init()
77 77
     {
78
-        list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'urls.json', $this->cacheType, TRUE);
78
+        list($this->routing, $this->slugs) = $this->cache->getDataFromFile(CONFIG_DIR.DIRECTORY_SEPARATOR.'urls.json', $this->cacheType, TRUE);
79 79
         if (empty($this->routing) || Config::getInstance()->getDebugMode()) {
80 80
             $this->debugLoad();
81
-        } else {
82
-            $this->domains = $this->cache->getDataFromFile(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', $this->cacheType, TRUE);
81
+        }else {
82
+            $this->domains = $this->cache->getDataFromFile(CONFIG_DIR.DIRECTORY_SEPARATOR.'domains.json', $this->cacheType, TRUE);
83 83
         }
84 84
         $this->checkExternalModules(false);
85 85
         $this->setLoaded();
@@ -117,9 +117,9 @@  discard block
 block discarded – undo
117 117
         }
118 118
 
119 119
         $not_found_route = Config::getParam('route.404');
120
-        if(null !== $not_found_route) {
120
+        if (null !== $not_found_route) {
121 121
             Request::getInstance()->redirect($this->getRoute($not_found_route, true));
122
-        } else {
122
+        }else {
123 123
             return $template->render('error.html.twig', array(
124 124
                 'exception' => $e,
125 125
                 'trace' => $e->getTraceAsString(),
@@ -170,12 +170,12 @@  discard block
 block discarded – undo
170 170
         try {
171 171
             //Search action and execute
172 172
             $this->searchAction($route);
173
-        } catch (AccessDeniedException $e) {
174
-            Logger::log(_('Solicitamos credenciales de acceso a zona restringida'), LOG_WARNING, ['file' => $e->getFile() . '[' . $e->getLine() . ']']);
173
+        }catch (AccessDeniedException $e) {
174
+            Logger::log(_('Solicitamos credenciales de acceso a zona restringida'), LOG_WARNING, ['file' => $e->getFile().'['.$e->getLine().']']);
175 175
             return Admin::staticAdminLogon($route);
176
-        } catch (RouterException $r) {
176
+        }catch (RouterException $r) {
177 177
             Logger::log($r->getMessage(), LOG_WARNING);
178
-        } catch (\Exception $e) {
178
+        }catch (\Exception $e) {
179 179
             Logger::log($e->getMessage(), LOG_ERR);
180 180
             throw $e;
181 181
         }
@@ -192,7 +192,7 @@  discard block
 block discarded – undo
192 192
      */
193 193
     protected function searchAction($route)
194 194
     {
195
-        Logger::log('Searching action to execute: ' . $route, LOG_INFO);
195
+        Logger::log('Searching action to execute: '.$route, LOG_INFO);
196 196
         //Revisamos si tenemos la ruta registrada
197 197
         $parts = parse_url($route);
198 198
         $path = array_key_exists('path', $parts) ? $parts['path'] : $route;
@@ -207,12 +207,12 @@  discard block
 block discarded – undo
207 207
                 /** @var $class \PSFS\base\types\Controller */
208 208
                 $class = RouterHelper::getClassToCall($action);
209 209
                 try {
210
-                    if($this->checkRequirements($action, $get)) {
210
+                    if ($this->checkRequirements($action, $get)) {
211 211
                         $this->executeCachedRoute($route, $action, $class, $get);
212
-                    } else {
212
+                    }else {
213 213
                         throw new RouterException(_('La ruta no es válida'), 400);
214 214
                     }
215
-                } catch (\Exception $e) {
215
+                }catch (\Exception $e) {
216 216
                     Logger::log($e->getMessage(), LOG_ERR);
217 217
                     throw $e;
218 218
                 }
@@ -227,15 +227,15 @@  discard block
 block discarded – undo
227 227
      * @return bool
228 228
      */
229 229
     private function checkRequirements(array $action, $params = []) {
230
-        if(!empty($params) && !empty($action['requirements'])) {
230
+        if (!empty($params) && !empty($action['requirements'])) {
231 231
             $checked = 0;
232
-            foreach(array_keys($params) as $key) {
233
-                if(in_array($key, $action['requirements'], true)) {
232
+            foreach (array_keys($params) as $key) {
233
+                if (in_array($key, $action['requirements'], true)) {
234 234
                     $checked++;
235 235
                 }
236 236
             }
237 237
             $valid = count($action['requirements']) === $checked;
238
-        } else {
238
+        }else {
239 239
             $valid = true;
240 240
         }
241 241
         return $valid;
@@ -285,14 +285,14 @@  discard block
 block discarded – undo
285 285
         $this->checkExternalModules();
286 286
         if (file_exists($modulesPath)) {
287 287
             $modules = $this->finder->directories()->in($modulesPath)->depth(0);
288
-            if($modules->hasResults()) {
288
+            if ($modules->hasResults()) {
289 289
                 foreach ($modules->getIterator() as $modulePath) {
290 290
                     $module = $modulePath->getBasename();
291
-                    $this->routing = $this->inspectDir($modulesPath . DIRECTORY_SEPARATOR . $module, $module, $this->routing);
291
+                    $this->routing = $this->inspectDir($modulesPath.DIRECTORY_SEPARATOR.$module, $module, $this->routing);
292 292
                 }
293 293
             }
294 294
         }
295
-        $this->cache->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'domains.json', $this->domains, Cache::JSON, TRUE);
295
+        $this->cache->storeData(CONFIG_DIR.DIRECTORY_SEPARATOR.'domains.json', $this->domains, Cache::JSON, TRUE);
296 296
     }
297 297
 
298 298
     /**
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
             $home_params = NULL;
309 309
             foreach ($this->routing as $pattern => $params) {
310 310
                 list($method, $route) = RouterHelper::extractHttpRoute($pattern);
311
-                if (preg_match('/' . preg_quote($route, '/') . '$/i', '/' . $home)) {
311
+                if (preg_match('/'.preg_quote($route, '/').'$/i', '/'.$home)) {
312 312
                     $home_params = $params;
313 313
                 }
314 314
             }
@@ -329,10 +329,10 @@  discard block
 block discarded – undo
329 329
     private function inspectDir($origen, $namespace = 'PSFS', $routing = [])
330 330
     {
331 331
         $files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name('*.php');
332
-        if($files->hasResults()) {
332
+        if ($files->hasResults()) {
333 333
             foreach ($files->getIterator() as $file) {
334 334
                 $filename = str_replace('/', '\\', str_replace($origen, '', $file->getPathname()));
335
-                $routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace);
335
+                $routing = $this->addRouting($namespace.str_replace('.php', '', $filename), $routing, $namespace);
336 336
             }
337 337
         }
338 338
         $this->finder = new Finder();
@@ -361,7 +361,7 @@  discard block
 block discarded – undo
361 361
     private function addRouting($namespace, &$routing, $module = 'PSFS')
362 362
     {
363 363
         if (self::exists($namespace)) {
364
-            if(I18nHelper::checkI18Class($namespace)) {
364
+            if (I18nHelper::checkI18Class($namespace)) {
365 365
                 return $routing;
366 366
             }
367 367
             $reflection = new \ReflectionClass($namespace);
@@ -403,7 +403,7 @@  discard block
 block discarded – undo
403 403
             if (!$this->domains) {
404 404
                 $this->domains = [];
405 405
             }
406
-            $domain = '@' . $class->getConstant('DOMAIN') . '/';
406
+            $domain = '@'.$class->getConstant('DOMAIN').'/';
407 407
             if (!array_key_exists($domain, $this->domains)) {
408 408
                 $this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain);
409 409
             }
@@ -419,11 +419,11 @@  discard block
 block discarded – undo
419 419
      */
420 420
     public function simpatize()
421 421
     {
422
-        $translationFileName = 'translations' . DIRECTORY_SEPARATOR . 'routes_translations.php';
423
-        $absoluteTranslationFileName = CACHE_DIR . DIRECTORY_SEPARATOR . $translationFileName;
422
+        $translationFileName = 'translations'.DIRECTORY_SEPARATOR.'routes_translations.php';
423
+        $absoluteTranslationFileName = CACHE_DIR.DIRECTORY_SEPARATOR.$translationFileName;
424 424
         $this->generateSlugs($absoluteTranslationFileName);
425 425
         GeneratorHelper::createDir(CONFIG_DIR);
426
-        Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . 'urls.json', array($this->routing, $this->slugs), Cache::JSON, TRUE);
426
+        Cache::getInstance()->storeData(CONFIG_DIR.DIRECTORY_SEPARATOR.'urls.json', array($this->routing, $this->slugs), Cache::JSON, TRUE);
427 427
 
428 428
         return $this;
429 429
     }
@@ -439,18 +439,18 @@  discard block
 block discarded – undo
439 439
     public function getRoute($slug = '', $absolute = FALSE, array $params = [])
440 440
     {
441 441
         if ('' === $slug) {
442
-            return $absolute ? Request::getInstance()->getRootUrl() . '/' : '/';
442
+            return $absolute ? Request::getInstance()->getRootUrl().'/' : '/';
443 443
         }
444 444
         if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) {
445 445
             throw new RouterException(_('No existe la ruta especificada'));
446 446
         }
447
-        $url = $absolute ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug];
447
+        $url = $absolute ? Request::getInstance()->getRootUrl().$this->slugs[$slug] : $this->slugs[$slug];
448 448
         if (!empty($params)) {
449 449
             foreach ($params as $key => $value) {
450
-                $url = str_replace('{' . $key . '}', $value, $url);
450
+                $url = str_replace('{'.$key.'}', $value, $url);
451 451
             }
452 452
         } elseif (!empty($this->routing[$this->slugs[$slug]]['default'])) {
453
-            $url = $absolute ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]['default'] : $this->routing[$this->slugs[$slug]]['default'];
453
+            $url = $absolute ? Request::getInstance()->getRootUrl().$this->routing[$this->slugs[$slug]]['default'] : $this->routing[$this->slugs[$slug]]['default'];
454 454
         }
455 455
 
456 456
         return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url);
@@ -492,17 +492,17 @@  discard block
 block discarded – undo
492 492
      */
493 493
     protected function executeCachedRoute($route, $action, $class, $params = NULL)
494 494
     {
495
-        Logger::log('Executing route ' . $route, LOG_INFO);
495
+        Logger::log('Executing route '.$route, LOG_INFO);
496 496
         $action['params'] = array_merge($action['params'], $params, Request::getInstance()->getQueryParams());
497 497
         Security::getInstance()->setSessionKey(Cache::CACHE_SESSION_VAR, $action);
498 498
         $cache = Cache::needCache();
499 499
         $execute = TRUE;
500 500
         if (FALSE !== $cache && $action['http'] === 'GET' && Config::getParam('debug') === FALSE) {
501 501
             list($path, $cacheDataName) = $this->cache->getRequestCacheHash();
502
-            $cachedData = $this->cache->readFromCache('json' . DIRECTORY_SEPARATOR . $path . $cacheDataName,
502
+            $cachedData = $this->cache->readFromCache('json'.DIRECTORY_SEPARATOR.$path.$cacheDataName,
503 503
                 $cache);
504 504
             if (NULL !== $cachedData) {
505
-                $headers = $this->cache->readFromCache('json' . DIRECTORY_SEPARATOR . $path . $cacheDataName . '.headers',
505
+                $headers = $this->cache->readFromCache('json'.DIRECTORY_SEPARATOR.$path.$cacheDataName.'.headers',
506 506
                     $cache, null, Cache::JSON);
507 507
                 Template::getInstance()->renderCache($cachedData, $headers);
508 508
                 $execute = FALSE;
@@ -545,11 +545,11 @@  discard block
 block discarded – undo
545 545
     private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath)
546 546
     {
547 547
         $extModule = $modulePath->getBasename();
548
-        $moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php');
549
-        if(file_exists($moduleAutoloader)) {
548
+        $moduleAutoloader = realpath($externalModulePath.DIRECTORY_SEPARATOR.$extModule.DIRECTORY_SEPARATOR.'autoload.php');
549
+        if (file_exists($moduleAutoloader)) {
550 550
             include_once $moduleAutoloader;
551 551
             if ($hydrateRoute) {
552
-                $this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, '\\' . $extModule, $this->routing);
552
+                $this->routing = $this->inspectDir($externalModulePath.DIRECTORY_SEPARATOR.$extModule, '\\'.$extModule, $this->routing);
553 553
             }
554 554
         }
555 555
     }
@@ -563,14 +563,14 @@  discard block
 block discarded – undo
563 563
     {
564 564
         try {
565 565
             $module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module);
566
-            $externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src';
566
+            $externalModulePath = VENDOR_DIR.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.'src';
567 567
             $externalModule = $this->finder->directories()->in($externalModulePath)->depth(0);
568
-            if($externalModule->hasResults()) {
568
+            if ($externalModule->hasResults()) {
569 569
                 foreach ($externalModule->getIterator() as $modulePath) {
570 570
                     $this->loadExternalAutoloader($hydrateRoute, $modulePath, $externalModulePath);
571 571
                 }
572 572
             }
573
-        } catch (\Exception $e) {
573
+        }catch (\Exception $e) {
574 574
             Logger::log($e->getMessage(), LOG_WARNING);
575 575
             $module = null;
576 576
         }
Please login to merge, or discard this patch.