|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\AutoLoader; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\AutoLoader\Exception as AutoloadException; |
|
6
|
|
|
use Nip\AutoLoader\Loaders\AbstractLoader; |
|
7
|
|
|
use Nip\AutoLoader\Loaders\ClassMap; |
|
8
|
|
|
use Nip\AutoLoader\Loaders\Psr4Class; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class AutoLoader |
|
12
|
|
|
* @package Nip\AutoLoader |
|
13
|
|
|
*/ |
|
14
|
|
|
class AutoLoader |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var bool |
|
19
|
|
|
*/ |
|
20
|
|
|
public static $splHandler = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var AbstractLoader[] |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $loaders = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $cachePath; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $ignoreTokens = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param self $autoloader |
|
39
|
|
|
* @return bool |
|
40
|
|
|
* @throws Exception |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function registerHandler($autoloader) |
|
43
|
|
|
{ |
|
44
|
|
|
// Only register once per instance |
|
45
|
1 |
|
if (static::$splHandler) { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
1 |
|
if ($autoloader instanceof self) { |
|
50
|
1 |
|
return spl_autoload_register([$autoloader, 'autoload']); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
throw new AutoloadException('Invalid AutoLoader specified in register handler'); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Singleton |
|
58
|
|
|
* |
|
59
|
|
|
* @return self |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function instance() |
|
62
|
|
|
{ |
|
63
|
|
|
static $instance; |
|
64
|
|
|
if (!($instance instanceof self)) { |
|
65
|
|
|
$instance = new self(); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $instance; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param $dir |
|
73
|
|
|
* @return $this |
|
74
|
|
|
*/ |
|
75
|
|
|
public function addDirectory($dir) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->getClassMapLoader()->addDirectory($dir); |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return ClassMap |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getClassMapLoader() |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getLoader('ClassMap'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param $name |
|
92
|
|
|
* @return AbstractLoader |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getLoader($name) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!$this->hasLoader($name)) { |
|
97
|
|
|
$this->initLoader($name); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->loaders[$name]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $name |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
public function hasLoader($name) |
|
108
|
|
|
{ |
|
109
|
|
|
return isset($this->loaders[$name]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param $name |
|
114
|
|
|
*/ |
|
115
|
|
|
public function initLoader($name) |
|
116
|
|
|
{ |
|
117
|
|
|
$loader = $this->newLoader($name); |
|
118
|
|
|
$this->addLoader($name, $loader); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param $name |
|
123
|
|
|
* @return AbstractLoader |
|
124
|
|
|
*/ |
|
125
|
|
|
public function newLoader($name) |
|
126
|
|
|
{ |
|
127
|
|
|
$class = $this->getLoaderClass($name); |
|
128
|
|
|
$loader = new $class; |
|
129
|
|
|
|
|
130
|
|
|
return $loader; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $name |
|
135
|
|
|
* @return string |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getLoaderClass($name) |
|
138
|
|
|
{ |
|
139
|
|
|
return 'Nip\AutoLoader\Loaders\\'.$name; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param $name |
|
144
|
|
|
* @param AbstractLoader $loader |
|
145
|
|
|
*/ |
|
146
|
|
|
public function addLoader($name, $loader) |
|
147
|
|
|
{ |
|
148
|
|
|
$loader->setAutoLoader($this); |
|
149
|
|
|
$this->loaders[$name] = $loader; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param $prefix |
|
154
|
|
|
* @param $baseDir |
|
155
|
|
|
* @return $this |
|
156
|
|
|
*/ |
|
157
|
|
|
public function addNamespace($prefix, $baseDir) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->getPsr4ClassLoader()->addPrefix($prefix, $baseDir); |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @return Psr4Class |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getPsr4ClassLoader() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->getLoader('Psr4Class'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function initLoaders() |
|
173
|
|
|
{ |
|
174
|
|
|
$loaderNames = $this->getLoaderOrder(); |
|
175
|
|
|
foreach ($loaderNames as $name) { |
|
176
|
|
|
$this->initLoader($name); |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return array |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getLoaderOrder() |
|
184
|
|
|
{ |
|
185
|
|
|
return ['Psr4', 'ClassMap']; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @return mixed |
|
190
|
|
|
*/ |
|
191
|
|
|
public function getCachePath() |
|
192
|
|
|
{ |
|
193
|
|
|
return $this->cachePath; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* @param $path |
|
198
|
|
|
* @return $this |
|
199
|
|
|
*/ |
|
200
|
|
|
public function setCachePath($path) |
|
201
|
|
|
{ |
|
202
|
|
|
$this->cachePath = $path; |
|
203
|
|
|
|
|
204
|
|
|
return $this; |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
/** |
|
208
|
|
|
* @param $class |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
|
|
public function isClass($class) |
|
212
|
|
|
{ |
|
213
|
|
|
return is_file($this->getClassLocation($class)); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param $class |
|
218
|
|
|
* @return null|string |
|
219
|
|
|
*/ |
|
220
|
|
|
public function getClassLocation($class) |
|
221
|
|
|
{ |
|
222
|
|
|
$loaders = $this->getLoaders(); |
|
223
|
|
|
foreach ($loaders as $loader) { |
|
224
|
|
|
$path = $loader->getClassLocation($class); |
|
225
|
|
|
if ($path) { |
|
|
|
|
|
|
226
|
|
|
return $path; |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
return null; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* @return AbstractLoader[] |
|
235
|
|
|
*/ |
|
236
|
|
|
public function getLoaders() |
|
237
|
|
|
{ |
|
238
|
|
|
return $this->loaders; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param $class |
|
243
|
|
|
* @return bool |
|
244
|
|
|
*/ |
|
245
|
|
|
public function autoload($class) |
|
246
|
|
|
{ |
|
247
|
|
|
try { |
|
248
|
|
|
return $this->load($class); |
|
249
|
|
|
} catch (AutoloadException $ex) { |
|
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
return false; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param $class |
|
257
|
|
|
* @return bool |
|
258
|
|
|
*/ |
|
259
|
|
|
public function load($class) |
|
260
|
|
|
{ |
|
261
|
|
|
if ($this->hasIgnoreTokens($class)) { |
|
262
|
|
|
return true; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
if (class_exists($class, false)) { |
|
266
|
|
|
return true; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
$loaders = $this->getLoaders(); |
|
270
|
|
|
foreach ($loaders as $loader) { |
|
271
|
|
|
if ($loader->load($class)) { |
|
272
|
|
|
return true; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
return false; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* @param $token |
|
281
|
|
|
* @return bool |
|
282
|
|
|
*/ |
|
283
|
|
|
public function hasIgnoreTokens($token) |
|
284
|
|
|
{ |
|
285
|
|
|
return in_array($token, $this->ignoreTokens); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param $token |
|
290
|
|
|
*/ |
|
291
|
|
|
public function addIgnoreTokens($token) |
|
292
|
|
|
{ |
|
293
|
|
|
$this->ignoreTokens[] = $token; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
/** |
|
297
|
|
|
* @return array |
|
298
|
|
|
*/ |
|
299
|
|
|
public function getIgnoreTokens() |
|
300
|
|
|
{ |
|
301
|
|
|
return $this->ignoreTokens; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param array $ignoreTokens |
|
306
|
|
|
*/ |
|
307
|
|
|
public function setIgnoreTokens($ignoreTokens) |
|
308
|
|
|
{ |
|
309
|
|
|
$this->ignoreTokens = $ignoreTokens; |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: