Passed
Push — master ( 08a67e...6063b2 )
by Fran
07:56
created

Router::addRouting()   C

Complexity

Conditions 11
Paths 6

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 11.1412

Importance

Changes 0
Metric Value
cc 11
eloc 19
nc 6
nop 3
dl 0
loc 30
ccs 17
cts 19
cp 0.8947
crap 11.1412
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 147 characters

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.

Loading history...
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);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 134 characters

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.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $e not be null|\Exception?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
89
     * @param boolean $isJson
90
     *
91
     * @return string HTML
92
     */
93
    public function httpNotFound(\Exception $e = NULL, $isJson = false)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $e. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
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());
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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)) {
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 140 characters

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.

Loading history...
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
        $externalModules .= ',psfs/auth';
249 1
        return $externalModules;
250
    }
251
252
    /**
253
     * Method that check if the project has sub project to include
254
     * @param boolean $hydrateRoute
255
     */
256 1
    private function checkExternalModules($hydrateRoute = true)
257
    {
258 1
        $externalModules = $this->getExternalModules();
259 1
        if (strlen($externalModules)) {
260 1
            $externalModules = explode(',', $externalModules);
261 1
            foreach ($externalModules as &$module) {
262 1
                $module = $this->loadExternalModule($hydrateRoute, $module);
263
            }
264
        }
265 1
    }
266
267
    /**
268
     * Method that gather all the routes in the project
269
     */
270 1
    private function generateRouting()
271
    {
272 1
        $base = SOURCE_DIR;
273 1
        $modulesPath = realpath(CORE_DIR);
274 1
        $this->routing = $this->inspectDir($base, "PSFS", array());
275 1
        $this->checkExternalModules();
276 1
        if (file_exists($modulesPath)) {
277
            $modules = $this->finder->directories()->in($modulesPath)->depth(0);
278
            foreach ($modules as $modulePath) {
279
                $module = $modulePath->getBasename();
280
                $this->routing = $this->inspectDir($modulesPath . DIRECTORY_SEPARATOR . $module, $module, $this->routing);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

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.

Loading history...
281
            }
282
        }
283 1
        $this->cache->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "domains.json", $this->domains, Cache::JSON, TRUE);
284 1
    }
285
286
    /**
287
     * Método que regenera el fichero de rutas
288
     * @throws ConfigException
289
     */
290 1
    public function hydrateRouting()
291
    {
292 1
        $this->generateRouting();
293 1
        $home = Config::getInstance()->get('home.action');
294 1
        if (NULL !== $home || $home !== '') {
295 1
            $home_params = NULL;
296 1
            foreach ($this->routing as $pattern => $params) {
297 1
                list($method, $route) = RouterHelper::extractHttpRoute($pattern);
298 1
                if (preg_match("/" . preg_quote($route, "/") . "$/i", "/" . $home)) {
299
                    $home_params = $params;
300
                }
301
            }
302 1
            if (NULL !== $home_params) {
303
                $this->routing['/'] = $home_params;
304
            }
305
        }
306 1
    }
307
308
    /**
309
     * Método que inspecciona los directorios en busca de clases que registren rutas
310
     *
311
     * @param string $origen
312
     * @param string $namespace
313
     * @param array $routing
314
     *
315
     * @return array
316
     * @throws ConfigException
317
     */
318 1
    private function inspectDir($origen, $namespace = 'PSFS', $routing = [])
319
    {
320 1
        $files = $this->finder->files()->in($origen)->path('/(controller|api)/i')->depth(1)->name("*.php");
321 1
        foreach ($files as $file) {
322 1
            $filename = str_replace("/", '\\', str_replace($origen, '', $file->getPathname()));
323 1
            $routing = $this->addRouting($namespace . str_replace('.php', '', $filename), $routing, $namespace);
324
        }
325 1
        $this->finder = new Finder();
326
327 1
        return $routing;
328
    }
329
330
    /**
331
     * Checks that a namespace exists
332
     * @param string $namespace
333
     * @return bool
334
     */
335 2
    public static function exists($namespace)
336
    {
337 2
        return (class_exists($namespace) || interface_exists($namespace) || trait_exists($namespace));
338
    }
339
340
    /**
341
     * Método que añade nuevas rutas al array de referencia
342
     *
343
     * @param string $namespace
344
     * @param array $routing
345
     * @param string $module
346
     *
347
     * @return array
348
     * @throws ConfigException
349
     */
350 1
    private function addRouting($namespace, &$routing, $module = 'PSFS')
351
    {
352 1
        if (self::exists($namespace)) {
353 1
            if(I18nHelper::checkI18Class($namespace)) {
354
                return $routing;
355
            }
356 1
            $reflection = new \ReflectionClass($namespace);
357 1
            if (FALSE === $reflection->isAbstract() && FALSE === $reflection->isInterface()) {
358 1
                $this->extractDomain($reflection);
359 1
                $classComments = $reflection->getDocComment();
360 1
                preg_match('/@api\ (.*)\n/im', $classComments, $apiPath);
361 1
                $api = '';
362 1
                if (count($apiPath)) {
363
                    $api = array_key_exists(1, $apiPath) ? $apiPath[1] : $api;
364
                }
365 1
                foreach ($reflection->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
366 1
                    if (preg_match('/@route\ /i', $method->getDocComment())) {
367 1
                        list($route, $info) = RouterHelper::extractRouteInfo($method, str_replace('\\', '', $api), str_replace('\\', '', $module));
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 147 characters

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.

Loading history...
368
369 1
                        if (null !== $route && null !== $info) {
370 1
                            $info['class'] = $namespace;
371 1
                            $routing[$route] = $info;
372
                        }
373
                    }
374
                }
375
            }
376
        }
377
378 1
        return $routing;
379
    }
380
381
    /**
382
     * Método que extrae de la ReflectionClass los datos necesarios para componer los dominios en los templates
383
     *
384
     * @param \ReflectionClass $class
385
     *
386
     * @return Router
387
     * @throws ConfigException
388
     */
389 1
    protected function extractDomain(\ReflectionClass $class)
390
    {
391
        //Calculamos los dominios para las plantillas
392 1
        if ($class->hasConstant("DOMAIN") && !$class->isAbstract()) {
393 1
            if (!$this->domains) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->domains of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
394 1
                $this->domains = [];
395
            }
396 1
            $domain = "@" . $class->getConstant("DOMAIN") . "/";
397 1
            if (!array_key_exists($domain, $this->domains)) {
398 1
                $this->domains[$domain] = RouterHelper::extractDomainInfo($class, $domain);
399
            }
400
        }
401
402 1
        return $this;
403
    }
404
405
    /**
406
     * Método que genera las urls amigables para usar dentro del framework
407
     * @return Router
408
     */
409 1
    public function simpatize()
410
    {
411 1
        $translationFileName = "translations" . DIRECTORY_SEPARATOR . "routes_translations.php";
412 1
        $absoluteTranslationFileName = CACHE_DIR . DIRECTORY_SEPARATOR . $translationFileName;
413 1
        $this->generateSlugs($absoluteTranslationFileName);
414 1
        GeneratorHelper::createDir(CONFIG_DIR);
415 1
        Cache::getInstance()->storeData(CONFIG_DIR . DIRECTORY_SEPARATOR . "urls.json", array($this->routing, $this->slugs), Cache::JSON, TRUE);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 characters

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.

Loading history...
416
417 1
        return $this;
418
    }
419
420
    /**
421
     * Método que devuelve una ruta del framework
422
     *
423
     * @param string $slug
424
     * @param boolean $absolute
425
     * @param array $params
426
     *
427
     * @return string|null
428
     * @throws RouterException
429
     */
430 3
    public function getRoute($slug = '', $absolute = FALSE, $params = [])
431
    {
432 3
        if (strlen($slug) === 0) {
433 1
            return ($absolute) ? Request::getInstance()->getRootUrl() . '/' : '/';
434
        }
435 3
        if (!is_array($this->slugs) || !array_key_exists($slug, $this->slugs)) {
436 1
            throw new RouterException(_("No existe la ruta especificada"));
437
        }
438 3
        $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->slugs[$slug] : $this->slugs[$slug];
439 3
        if (!empty($params)) foreach ($params as $key => $value) {
440
            $url = str_replace("{" . $key . "}", $value, $url);
441 3
        } elseif (!empty($this->routing[$this->slugs[$slug]]["default"])) {
442 3
            $url = ($absolute) ? Request::getInstance()->getRootUrl() . $this->routing[$this->slugs[$slug]]["default"] : $this->routing[$this->slugs[$slug]]["default"];
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 168 characters

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.

Loading history...
443
        }
444
445 3
        return preg_replace('/(GET|POST|PUT|DELETE|ALL)\#\|\#/', '', $url);
446
    }
447
448
    /**
449
     * Método que devuelve las rutas de administración
450
     * @deprecated
451
     * @return array
452
     */
453
    public function getAdminRoutes()
454
    {
455
        return AdminHelper::getAdminRoutes($this->routing);
456
    }
457
458
    /**
459
     * Método que devuelve le controlador del admin
460
     * @deprecated
461
     * @return Admin
462
     */
463
    public function getAdmin()
464
    {
465
        return Admin::getInstance();
466
    }
467
468
    /**
469
     * Método que extrae los dominios
470
     * @return array
471
     */
472 1
    public function getDomains()
473
    {
474 1
        return $this->domains ?: [];
475
    }
476
477
    /**
478
     * Método que ejecuta una acción del framework y revisa si lo tenemos cacheado ya o no
479
     *
480
     * @param string $route
481
     * @param array|null $action
482
     * @param types\Controller $class
483
     * @param array $params
0 ignored issues
show
Documentation introduced by
Should the type for parameter $params not be array|null? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
484
     */
485
    protected function executeCachedRoute($route, $action, $class, $params = NULL)
486
    {
487
        Logger::log('Executing route ' . $route, LOG_INFO);
488
        $action['params'] = array_merge($action['params'], $params, Request::getInstance()->getQueryParams());
489
        Security::getInstance()->setSessionKey("__CACHE__", $action);
490
        $cache = Cache::needCache();
491
        $execute = TRUE;
492
        if (FALSE !== $cache && Config::getParam('debug') === FALSE && $action['http'] === 'GET') {
493
            list($path, $cacheDataName) = $this->cache->getRequestCacheHash();
494
            $cachedData = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $path . $cacheDataName,
495
                $cache, null);
0 ignored issues
show
Bug introduced by
It seems like $cache defined by \PSFS\base\Cache::needCache() on line 490 can also be of type boolean; however, PSFS\base\Cache::readFromCache() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
496
            if (NULL !== $cachedData) {
497
                $headers = $this->cache->readFromCache("json" . DIRECTORY_SEPARATOR . $path . $cacheDataName . ".headers",
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 122 characters

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.

Loading history...
498
                    $cache, null, Cache::JSON);
0 ignored issues
show
Bug introduced by
It seems like $cache defined by \PSFS\base\Cache::needCache() on line 490 can also be of type boolean; however, PSFS\base\Cache::readFromCache() does only seem to accept integer, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
499
                Template::getInstance()->renderCache($cachedData, $headers);
500
                $execute = FALSE;
501
            }
502
        }
503
        if ($execute) {
504
            Logger::log(_('Start executing action'), LOG_DEBUG);
505
            if (false === call_user_func_array(array($class, $action['method']), $params)) {
506
                Logger::log(_('An error ocurred trying to execute the action'), LOG_ERR, [error_get_last()]);
507
            }
508
        }
509
    }
510
511
    /**
512
     * Parse slugs to create translations
513
     *
514
     * @param string $absoluteTranslationFileName
515
     */
516 1
    private function generateSlugs($absoluteTranslationFileName)
517
    {
518 1
        $translations = I18nHelper::generateTranslationsFile($absoluteTranslationFileName);
519 1
        foreach ($this->routing as $key => &$info) {
520 1
            $keyParts = explode("#|#", $key);
521 1
            $keyParts = array_key_exists(1, $keyParts) ? $keyParts[1] : $keyParts[0];
522 1
            $slug = RouterHelper::slugify($keyParts);
523 1
            if (NULL !== $slug && !array_key_exists($slug, $translations)) {
524 1
                $translations[$slug] = $info['label'];
525 1
                file_put_contents($absoluteTranslationFileName, "\$translations[\"{$slug}\"] = _(\"{$slug}\");\n", FILE_APPEND);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 128 characters

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.

Loading history...
526
            }
527 1
            $this->slugs[$slug] = $key;
528 1
            $info["slug"] = $slug;
529
        }
530 1
    }
531
532
    /**
533
     * @param bool $hydrateRoute
534
     * @param $modulePath
535
     * @param $externalModulePath
536
     */
537
    private function loadExternalAutoloader($hydrateRoute, SplFileInfo $modulePath, $externalModulePath)
538
    {
539
        $extModule = $modulePath->getBasename();
540
        $moduleAutoloader = realpath($externalModulePath . DIRECTORY_SEPARATOR . $extModule . DIRECTORY_SEPARATOR . 'autoload.php');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 132 characters

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.

Loading history...
541
        @include $moduleAutoloader;
542
        if ($hydrateRoute) {
543
            $this->routing = $this->inspectDir($externalModulePath . DIRECTORY_SEPARATOR . $extModule, '\\' . $extModule, $this->routing);
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 138 characters

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.

Loading history...
544
        }
545
    }
546
547
    /**
548
     * @param $hydrateRoute
549
     * @param $module
550
     * @return mixed
551
     */
552 1
    private function loadExternalModule($hydrateRoute, $module)
553
    {
554
        try {
555 1
            $module = preg_replace('/(\\\|\/)/', DIRECTORY_SEPARATOR, $module);
556 1
            $externalModulePath = VENDOR_DIR . DIRECTORY_SEPARATOR . $module . DIRECTORY_SEPARATOR . 'src';
557 1
            $externalModule = $this->finder->directories()->in($externalModulePath)->depth(0);
558
            foreach ($externalModule as $modulePath) {
559
                $this->loadExternalAutoloader($hydrateRoute, $modulePath, $externalModulePath);
560
            }
561 1
        } catch (\Exception $e) {
562 1
            Logger::log($e->getMessage(), LOG_WARNING);
563 1
            $module = null;
564
        }
565 1
        return $module;
566
    }
567
568
}
569