-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
RobotLoader.php
537 lines (432 loc) 路 12.6 KB
/
RobotLoader.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Loaders;
/**
* Nette auto loader is responsible for loading classes and interfaces.
*
* <code>
* $loader = new Nette\Loaders\RobotLoader;
* $loader->addDirectory('app');
* $loader->excludeDirectory('app/exclude');
* $loader->setTempDirectory('temp');
* $loader->register();
* </code>
*/
class RobotLoader
{
private const RetryLimit = 3;
/** @var string[] */
public array $ignoreDirs = ['.*', '*.old', '*.bak', '*.tmp', 'temp'];
/** @var string[] */
public array $acceptFiles = ['*.php'];
private bool $autoRebuild = true;
private bool $reportParseErrors = true;
/** @var string[] */
private array $scanPaths = [];
/** @var string[] */
private array $excludeDirs = [];
/** @var array<string, array{string, int}> class => [file, time] */
private array $classes = [];
private bool $cacheLoaded = false;
private bool $refreshed = false;
/** @var array<string, int> class => counter */
private array $missingClasses = [];
/** @var array<string, int> file => mtime */
private array $emptyFiles = [];
private ?string $tempDirectory = null;
private bool $needSave = false;
public function __construct()
{
if (!extension_loaded('tokenizer')) {
throw new \LogicException('PHP extension Tokenizer is not loaded.');
}
}
public function __destruct()
{
if ($this->needSave) {
$this->saveCache();
}
}
/**
* Register autoloader.
*/
public function register(bool $prepend = false): static
{
spl_autoload_register([$this, 'tryLoad'], prepend: $prepend);
return $this;
}
/**
* Handles autoloading of classes, interfaces or traits.
*/
public function tryLoad(string $type): void
{
$this->loadCache();
$missing = $this->missingClasses[$type] ?? null;
if ($missing >= self::RetryLimit) {
return;
}
[$file, $mtime] = $this->classes[$type] ?? null;
if ($this->autoRebuild) {
if (!$this->refreshed) {
if (!$file || !is_file($file)) {
$this->refreshClasses();
[$file] = $this->classes[$type] ?? null;
$this->needSave = true;
} elseif (filemtime($file) !== $mtime) {
$this->updateFile($file);
[$file] = $this->classes[$type] ?? null;
$this->needSave = true;
}
}
if (!$file || !is_file($file)) {
$this->missingClasses[$type] = ++$missing;
$this->needSave = $this->needSave || $file || ($missing <= self::RetryLimit);
unset($this->classes[$type]);
$file = null;
}
}
if ($file) {
(static function ($file) { require $file; })($file);
}
}
/**
* Add path or paths to list.
*/
public function addDirectory(string ...$paths): static
{
$this->scanPaths = array_merge($this->scanPaths, $paths);
return $this;
}
public function reportParseErrors(bool $on = true): static
{
$this->reportParseErrors = $on;
return $this;
}
/**
* Excludes path or paths from list.
*/
public function excludeDirectory(string ...$paths): static
{
$this->excludeDirs = array_merge($this->excludeDirs, $paths);
return $this;
}
/**
* @return array<string, string> class => filename
*/
public function getIndexedClasses(): array
{
$this->loadCache();
$res = [];
foreach ($this->classes as $class => [$file]) {
$res[$class] = $file;
}
return $res;
}
/**
* Rebuilds class list cache.
*/
public function rebuild(): void
{
$this->cacheLoaded = true;
$this->classes = $this->missingClasses = $this->emptyFiles = [];
$this->refreshClasses();
if ($this->tempDirectory) {
$this->saveCache();
}
}
/**
* Refreshes class list cache.
*/
public function refresh(): void
{
$this->loadCache();
if (!$this->refreshed) {
$this->refreshClasses();
$this->saveCache();
}
}
/**
* Refreshes $this->classes & $this->emptyFiles.
*/
private function refreshClasses(): void
{
$this->refreshed = true; // prevents calling refreshClasses() or updateFile() in tryLoad()
$files = $this->emptyFiles;
$classes = [];
foreach ($this->classes as $class => [$file, $mtime]) {
$files[$file] = $mtime;
$classes[$file][] = $class;
}
$this->classes = $this->emptyFiles = [];
foreach ($this->scanPaths as $path) {
$iterator = is_file($path)
? [$path]
: $this->createFileIterator($path);
foreach ($iterator as $file) {
$mtime = filemtime($file);
$foundClasses = isset($files[$file]) && $files[$file] === $mtime
? ($classes[$file] ?? [])
: $this->scanPhp($file);
if (!$foundClasses) {
$this->emptyFiles[$file] = $mtime;
}
$files[$file] = $mtime;
$classes[$file] = []; // prevents the error when adding the same file twice
foreach ($foundClasses as $class) {
if (isset($this->classes[$class])) {
throw new \RuntimeException(sprintf(
'Ambiguous class %s resolution; defined in %s and in %s.',
$class,
$this->classes[$class][0],
$file,
));
}
$this->classes[$class] = [$file, $mtime];
unset($this->missingClasses[$class]);
}
}
}
}
/**
* Creates an iterator scanning directory for PHP files and subdirectories.
* @throws \RuntimeException if path is not found
*/
private function createFileIterator(string $dir): \Generator
{
if (!is_dir($dir)) {
throw new \RuntimeException(sprintf("Directory '%s' not found.", $dir));
}
$dir = realpath($dir) ?: $dir; // realpath does not work in phar
$disallow = [];
foreach (array_merge($this->ignoreDirs, $this->excludeDirs) as $item) {
if ($item = realpath($item)) {
$disallow[$item] = true;
}
}
yield from $this->traverseDir($dir, $disallow);
}
private function traverseDir(string $dir, array $disallow): \Generator
{
try {
$files = new \FilesystemIterator($dir, \FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::CURRENT_AS_PATHNAME | \FilesystemIterator::UNIX_PATHS);
} catch (\RuntimeException) {
return;
}
foreach ($files as $file) {
$realPath = realpath($file);
$file = $realPath ?: $file;
if ($realPath && isset($disallow[$realPath])) {
continue;
} elseif (is_dir($file) && !self::matches(basename($file), $this->ignoreDirs)) {
yield from $this->traverseDir($file, $disallow);
} elseif (is_file($file) && self::matches(basename($file), $this->acceptFiles)) {
yield $file;
}
}
}
private static function matches(string $file, array $masks): bool
{
foreach ($masks as $mask) {
if (fnmatch($mask, $file)) {
return true;
}
}
return false;
}
private function updateFile(string $file): void
{
foreach ($this->classes as $class => [$prevFile]) {
if ($file === $prevFile) {
unset($this->classes[$class]);
}
}
$foundClasses = is_file($file) ? $this->scanPhp($file) : [];
foreach ($foundClasses as $class) {
[$prevFile, $prevMtime] = $this->classes[$class] ?? null;
if (isset($prevFile) && @filemtime($prevFile) !== $prevMtime) { // @ file may not exist
$this->updateFile($prevFile);
[$prevFile] = $this->classes[$class] ?? null;
}
if (isset($prevFile)) {
throw new \RuntimeException(sprintf(
'Ambiguous class %s resolution; defined in %s and in %s.',
$class,
$prevFile,
$file,
));
}
$this->classes[$class] = [$file, filemtime($file)];
}
}
/**
* Searches classes, interfaces and traits in PHP file.
* @return string[]
*/
private function scanPhp(string $file): array
{
$code = file_get_contents($file);
$expected = false;
$namespace = $name = '';
$level = $minLevel = 0;
$classes = [];
try {
$tokens = \PhpToken::tokenize($code, TOKEN_PARSE);
} catch (\ParseError $e) {
if ($this->reportParseErrors) {
$rp = new \ReflectionProperty($e, 'file');
$rp->setAccessible(true);
$rp->setValue($e, $file);
throw $e;
}
$tokens = [];
}
foreach ($tokens as $token) {
switch ($token->id) {
case T_COMMENT:
case T_DOC_COMMENT:
case T_WHITESPACE:
continue 2;
case T_STRING:
case T_NAME_QUALIFIED:
if ($expected) {
$name .= $token->text;
}
continue 2;
case T_NAMESPACE:
case T_CLASS:
case T_INTERFACE:
case T_TRAIT:
case PHP_VERSION_ID < 80100
? T_CLASS
: T_ENUM:
$expected = $token->id;
$name = '';
continue 2;
}
if ($expected) {
if ($expected === T_NAMESPACE) {
$namespace = $name ? $name . '\\' : '';
$minLevel = $token->text === '{' ? 1 : 0;
} elseif ($name && $level === $minLevel) {
$classes[] = $namespace . $name;
}
$expected = null;
}
if ($token->text === '{') {
$level++;
} elseif ($token->text === '}') {
$level--;
}
}
return $classes;
}
/********************* caching ****************d*g**/
/**
* Sets auto-refresh mode.
*/
public function setAutoRefresh(bool $on = true): static
{
$this->autoRebuild = $on;
return $this;
}
/**
* Sets path to temporary directory.
*/
public function setTempDirectory(string $dir): static
{
if (!is_dir($dir) && !@mkdir($dir, recursive: true) && !is_dir($dir)) { // @ - dir may already exist
throw new \RuntimeException("Unable to create directory '$dir'");
}
$this->tempDirectory = $dir;
return $this;
}
/**
* Loads class list from cache.
*/
private function loadCache(): void
{
if ($this->cacheLoaded) {
return;
}
$this->cacheLoaded = true;
$file = $this->generateCacheFileName();
// Solving atomicity to work everywhere is really pain in the ass.
// 1) We want to do as little as possible IO calls on production and also directory and file can be not writable (#19)
// so on Linux we include the file directly without shared lock, therefore, the file must be created atomically by renaming.
// 2) On Windows file cannot be renamed-to while is open (ie by include() #11), so we have to acquire a lock.
$lock = defined('PHP_WINDOWS_VERSION_BUILD')
? $this->acquireLock("$file.lock", LOCK_SH)
: null;
$data = @include $file; // @ file may not exist
if (is_array($data)) {
[$this->classes, $this->missingClasses, $this->emptyFiles] = $data;
return;
}
if ($lock) {
flock($lock, LOCK_UN); // release shared lock so we can get exclusive
}
$lock = $this->acquireLock("$file.lock", LOCK_EX);
// while waiting for exclusive lock, someone might have already created the cache
$data = @include $file; // @ file may not exist
if (is_array($data)) {
[$this->classes, $this->missingClasses, $this->emptyFiles] = $data;
return;
}
$this->classes = $this->missingClasses = $this->emptyFiles = [];
$this->refreshClasses();
$this->saveCache($lock);
// On Windows concurrent creation and deletion of a file can cause a 'permission denied' error,
// therefore, we will not delete the lock file. Windows is really annoying.
}
/**
* Writes class list to cache.
* @param resource $lock
*/
private function saveCache($lock = null): void
{
// we have to acquire a lock to be able safely rename file
// on Linux: that another thread does not rename the same named file earlier
// on Windows: that the file is not read by another thread
$file = $this->generateCacheFileName();
$lock = $lock ?: $this->acquireLock("$file.lock", LOCK_EX);
$code = "<?php\nreturn " . var_export([$this->classes, $this->missingClasses, $this->emptyFiles], true) . ";\n";
if (file_put_contents("$file.tmp", $code) !== strlen($code) || !rename("$file.tmp", $file)) {
@unlink("$file.tmp"); // @ file may not exist
throw new \RuntimeException(sprintf("Unable to create '%s'.", $file));
}
if (function_exists('opcache_invalidate')) {
@opcache_invalidate($file, force: true); // @ can be restricted
}
}
/** @return resource */
private function acquireLock(string $file, int $mode)
{
$handle = @fopen($file, 'w'); // @ is escalated to exception
if (!$handle) {
throw new \RuntimeException(sprintf("Unable to create file '%s'. %s", $file, error_get_last()['message']));
} elseif (!@flock($handle, $mode)) { // @ is escalated to exception
throw new \RuntimeException(sprintf(
"Unable to acquire %s lock on file '%s'. %s",
$mode & LOCK_EX ? 'exclusive' : 'shared',
$file,
error_get_last()['message'],
));
}
return $handle;
}
private function generateCacheFileName(): string
{
if (!$this->tempDirectory) {
throw new \LogicException('Set path to temporary directory using setTempDirectory().');
}
return $this->tempDirectory . '/' . md5(serialize($this->generateCacheKey())) . '.php';
}
protected function generateCacheKey(): array
{
return [$this->ignoreDirs, $this->acceptFiles, $this->scanPaths, $this->excludeDirs, 'v2'];
}
}