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