1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Metadata\Driver; |
6
|
|
|
|
7
|
|
|
class FileLocator implements AdvancedFileLocatorInterface, TraceableFileLocatorInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string[] |
11
|
|
|
*/ |
12
|
|
|
private $dirs; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @param string[] $dirs |
16
|
|
|
*/ |
17
|
4 |
|
public function __construct(array $dirs) |
18
|
|
|
{ |
19
|
4 |
|
$this->dirs = $dirs; |
20
|
4 |
|
} |
21
|
|
|
|
22
|
3 |
|
/** |
23
|
|
|
* @return array<string, bool> |
24
|
3 |
|
*/ |
25
|
3 |
|
public function getPossibleFilesForClass(\ReflectionClass $class, string $extension): array |
26
|
1 |
|
{ |
27
|
|
|
$possibleFiles = []; |
28
|
|
|
foreach ($this->dirs as $prefix => $dir) { |
29
|
3 |
|
if ('' !== $prefix && 0 !== strpos($class->getNamespaceName(), $prefix)) { |
30
|
3 |
|
continue; |
31
|
3 |
|
} |
32
|
3 |
|
|
33
|
|
|
$len = '' === $prefix ? 0 : strlen($prefix) + 1; |
34
|
|
|
$path = $dir . '/' . str_replace('\\', '.', substr($class->name, $len)) . '.' . $extension; |
35
|
|
|
$existsPath = file_exists($path); |
36
|
1 |
|
$possibleFiles[$path] = $existsPath; |
37
|
|
|
if ($existsPath) { |
38
|
|
|
return $possibleFiles; |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
return $possibleFiles; |
43
|
|
|
} |
44
|
1 |
|
|
45
|
1 |
|
public function findFileForClass(\ReflectionClass $class, string $extension): ?string |
46
|
|
|
{ |
47
|
1 |
|
foreach ($this->getPossibleFilesForClass($class, $extension) as $path => $existsPath) { |
48
|
1 |
|
if ($existsPath) { |
49
|
1 |
|
return $path; |
50
|
|
|
} |
51
|
1 |
|
} |
52
|
1 |
|
|
53
|
1 |
|
return null; |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
1 |
|
* {@inheritDoc} |
58
|
|
|
*/ |
59
|
|
|
public function findAllClasses(string $extension): array |
60
|
|
|
{ |
61
|
1 |
|
$classes = []; |
62
|
|
|
foreach ($this->dirs as $prefix => $dir) { |
63
|
|
|
/** @var \RecursiveIteratorIterator|\SplFileInfo[] $iterator */ |
64
|
|
|
$iterator = new \RecursiveIteratorIterator( |
65
|
|
|
new \RecursiveDirectoryIterator($dir), |
66
|
|
|
\RecursiveIteratorIterator::LEAVES_ONLY |
67
|
|
|
); |
68
|
|
|
$nsPrefix = '' !== $prefix ? $prefix . '\\' : ''; |
69
|
|
|
foreach ($iterator as $file) { |
70
|
|
|
if (($fileName = $file->getBasename('.' . $extension)) === $file->getBasename()) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$classes[] = $nsPrefix . str_replace('.', '\\', $fileName); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $classes; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|