Test Failed
Push — main ( 894963...0f30b5 )
by Rafael
05:41
created

Dispatcher::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 2
b 0
f 0
cc 1
nc 1
nop 0
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;
20
21
use Alxarafe\Lib\Functions;
22
use Alxarafe\Lib\Routes;
23
use Alxarafe\Lib\Trans;
24
use Alxarafe\Tools\Dispatcher\ApiDispatcher;
25
use Alxarafe\Tools\Dispatcher\WebDispatcher;
26
use DebugBar\DebugBarException;
27
28
class Dispatcher
29
{
30
    /**
31
     * Get variable containing the name of the module to which the controller to be executed belongs.
32
     */
33
    public const MODULE = 'module';
34
35
    /**
36
     * Get variable containing the name of the controller to execute.
37
     */
38
    public const CONTROLLER = 'controller';
39
40
    /**
41
     * Get variable containing the name of the method to execute.
42
     * Not normally used. The action is executed automatically from the index method.
43
     */
44
    private const METHOD = 'method';
45
46
    /**
47
     * Run de selected controller.
48
     * Discriminates by URL whether to paint a web page, or return an API response.
49
     *
50
     * @throws DebugBarException
51
     */
52
    public static function run($alternative_routes = []): void
53
    {
54
        self::initialize();
55
56
        if (!empty($alternative_routes)) {
57
            Routes::addRoutes($alternative_routes);
58
        }
59
60
        $to_search = '/index.php/api/';
61
        $route = $_SERVER['PHP_SELF'];
62
        $pos = strpos($route, $to_search);
63
        if ($pos !== false) {
64
            $controller = substr($route, $pos + strlen($to_search));
65
            ApiDispatcher::run($controller);
66
            return;
67
        }
68
69
        $module = filter_input(INPUT_GET, static::MODULE) ?? 'Admin';
70
        $controller = filter_input(INPUT_GET, static::CONTROLLER) ?? 'Info';
71
        $method = filter_input(INPUT_GET, static::METHOD) ?? 'index';
72
        WebDispatcher::run($module, $controller, $method);
73
    }
74
75
    /**
76
     * Initializes all the utilities and libraries of the framework environment.
77
     *
78
     * @return void
79
     * @throws DebugBarException
80
     */
81
    private static function initialize()
82
    {
83
        self::initializeConstants();
84
85
        Trans::initialize();
86
    }
87
88
    private static function initializeConstants()
89
    {
90
        /**
91
         * Define BASE_PATH if it does not exist.
92
         * It's usually created in main index.php.
93
         * It's the full path to the public folder.
94
         */
95
        Functions::defineIfNotDefined('ALX_PATH', realpath(__DIR__ . '/../../..'));
96
        Functions::defineIfNotDefined('APP_PATH', realpath(constant('ALX_PATH') . '/../../..'));
97
        Functions::defineIfNotDefined('BASE_PATH', constant('APP_PATH') . '/public');
98
        Functions::defineIfNotDefined('BASE_URL', Functions::getUrl());
99
    }
100
}
101