Test Failed
Push — main ( 330702...6ea9a3 )
by Rafael
14:31
created

Dispatcher::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
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\Trans;
23
use DebugBar\DebugBarException;
24
25
class Dispatcher
26
{
27
    /**
28
     * Run the controller for the indicated module, if it exists.
29
     * Returns true if it can be executed.
30
     *
31
     * @param string $module
32
     * @param string $controller
33
     * @param array $alternative_routes
34
     *
35
     * @return bool
36
     * @throws DebugBarException
37
     */
38
    public static function run(string $module, string $controller, 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)) {
51
                Debug::message("Dispatcher::process(): Ok");
52
                return true;
53
            }
54
        }
55
        Debug::message("Dispatcher::fail(): $module:$controller.");
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::initialize_constants();
68
69
        Trans::initialize();
70
        Debug::initialize();
71
    }
72
73
    private static function initialize_constants()
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
     * @return bool
94
     */
95
    private static function processFolder(string $class, string $route, string $module, string $controller): bool
96
    {
97
        /**
98
         * Defines the full path ($realpath) to the modules folder ($route).
99
         */
100
        $realpath = realpath(constant('APP_PATH') . '/' . $route);
101
102
        /**
103
         * Adds the module to the path ($basepath), if it's a module.
104
         */
105
        $basepath = $realpath;
106
        if (!empty($module)) {
107
            $basepath = $realpath . '/' . $module;
108
        }
109
110
        /**
111
         * Defines full classname and filename
112
         */
113
        $className = $class . '\\' . $module . '\\Controller\\' . $controller;
114
        $filename = $basepath . '/Controller/' . $controller . '.php';
115
116
        Debug::message('Filename: ' . $filename);
117
        Debug::message('Class: ' . $className);
118
        if (!file_exists($filename)) {
119
            return false;
120
        }
121
122
        $controller = new $className();
123
        if ($controller === null) {
124
            return false;
125
        }
126
127
        /**
128
         * If the class exists and is successfully instantiated, the module blade templates folder
129
         * is added, if they exist.
130
         */
131
        if (method_exists($controller, 'setTemplatesPath')) {
132
            $templates_path = $basepath . '/Templates';
133
            Debug::message('Templates: ' . $templates_path);
134
            $controller->setTemplatesPath($templates_path);
135
        }
136
137
        /**
138
         * Runs the index method to launch the controller.
139
         */
140
        $controller->index();
141
142
        return true;
143
    }
144
}
145