|
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 DoliCore\Tools; |
|
20
|
|
|
|
|
21
|
|
|
use Alxarafe\Tools\Debug; |
|
|
|
|
|
|
22
|
|
|
use Alxarafe\Tools\Dispatcher as DispatcherBase; |
|
23
|
|
|
|
|
24
|
|
|
class Dispatcher extends DispatcherBase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Process controller paths imported from Dolibarr. Try to locate it as a |
|
28
|
|
|
* modern controller first. |
|
29
|
|
|
* |
|
30
|
|
|
* This code is duplicated in Core/Dispatcher. |
|
31
|
|
|
* It can be refactored by creating an array with the possible "className" and |
|
32
|
|
|
* "filename", and looping through them all from Core/Dispatcher. |
|
33
|
|
|
* In that case, Dolibarr/Dispatcher would only have to add the new routes to |
|
34
|
|
|
* search for your drivers. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $module |
|
37
|
|
|
* @param string $controller |
|
38
|
|
|
* @return bool |
|
39
|
|
|
*/ |
|
40
|
|
|
protected static function processFolder(string $module, string $controller): bool |
|
41
|
|
|
{ |
|
42
|
|
|
if (parent::processFolder($module, $controller)) { |
|
43
|
|
|
return true; |
|
44
|
|
|
} |
|
45
|
|
|
$className = 'DoliModules\\' . $module . '\\Controller\\' . $controller; |
|
46
|
|
|
$filename = realpath(constant('BASE_PATH') . '/../Dolibarr/Modules/' . $module . '/Controller/' . $controller . '.php'); |
|
47
|
|
|
Debug::message('Filename: ' . $filename); |
|
48
|
|
|
Debug::message('Class: ' . $className); |
|
49
|
|
|
if (!file_exists($filename)) { |
|
50
|
|
|
return false; |
|
51
|
|
|
} |
|
52
|
|
|
$controller = new $className(); |
|
53
|
|
|
if ($controller === null) { |
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
$controller->index(); |
|
57
|
|
|
return true; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: