-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoload.php
311 lines (239 loc) · 7.78 KB
/
Autoload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/base/blob/master/LICENSE
*/
namespace Quid\Base;
// autoload
// class with methods a layer over the native PHP autoload logic
final class Autoload extends Root
{
// config
protected static array $config = [
'psr4'=>[] // garde une copie du racine de l'auto chargement des classes
];
// construct
// constructeur protégé
final protected function __construct() { }
// call
// recherche un nom de classe à travers tout le pool autoload
final public static function call(string $class):bool
{
$return = false;
if(!class_exists($class,false))
{
spl_autoload_call($class);
$return = (class_exists($class,false));
}
return $return;
}
// getExtensions
// retourne les extensions utilisés dans l'implémentation par défaut
final public static function getExtensions():string
{
return spl_autoload_extensions();
}
// setExtensions
// change les extensions utilisés dans l'implémentation par défaut
final public static function setExtensions($value):bool
{
$return = false;
if($value === true)
$value = '.'.self::phpExtension();
if(is_array($value))
$value = implode(',',$value);
if(is_string($value))
{
$extension = spl_autoload_extensions($value);
$return = ($extension === $value);
}
return $return;
}
// register
// ajoute une fonction dans le pool des autoload
// si tout est vide, utilise l'implémentation spl_autoload par défaut qui se base sur les include path
// si call est true, utilise la méthode manual
// note: l'implémentation spl_autoload par défaut ne déclenche pas le callback __init sur les classes
final public static function register(?callable $call=null,bool $throw=true,bool $prepend=false):bool
{
$return = false;
if(!empty($call))
$return = spl_autoload_register($call,$throw,$prepend);
else
$return = spl_autoload_register();
return $return;
}
// unregister
// enlève une fonction du pool autoload
final public static function unregister(callable $call):bool
{
return spl_autoload_unregister($call);
}
// unregisterAll
// enlève toutes les fonctions autoload enregistrés
final public static function unregisterAll():array
{
$return = [];
foreach (self::all() as $key => $value)
{
$return[$key] = self::unregister($value);
}
return $return;
}
// all
// retourne les fonctions autoload enregistrés
final public static function all():array
{
$return = spl_autoload_functions();
if(!is_array($return))
$return = [];
return $return;
}
// index
// permet de retourner une callable autoload, via index
final public static function index(int $index):?callable
{
return Arr::index($index,self::all());
}
// getPath
// retourne le psr4 ainsi que le path à utiliser pour autoload la classe
// ceci est utilisé par getFilePath et getDirPath
final protected static function getPath(string $class,bool $different=true):?string
{
$return = null;
$psr4 = self::getPsr4($class,$different);
if(!empty($psr4))
{
$return = current($psr4);
$key = key($psr4);
$len = (strlen($key) + 1);
$after = substr($class,$len);
if(is_string($after) && strlen($after))
{
$after = str_replace('\\','/',$after);
$return .= '/'.$after;
}
}
return $return;
}
// getFilePath
// retourne un chemin de classe à partir d'un fqcn
// possible de spécifier s'il doit exister
final public static function getFilePath(string $class,bool $exists=true,bool $different=true):?string
{
$return = self::getPath($class,$different);
if(is_string($return))
{
$return .= '.'.self::phpExtension();
if($exists === true && !file_exists($return))
$return = null;
}
return $return;
}
// getDirPath
// retourne un chemin de dossier à partir d'un fqcn
// possible de spécifier s'il doit exister
final public static function getDirPath(string $class,bool $exists=true,bool $different=false):?string
{
$return = self::getPath($class,$different);
if(is_string($return) && $exists === true && !is_dir($return))
$return = null;
return $return;
}
// getPsr4
// retourne un tableau clé valeur avec le psr4 à utiliser
// sinon null
final public static function getPsr4(string $class,bool $different=false):?array
{
$return = null;
$source = self::$config['psr4'];
foreach ($source as $key => $value)
{
if($different === false || $class !== $key)
{
if(strpos($class,$key) === 0)
{
$return = [$key=>$value];
break;
}
}
}
return $return;
}
// setPsr4
// change ou ajoute un point racine
final public static function setPsr4(string $key,string $value):void
{
self::$config['psr4'][$key] = $value;
}
// setsPsr4
// change ou ajoute plusieurs racine de classe
final public static function setsPsr4(array $keyValue):void
{
foreach ($keyValue as $key => $value)
{
if(is_string($key) && is_string($value))
self::$config['psr4'][$key] = $value;
}
}
// unsetPsr4
// enlève un point racine
final public static function unsetPsr4(string $key):void
{
if(array_key_exists($key,self::$config['psr4']))
unset(self::$config['psr4'][$key]);
}
// allPsr4
// retourne le tableau des psr4
// possible de fournir un callback et de sort
final public static function allPsr4(?\Closure $closure=null,bool $sort=false):array
{
$return = self::$config['psr4'];
if(!empty($closure))
{
foreach ($return as $key => $value)
{
if($closure($key,$value) !== true)
unset($return[$key]);
}
}
if($sort === true)
ksort($return);
return $return;
}
// removeAlias
// retirer les noms de classes qui semblent être des alias
// les alias sont stockés en lowercase par php
// la logique quid vaut que la classe ait un namespace et que le nom termine par alias
final public static function removeAlias(array $return):array
{
foreach ($return as $key => $value)
{
if(strtolower($value) === $value && strpos($value,'\\') && substr($value,-5) === 'alias')
unset($return[$key]);
}
return $return;
}
// overview
// génère un tableau multidimensionnel avec le count, size et line pour chaque namespace dans psr4
// possible de filtrer par une closure
final public static function overview(?\Closure $closure=null,bool $sort=true):array
{
$return = [];
$extension = self::phpExtension();
foreach (self::allPsr4($closure,$sort) as $key => $value)
{
$return[$key] = Dir::overview($value,$extension);
}
return $return;
}
// phpExtension
// retourne l'extension de php
final public static function phpExtension():string
{
return 'php';
}
}
?>