Passed
Push — main ( 633b92...66245a )
by Dimitri
12:27
created

FileLocator   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 86
c 2
b 1
f 0
dl 0
loc 212
rs 10
wmc 30

7 Methods

Rating   Name   Duplication   Size   Complexity  
A verifyPreferApp() 0 8 2
A helper() 0 21 4
A getBasename() 0 8 2
B library() 0 33 6
A model() 0 22 4
A lang() 0 36 5
B controller() 0 33 7
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Loader;
13
14
use BlitzPHP\Database\Contracts\ConnectionInterface;
0 ignored issues
show
Bug introduced by
The type BlitzPHP\Database\Contracts\ConnectionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use BlitzPHP\Exceptions\LoadException;
16
use BlitzPHP\Utilities\Helpers;
17
18
class FileLocator
19
{
20
    /**
21
     * Charge un fichier de traduction
22
     */
23
    public static function lang(string $lang, string $locale): array
24
    {
25
        $file  = Helpers::ensureExt($lang, 'php');
26
        $paths = [
27
            // Chemin d'accès aux langues de l'application
28
            LANG_PATH . $locale . DS . $file,
29
30
            // Chemin vers les langues du système
31
            SYST_PATH . 'Constants' . DS . 'language' . DS . $locale . DS . $file,
32
33
            // Chemin d'accès aux langues de l'application
34
            LANG_PATH . config('app.language') . DS . $file,
35
36
            // Chemin vers les langues du système
37
            SYST_PATH . 'Constants' . DS . 'language' . DS . config('app.language') . DS . $file,
38
        ];
39
        $paths = array_unique($paths);
40
41
        $file_exist = false;
42
        $languages  = [];
43
44
        foreach ($paths as $path) {
45
            if (file_exists($path)) {
46
                if (! in_array($path, get_included_files(), true)) {
47
                    $languages = array_merge($languages, (array) require($path));
48
                }
49
                $file_exist = true;
50
                break;
51
            }
52
        }
53
54
        if (true !== $file_exist) {
55
            throw LoadException::langNotFound($lang);
56
        }
57
58
        return $languages;
59
    }
60
61
    /**
62
     * Charge un fichier d'aide (helper)
63
     *
64
     * @return void
65
     */
66
    public static function helper(string $helper)
67
    {
68
        $file  = Helpers::ensureExt($helper, 'php');
69
        $paths = [
70
            // Helpers système
71
            SYST_PATH . 'Helpers' . DS . $file,
72
73
            // Helpers de l'application
74
            APP_PATH . 'Helpers' . DS . $file,
75
        ];
76
        $file_exist = false;
77
78
        foreach ($paths as $path) {
79
            if (file_exists($path)) {
80
                require_once $path;
81
                $file_exist = true;
82
            }
83
        }
84
85
        if (true !== $file_exist) {
86
            throw LoadException::helperNotFound($helper);
87
        }
88
    }
89
90
    /**
91
     * Cree et renvoie une librairie donnée
92
     *
93
     * @return mixed
94
     */
95
    public static function library(string $library)
96
    {
97
        $library = str_replace(DS, '/', $library);
98
        $library = explode('/', $library);
99
100
        $lib                          = ucfirst(end($library));
101
        $library[count($library) - 1] = $lib;
102
103
        $file  = Helpers::ensureExt(implode(DS, $library), 'php');
104
        $paths = [
105
            SYST_PATH . 'Libraries' . DS . $file,
106
107
            APP_PATH . 'Libraries' . DS . $file,
108
        ];
109
        $file_syst = $file_exist = false;
110
111
        if (file_exists($paths[0])) {
112
            $lib       = "BlitzPhp\\Libraries\\{$lib}";
113
            $file_syst = $file_exist = true;
114
        } elseif (file_exists($paths[1])) {
115
            require_once $paths[1];
116
            $file_exist = true;
117
        }
118
119
        if (true !== $file_exist) {
120
            throw LoadException::libraryNotFound($lib);
121
        }
122
123
        if (true !== $file_syst && ! class_exists($lib)) {
124
            throw LoadException::libraryDontExist($lib);
125
        }
126
127
        return Injector::make($lib);
128
    }
129
130
    /**
131
     * Cree et renvoi un model donné
132
     *
133
     * @template T of \BlitzPHP\Models\BaseModel
134
     *
135
     * @param class-string<T> $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
136
     *
137
     * @return T
138
     */
139
    public static function model(string $model, array $options = [], ?ConnectionInterface $connection = null)
140
    {
141
        $options = array_merge([
142
            'preferApp' => true,
143
        ], $options);
144
145
        if (! preg_match('#Model$#', $model)) {
146
            $model .= 'Model';
147
        }
148
149
        if ($options['preferApp'] === true) {
150
            $model = self::getBasename($model);
151
152
            $model = str_replace(APP_NAMESPACE . '\\Models\\', '', $model);
153
            $model = APP_NAMESPACE . '\\Models\\' . $model;
154
        }
155
156
        if (! class_exists($model)) {
157
            throw LoadException::modelNotFound($model);
158
        }
159
160
        return Injector::make($model, [$connection]);
161
    }
162
163
    /**
164
     * Cree et renvoi un controleur donné
165
     *
166
     * @return \dFramework\core\controllers\BaseController
0 ignored issues
show
Bug introduced by
The type dFramework\core\controllers\BaseController was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
167
     */
168
    public static function controller(string $controller)
169
    {
170
        $controller = str_replace(DS, '/', $controller);
171
        $controller = explode('/', $controller);
172
173
        $con                                = ucfirst(end($controller));
174
        $con                                = (! preg_match('#Controller$#', $con)) ? $con . 'Controller' : $con;
175
        $controller[count($controller) - 1] = $con;
176
177
        foreach ($controller as $key => &$value) {
178
            if (preg_match('#^Controllers?$#i', $value)) {
179
                unset($value, $controller[$key]);
180
            }
181
        }
182
183
        $path = CONTROLLER_PATH . Helpers::ensureExt(implode(DS, $controller), 'php');
184
185
        if (! file_exists($path)) {
186
            throw LoadException::controllerNotFound(str_replace('Controller', '', $con), $path);
187
        }
188
189
        require_once $path;
190
191
        $class_namespaced = implode('\\', $controller);
192
193
        if (class_exists($class_namespaced, false)) {
194
            return Injector::make($class_namespaced);
195
        }
196
        if (! class_exists($con, false)) {
197
            throw LoadException::controllerDontExist(str_replace('Controller', '', $con), $path);
198
        }
199
200
        return Injector::make($con);
201
    }
202
203
    /**
204
     * Recupere le nom de base a partir du nom de la classe, namespacé ou non.
205
     */
206
    public static function getBasename(string $name): string
207
    {
208
        // Determine le basename
209
        if ($basename = strrchr($name, '\\')) {
210
            return substr($basename, 1);
211
        }
212
213
        return $name;
214
    }
215
216
    /**
217
     * Verifie si la classe satisfait l'option "preferApp"
218
     *
219
     * @param array  $options directives specifier pqr le composant
220
     * @param string $name    Nom de la classe, namespace optionel
221
     */
222
    protected static function verifyPreferApp(array $options, string $name): bool
223
    {
224
        // Tout element sans restriction passe
225
        if (! $options['preferApp']) {
226
            return true;
227
        }
228
229
        return strpos($name, APP_NAMESPACE) === 0;
230
    }
231
}
232