1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (C) 2022-2023 Rafael San José Tovar <[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
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Alxarafe\Core\Helpers; |
20
|
|
|
|
21
|
|
|
use Alxarafe\Core\Singletons\Debug; |
22
|
|
|
|
23
|
|
|
class Dispatcher |
24
|
|
|
{ |
25
|
|
|
public function __construct() |
26
|
|
|
{ |
27
|
|
|
new Globals(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public static function getFolders($folder): array |
31
|
|
|
{ |
32
|
|
|
$modulesFolder = constant('BASE_DIR') . '/Modules/'; |
33
|
|
|
$folder = trim($folder, '/') . '/'; |
34
|
|
|
|
35
|
|
|
$return = []; |
36
|
|
|
$return[] = constant('BASE_DIR') . '/src/' . $folder; |
37
|
|
|
foreach (scandir($modulesFolder) as $path) { |
38
|
|
|
if ($path === '.' || $path === '..') { |
39
|
|
|
continue; |
40
|
|
|
} |
41
|
|
|
$tableFolder = $modulesFolder . $path . '/' . $folder; |
42
|
|
|
if (is_dir($tableFolder)) { |
43
|
|
|
$return[] = $tableFolder; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
return $return; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Busca archivos en las carpetas de los módulos y del núcleo, retornando un array con todos los archivos |
51
|
|
|
* encontrados. |
52
|
|
|
* |
53
|
|
|
* TODO: Hay que resolver los siguientes puntos en cuanto se comience a trabajar con módulos: |
54
|
|
|
* - Es posible que un archivo pueda estar repetido, en cuyo caso, habrá que ver cómo solventarlo. |
55
|
|
|
* - Es posible que el orden importe, por ejemplo porque un módulo sobreescriba una funcionalidad. |
56
|
|
|
* |
57
|
|
|
* @author Rafael San José Tovar <[email protected]> |
58
|
|
|
* @version 2023.0105 |
59
|
|
|
* |
60
|
|
|
* @param string $folder |
61
|
|
|
* @param string $extension |
62
|
|
|
* |
63
|
|
|
* @return array |
64
|
|
|
*/ |
65
|
|
|
public static function getFiles(string $folder, string $extension): array |
66
|
|
|
{ |
67
|
|
|
if (strpos($extension, '.') !== 0) { |
68
|
|
|
$extension = '.' . $extension; |
69
|
|
|
} |
70
|
|
|
$length = strlen($extension); |
71
|
|
|
|
72
|
|
|
$return = []; |
73
|
|
|
foreach (self::getFolders($folder) as $path) { |
74
|
|
|
foreach (scandir($path) as $file) { |
75
|
|
|
if ($file != '.' && $file != '..' && substr($file, -$length) == $extension) { |
76
|
|
|
$return[substr($file, 0, strlen($file) - $length)] = $path . $file; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return $return; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function run(): bool |
84
|
|
|
{ |
85
|
|
|
$module = ucfirst($_GET[Globals::MODULE_GET_VAR] ?? Globals::DEFAULT_MODULE_NAME); |
86
|
|
|
$controller = ucfirst($_GET[Globals::CONTROLLER_GET_VAR] ?? Globals::DEFAULT_CONTROLLER_NAME); |
87
|
|
|
Debug::addMessage('messages', "Dispatcher::process() trying for '$module':'$controller'"); |
88
|
|
|
if ($this->processFolder($module, $controller)) { |
89
|
|
|
Debug::addMessage('messages', "Dispatcher::process(): Ok"); |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function processFolder(string $module, string $controller, string $method = 'main'): bool |
96
|
|
|
{ |
97
|
|
|
if ($module === ucfirst(Globals::DEFAULT_MODULE_NAME)) { |
98
|
|
|
$className = 'Alxarafe\\Controllers\\' . $controller; |
99
|
|
|
$filename = constant('BASE_DIR') . '/src/Controllers/' . $controller . '.php'; |
100
|
|
|
} else { |
101
|
|
|
$className = constant('MODULES_DIR') . '\\' . $module . '\\Controllers\\' . $controller; |
102
|
|
|
$filename = constant('BASE_DIR') . '/' . constant('MODULES_DIR') . '/' . $module . '/Controllers/' . $controller . '.php'; |
103
|
|
|
} |
104
|
|
|
if (file_exists($filename)) { |
105
|
|
|
Debug::addMessage('messages', "$className exists!"); |
106
|
|
|
$controller = new $className(); |
107
|
|
|
$controller->{$method}(); |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|