|
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 |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|