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
|
|
|
* 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
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Lib; |
20
|
|
|
|
21
|
|
|
use DebugBar\DebugBarException; |
22
|
|
|
|
23
|
|
|
class Dispatcher |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* Run the controller for the indicated module, if it exists. |
27
|
|
|
* Returns true if it can be executed. |
28
|
|
|
* |
29
|
|
|
* @param string $module |
30
|
|
|
* @param string $controller |
31
|
|
|
* @param array $alternative_routes |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
* @throws DebugBarException |
35
|
|
|
*/ |
36
|
|
|
public static function run(string $module, string $controller, array $alternative_routes = []): bool |
37
|
|
|
{ |
38
|
|
|
self::initialize(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Adding core module path |
42
|
|
|
*/ |
43
|
|
|
$routes = array_merge($alternative_routes, [ |
44
|
|
|
'CoreModules' => 'vendor/rsanjoseo/alxarafe/src/Modules/', |
45
|
|
|
]); |
46
|
|
|
$controller .= 'Controller'; |
47
|
|
|
foreach ($routes as $class => $route) { |
48
|
|
|
if (static::processFolder($class, $route, $module, $controller)) { |
49
|
|
|
Debug::message("Dispatcher::process(): Ok"); |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
Debug::message("Dispatcher::fail(): $module:$controller."); |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Initializes all the utilities and libraries of the framework environment. |
59
|
|
|
* |
60
|
|
|
* @return void |
61
|
|
|
* @throws DebugBarException |
62
|
|
|
*/ |
63
|
|
|
private static function initialize() |
64
|
|
|
{ |
65
|
|
|
self::initializeConstants(); |
66
|
|
|
|
67
|
|
|
Trans::initialize(); |
68
|
|
|
Debug::initialize(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private static function initializeConstants() |
72
|
|
|
{ |
73
|
|
|
/** |
74
|
|
|
* Define BASE_PATH if it does not exist. |
75
|
|
|
* It's usually created in main index.php. |
76
|
|
|
* It's the full path to the public folder. |
77
|
|
|
*/ |
78
|
|
|
Functions::defineIfNotDefined('ALX_PATH', realpath(__DIR__ . '/../../..')); |
79
|
|
|
Functions::defineIfNotDefined('APP_PATH', realpath(constant('ALX_PATH') . '/../../..')); |
80
|
|
|
Functions::defineIfNotDefined('BASE_PATH', constant('APP_PATH') . '/public'); |
81
|
|
|
Functions::defineIfNotDefined('BASE_URL', Functions::getUrl()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Process modern application controller paths. |
86
|
|
|
* |
87
|
|
|
* @param string $class |
88
|
|
|
* @param string $route |
89
|
|
|
* @param string $module |
90
|
|
|
* @param string $controller |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
private static function processFolder(string $class, string $route, string $module, string $controller): bool |
94
|
|
|
{ |
95
|
|
|
/** |
96
|
|
|
* Defines the full path ($realpath) to the modules folder ($route). |
97
|
|
|
*/ |
98
|
|
|
$realpath = realpath(constant('APP_PATH') . '/' . $route); |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Adds the module to the path ($basepath), if it's a module. |
102
|
|
|
*/ |
103
|
|
|
$basepath = $realpath; |
104
|
|
|
if (!empty($module)) { |
105
|
|
|
$basepath = $realpath . '/' . $module; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Defines full classname and filename |
110
|
|
|
*/ |
111
|
|
|
$className = $class . '\\' . $module . '\\Controller\\' . $controller; |
112
|
|
|
$filename = $basepath . '/Controller/' . $controller . '.php'; |
113
|
|
|
|
114
|
|
|
Debug::message('Filename: ' . $filename); |
115
|
|
|
Debug::message('Class: ' . $className); |
116
|
|
|
if (!file_exists($filename)) { |
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
$controller = new $className(); |
121
|
|
|
if ($controller === null) { |
122
|
|
|
return false; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* If the class exists and is successfully instantiated, the module blade templates folder |
127
|
|
|
* is added, if they exist. |
128
|
|
|
*/ |
129
|
|
|
if (method_exists($controller, 'setTemplatesPath')) { |
130
|
|
|
$templates_path = $basepath . '/Templates'; |
131
|
|
|
Debug::message('Templates: ' . $templates_path); |
132
|
|
|
$controller->setTemplatesPath($templates_path); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Runs the index method to launch the controller. |
137
|
|
|
*/ |
138
|
|
|
$controller->index(); |
139
|
|
|
|
140
|
|
|
return true; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|