Passed
Push — main ( 4acb40...2e52e3 )
by Dimitri
03:07
created

FileLocator::findLangFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 3
nop 3
dl 0
loc 25
rs 9.9
c 0
b 0
f 0
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\Contracts\Database\ConnectionInterface;
15
use BlitzPHP\Exceptions\LoadException;
16
use BlitzPHP\Utilities\Helpers;
17
18
class FileLocator
19
{
20
    /**
21
     * Charge un fichier d'aide (helper)
22
     *
23
     * @return void
24
     */
25
    public static function helper(string $helper)
26
    {
27
        $file  = Helpers::ensureExt($helper, 'php');
28
        $paths = [
29
            // Helpers système
30
            SYST_PATH . 'Helpers' . DS . $file,
31
32
            // Helpers de l'application
33
            APP_PATH . 'Helpers' . DS . $file,
34
        ];
35
        $file_exist = false;
36
37
        foreach ($paths as $path) {
38
            if (file_exists($path)) {
39
                require_once $path;
40
                $file_exist = true;
41
            }
42
        }
43
44
        if (true !== $file_exist) {
45
            throw LoadException::helperNotFound($helper);
46
        }
47
    }
48
49
    /**
50
     * Cree et renvoie une librairie donnée
51
     *
52
     * @return mixed
53
     */
54
    public static function library(string $library)
55
    {
56
        $library = str_replace(DS, '/', $library);
57
        $library = explode('/', $library);
58
59
        $lib                          = ucfirst(end($library));
60
        $library[count($library) - 1] = $lib;
61
62
        $file  = Helpers::ensureExt(implode(DS, $library), 'php');
63
        $paths = [
64
            SYST_PATH . 'Libraries' . DS . $file,
65
66
            APP_PATH . 'Libraries' . DS . $file,
67
        ];
68
        $file_syst = $file_exist = false;
69
70
        if (file_exists($paths[0])) {
71
            $lib       = "BlitzPhp\\Libraries\\{$lib}";
72
            $file_syst = $file_exist = true;
73
        } elseif (file_exists($paths[1])) {
74
            require_once $paths[1];
75
            $file_exist = true;
76
        }
77
78
        if (true !== $file_exist) {
79
            throw LoadException::libraryNotFound($lib);
80
        }
81
82
        if (true !== $file_syst && ! class_exists($lib)) {
83
            throw LoadException::libraryDontExist($lib);
84
        }
85
86
        return Injector::make($lib);
87
    }
88
89
    /**
90
     * Cree et renvoi un model donné
91
     *
92
     * @template T of \BlitzPHP\Models\BaseModel
93
     *
94
     * @param class-string<T> $model
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...
95
     *
96
     * @return T
97
     */
98
    public static function model(string $model, array $options = [], ?ConnectionInterface $connection = null)
99
    {
100
        $options = array_merge([
101
            'preferApp' => true,
102
        ], $options);
103
104
        if (! preg_match('#Model$#', $model)) {
105
            $model .= 'Model';
106
        }
107
108
        if ($options['preferApp'] === true) {
109
            // $model = self::getBasename($model);
110
111
            $model = str_replace(APP_NAMESPACE . '\\Models\\', '', $model);
112
            $model = APP_NAMESPACE . '\\Models\\' . $model;
113
        }
114
115
        if (! class_exists($model)) {
116
            throw LoadException::modelNotFound($model);
117
        }
118
119
        return Injector::make($model, [$connection]);
120
    }
121
122
    /**
123
     * Cree et renvoi un controleur donné
124
     *
125
     * @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...
126
     */
127
    public static function controller(string $controller)
128
    {
129
        $controller = str_replace(DS, '/', $controller);
130
        $controller = explode('/', $controller);
131
132
        $con                                = ucfirst(end($controller));
133
        $con                                = (! preg_match('#Controller$#', $con)) ? $con . 'Controller' : $con;
134
        $controller[count($controller) - 1] = $con;
135
136
        foreach ($controller as $key => &$value) {
137
            if (preg_match('#^Controllers?$#i', $value)) {
138
                unset($value, $controller[$key]);
139
            }
140
        }
141
142
        $path = CONTROLLER_PATH . Helpers::ensureExt(implode(DS, $controller), 'php');
143
144
        if (! file_exists($path)) {
145
            throw LoadException::controllerNotFound(str_replace('Controller', '', $con), $path);
146
        }
147
148
        require_once $path;
149
150
        $class_namespaced = implode('\\', $controller);
151
152
        if (class_exists($class_namespaced, false)) {
153
            return Injector::make($class_namespaced);
154
        }
155
        if (! class_exists($con, false)) {
156
            throw LoadException::controllerDontExist(str_replace('Controller', '', $con), $path);
157
        }
158
159
        return Injector::make($con);
160
    }
161
162
    /**
163
     * Recupere le nom de base a partir du nom de la classe, namespacé ou non.
164
     */
165
    public static function getBasename(string $name): string
166
    {
167
        // Determine le basename
168
        if ($basename = strrchr($name, '\\')) {
169
            return substr($basename, 1);
170
        }
171
172
        return $name;
173
    }
174
175
    /**
176
     * Verifie si la classe satisfait l'option "preferApp"
177
     *
178
     * @param array  $options directives specifier pqr le composant
179
     * @param string $name    Nom de la classe, namespace optionel
180
     */
181
    protected static function verifyPreferApp(array $options, string $name): bool
182
    {
183
        // Tout element sans restriction passe
184
        if (! $options['preferApp']) {
185
            return true;
186
        }
187
188
        return strpos($name, APP_NAMESPACE) === 0;
189
    }
190
}
191