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\Tools\Dispatcher; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Lib\Routes; |
22
|
|
|
use Alxarafe\Tools\Debug; |
23
|
|
|
use CoreModules\Admin\Controller\Error404Controller; |
24
|
|
|
use DebugBar\DebugBarException; |
25
|
|
|
|
26
|
|
|
class WebDispatcher extends Dispatcher |
27
|
|
|
{ |
28
|
|
|
protected static function dieWithMessage($message) |
29
|
|
|
{ |
30
|
|
|
Debug::message('WebDispatcher error: '.$message); |
31
|
|
|
new Error404Controller(); |
32
|
|
|
die(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Run the controller for the indicated module, if it exists. |
37
|
|
|
* Returns true if it can be executed. |
38
|
|
|
* |
39
|
|
|
* @param string $module |
40
|
|
|
* @param string $controller |
41
|
|
|
* @param string $method |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
* @throws DebugBarException |
45
|
|
|
*/ |
46
|
|
|
public static function run(string $module, string $controller, string $method): bool |
47
|
|
|
{ |
48
|
|
|
Debug::initialize(); |
49
|
|
|
|
50
|
|
|
$routes = Routes::getAllRoutes(); |
51
|
|
|
$endpoint = $routes['Controller'][$module][$controller] ?? null; |
52
|
|
|
if ($endpoint === null) { |
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
Debug::message("Dispatcher::runWeb executing $module::$controller ($endpoint)"); |
57
|
|
|
$route_array = explode('|', $endpoint); |
58
|
|
|
$className = $route_array[0]; |
59
|
|
|
$filename = $route_array[1]; |
60
|
|
|
|
61
|
|
|
if (!file_exists($filename)) { |
62
|
|
|
Debug::message("Dispatcher::runWeb error: $filename does not exists"); |
63
|
|
|
new Error404Controller(); |
64
|
|
|
return false; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
require_once $filename; |
68
|
|
|
|
69
|
|
|
$controller = new $className(); |
70
|
|
|
if ($controller === null) { |
71
|
|
|
Debug::message("Dispatcher::runApi error: $className not found"); |
72
|
|
|
new Error404Controller(); |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$templates_path = [ |
77
|
|
|
constant('ALX_PATH') . '/src/Modules/' . $module . '/Templates/', |
78
|
|
|
constant('BASE_PATH') . '/../Modules/' . $module . '/Templates/', |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* If the class exists and is successfully instantiated, the module blade templates folder |
83
|
|
|
* is added, if they exist. |
84
|
|
|
*/ |
85
|
|
|
if (method_exists($controller, 'setTemplatesPath')) { |
86
|
|
|
Debug::message('Templates: ' . $templates_path[0]); |
87
|
|
|
Debug::message('Templates: ' . $templates_path[1]); |
88
|
|
|
$controller->setTemplatesPath($templates_path); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (!method_exists($controller, $method)) { |
92
|
|
|
Debug::message('Method ' . $method . ' not found in controller ' . $className); |
93
|
|
|
$method = 'index'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Runs the index method to launch the controller. |
98
|
|
|
*/ |
99
|
|
|
$controller->{$method}(); |
100
|
|
|
|
101
|
|
|
return true; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.