1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\SourceLocator\Type\Composer\Psr; |
6: | |
7: | use PHPStan\BetterReflection\Identifier\Identifier; |
8: | |
9: | use function array_map; |
10: | use function array_merge; |
11: | use function array_unique; |
12: | use function array_values; |
13: | use function rtrim; |
14: | use function str_replace; |
15: | use function strpos; |
16: | |
17: | final class Psr0Mapping implements PsrAutoloaderMapping |
18: | { |
19: | |
20: | private $mappings = []; |
21: | |
22: | private function __construct() |
23: | { |
24: | } |
25: | |
26: | |
27: | public static function fromArrayMappings(array $mappings): self |
28: | { |
29: | $instance = new self(); |
30: | |
31: | $instance->mappings = array_map(static function (array $directories): array { |
32: | return array_map(static function (string $directory) : string { |
33: | return rtrim($directory, '/'); |
34: | }, $directories); |
35: | }, $mappings); |
36: | |
37: | return $instance; |
38: | } |
39: | |
40: | |
41: | public function resolvePossibleFilePaths(Identifier $identifier): array |
42: | { |
43: | if (! $identifier->isClass()) { |
44: | return []; |
45: | } |
46: | |
47: | $className = $identifier->getName(); |
48: | |
49: | foreach ($this->mappings as $prefix => $paths) { |
50: | if ($prefix === '') { |
51: | continue; |
52: | } |
53: | |
54: | if (strpos($className, $prefix) === 0) { |
55: | return array_map(static function (string $path) use ($className) : string { |
56: | return $path . '/' . str_replace(['\\', '_'], '/', $className) . '.php'; |
57: | }, $paths); |
58: | } |
59: | } |
60: | |
61: | return []; |
62: | } |
63: | |
64: | |
65: | public function directories(): array |
66: | { |
67: | return array_values(array_unique(array_merge([], ...array_values($this->mappings)))); |
68: | } |
69: | } |
70: | |