1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\SourceLocator\Type\Composer\Factory; |
6: | |
7: | use PHPStan\BetterReflection\SourceLocator\Ast\Locator; |
8: | use PHPStan\BetterReflection\SourceLocator\Type\AggregateSourceLocator; |
9: | use PHPStan\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\FailedToParseJson; |
10: | use PHPStan\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\InvalidProjectDirectory; |
11: | use PHPStan\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingComposerJson; |
12: | use PHPStan\BetterReflection\SourceLocator\Type\Composer\Factory\Exception\MissingInstalledJson; |
13: | use PHPStan\BetterReflection\SourceLocator\Type\Composer\Psr\Psr0Mapping; |
14: | use PHPStan\BetterReflection\SourceLocator\Type\Composer\Psr\Psr4Mapping; |
15: | use PHPStan\BetterReflection\SourceLocator\Type\Composer\PsrAutoloaderLocator; |
16: | use PHPStan\BetterReflection\SourceLocator\Type\DirectoriesSourceLocator; |
17: | use PHPStan\BetterReflection\SourceLocator\Type\SingleFileSourceLocator; |
18: | use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator; |
19: | |
20: | use function array_filter; |
21: | use function array_map; |
22: | use function array_merge; |
23: | use function array_merge_recursive; |
24: | use function array_values; |
25: | use function assert; |
26: | use function file_get_contents; |
27: | use function is_array; |
28: | use function is_dir; |
29: | use function is_file; |
30: | use function is_string; |
31: | use function json_decode; |
32: | use function realpath; |
33: | use function rtrim; |
34: | |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | final class MakeLocatorForInstalledJson |
41: | { |
42: | public function __invoke(string $installationPath, Locator $astLocator): SourceLocator |
43: | { |
44: | $realInstallationPath = (string) realpath($installationPath); |
45: | |
46: | if (! is_dir($realInstallationPath)) { |
47: | throw InvalidProjectDirectory::atPath($installationPath); |
48: | } |
49: | |
50: | $composerJsonPath = $realInstallationPath . '/composer.json'; |
51: | |
52: | if (! is_file($composerJsonPath)) { |
53: | throw MissingComposerJson::inProjectPath($installationPath); |
54: | } |
55: | |
56: | $composerJsonContent = file_get_contents($composerJsonPath); |
57: | assert(is_string($composerJsonContent)); |
58: | |
59: | |
60: | $composer = json_decode($composerJsonContent, true); |
61: | $vendorDir = $composer['config']['vendor-dir'] ?? 'vendor'; |
62: | $vendorDir = rtrim($vendorDir, '/'); |
63: | |
64: | $installedJsonPath = $realInstallationPath . '/' . $vendorDir . '/composer/installed.json'; |
65: | |
66: | if (! is_file($installedJsonPath)) { |
67: | throw MissingInstalledJson::inProjectPath($realInstallationPath . '/' . $vendorDir); |
68: | } |
69: | |
70: | $jsonContent = file_get_contents($installedJsonPath); |
71: | assert(is_string($jsonContent)); |
72: | |
73: | |
74: | $installedJson = json_decode($jsonContent, true); |
75: | |
76: | if (! is_array($installedJson)) { |
77: | throw FailedToParseJson::inFile($installedJsonPath); |
78: | } |
79: | |
80: | |
81: | $installed = $installedJson['packages'] ?? $installedJson; |
82: | |
83: | $classMapPaths = array_merge([], ...array_map(function (array $package) use ($realInstallationPath, $vendorDir) : array { |
84: | return $this->prefixPaths($this->packageToClassMapPaths($package), $this->packagePrefixPath($realInstallationPath, $package, $vendorDir)); |
85: | }, $installed)); |
86: | $classMapFiles = array_filter($classMapPaths, 'is_file'); |
87: | $classMapDirectories = array_values(array_filter($classMapPaths, 'is_dir')); |
88: | $filePaths = array_merge([], ...array_map(function (array $package) use ($realInstallationPath, $vendorDir) : array { |
89: | return $this->prefixPaths($this->packageToFilePaths($package), $this->packagePrefixPath($realInstallationPath, $package, $vendorDir)); |
90: | }, $installed)); |
91: | |
92: | return new AggregateSourceLocator(array_merge([ |
93: | new PsrAutoloaderLocator(Psr4Mapping::fromArrayMappings(array_merge_recursive([], ...array_map(function (array $package) use ($realInstallationPath, $vendorDir) : array { |
94: | return $this->prefixWithPackagePath($this->packageToPsr4AutoloadNamespaces($package), $realInstallationPath, $package, $vendorDir); |
95: | }, $installed))), $astLocator), |
96: | new PsrAutoloaderLocator(Psr0Mapping::fromArrayMappings(array_merge_recursive([], ...array_map(function (array $package) use ($realInstallationPath, $vendorDir) : array { |
97: | return $this->prefixWithPackagePath($this->packageToPsr0AutoloadNamespaces($package), $realInstallationPath, $package, $vendorDir); |
98: | }, $installed))), $astLocator), |
99: | new DirectoriesSourceLocator($classMapDirectories, $astLocator), |
100: | ], ...array_map(static function (string $file) use ($astLocator): array { |
101: | assert($file !== ''); |
102: | |
103: | return [new SingleFileSourceLocator($file, $astLocator)]; |
104: | }, array_merge($classMapFiles, $filePaths)))); |
105: | } |
106: | |
107: | |
108: | |
109: | |
110: | |
111: | |
112: | private function packageToPsr4AutoloadNamespaces(array $package): array |
113: | { |
114: | return array_map(static function ($namespacePaths) : array { |
115: | return (array) $namespacePaths; |
116: | }, $package['autoload']['psr-4'] ?? []); |
117: | } |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | private function packageToPsr0AutoloadNamespaces(array $package): array |
125: | { |
126: | return array_map(static function ($namespacePaths) : array { |
127: | return (array) $namespacePaths; |
128: | }, $package['autoload']['psr-0'] ?? []); |
129: | } |
130: | |
131: | |
132: | |
133: | |
134: | |
135: | |
136: | private function packageToClassMapPaths(array $package): array |
137: | { |
138: | return $package['autoload']['classmap'] ?? []; |
139: | } |
140: | |
141: | |
142: | |
143: | |
144: | |
145: | |
146: | private function packageToFilePaths(array $package): array |
147: | { |
148: | return $package['autoload']['files'] ?? []; |
149: | } |
150: | |
151: | |
152: | private function packagePrefixPath(string $trimmedInstallationPath, array $package, string $vendorDir): string |
153: | { |
154: | return $trimmedInstallationPath . '/' . $vendorDir . '/' . $package['name'] . '/'; |
155: | } |
156: | |
157: | |
158: | |
159: | |
160: | |
161: | |
162: | |
163: | private function prefixWithPackagePath(array $paths, string $trimmedInstallationPath, array $package, string $vendorDir): array |
164: | { |
165: | $prefix = $this->packagePrefixPath($trimmedInstallationPath, $package, $vendorDir); |
166: | |
167: | return array_map(function (array $paths) use ($prefix) : array { |
168: | return $this->prefixPaths($paths, $prefix); |
169: | }, $paths); |
170: | } |
171: | |
172: | |
173: | |
174: | |
175: | |
176: | |
177: | private function prefixPaths(array $paths, string $prefix): array |
178: | { |
179: | return array_map(static function (string $path) use ($prefix) : string { |
180: | return $prefix . $path; |
181: | }, $paths); |
182: | } |
183: | } |
184: | |