Test Failed
Push — main ( 2b582c...894963 )
by Rafael
05:38
created

Routes::getAllRoutes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 17
rs 9.9666
c 1
b 0
f 0
cc 3
nc 3
nop 0
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 * or see https://www.gnu.org/
18
 */
19
20
namespace Alxarafe\Lib;
21
22
abstract class Routes
23
{
24
    private static $routes = [];
25
26
    private static $search_routes = [
27
        'Modules' => 'Modules/',
28
        'CoreModules' => 'vendor/rsanjoseo/alxarafe/src/Modules/',
29
    ];
30
31
    public static function addRoutes(array $routes = [])
32
    {
33
        static::$routes = [];
0 ignored issues
show
Bug introduced by
Since $routes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $routes to at least protected.
Loading history...
34
        static::$search_routes = array_merge($routes, static::$search_routes);
0 ignored issues
show
Bug introduced by
Since $search_routes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $search_routes to at least protected.
Loading history...
35
    }
36
37
    private static function getRoutesFor($class_type, $suffix): array
38
    {
39
        $routes = [];
40
41
        foreach (static::$search_routes as $class => $route) {
0 ignored issues
show
Bug introduced by
Since $search_routes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $search_routes to at least protected.
Loading history...
42
            $full_path = realpath(constant('BASE_PATH') . '/../' . $route);
43
            if (empty($full_path)) {
44
                continue;
45
            }
46
47
            $modules = glob($full_path . '/*', GLOB_ONLYDIR);
48
49
            foreach ($modules as $module_path) {
50
                $module = basename($module_path);
51
                $data = glob($module_path . '/' . $class_type . '/*' . $suffix . '.php');
52
53
                foreach ($data as $filename) {
54
                    $class_only_name = basename($filename, $suffix . '.php');
55
                    $class_name = "$class\\$module\\$class_type\\$class_only_name$suffix";
56
57
                    $routes[$module][$class_only_name] = $class_name.'|'.$filename;
58
                }
59
60
            }
61
        }
62
63
        return $routes;
64
    }
65
66
    public static function getAllRoutes()
67
    {
68
        if (!empty(static::$routes)) {
0 ignored issues
show
Bug introduced by
Since $routes is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $routes to at least protected.
Loading history...
69
            return static::$routes;
70
        }
71
72
        $routes = [
73
            'Controller' => 'Controller',
74
            'Api' => 'Controller',
75
            'Model' => '',
76
        ];
77
78
        foreach ($routes as $class_type => $suffix) {
79
            static::$routes[$class_type] = static::getRoutesFor($class_type, $suffix);
80
        }
81
82
        return static::$routes;
83
    }
84
85
86
}
87