Skip to content

Commit 4cf0141

Browse files
committed
Look at install-path from installed.json available in Composer v2
1 parent 00b0284 commit 4cf0141

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

src/Reflection/BetterReflection/SourceLocator/ComposerJsonAndInstalledJsonSourceLocatorMaker.php

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,19 @@ public function __construct(
2929
$this->optimizedPsrAutoloaderLocatorFactory = $optimizedPsrAutoloaderLocatorFactory;
3030
}
3131

32-
public function create(string $installationPath): ?SourceLocator
32+
public function create(string $projectInstallationPath): ?SourceLocator
3333
{
34-
$composerJsonPath = $installationPath . '/composer.json';
34+
$composerJsonPath = $projectInstallationPath . '/composer.json';
3535
if (!is_file($composerJsonPath)) {
3636
return null;
3737
}
38-
$installedJsonPath = $installationPath . '/vendor/composer/installed.json';
38+
$installedJsonPath = $projectInstallationPath . '/vendor/composer/installed.json';
3939
if (!is_file($installedJsonPath)) {
4040
return null;
4141
}
4242

43+
$installedJsonDirectoryPath = dirname($installedJsonPath);
44+
4345
try {
4446
$composerJsonContents = FileReader::read($composerJsonPath);
4547
$composer = Json::decode($composerJsonContents, Json::FORCE_ARRAY);
@@ -57,34 +59,35 @@ public function create(string $installationPath): ?SourceLocator
5759
$installed = $installedJson['packages'] ?? $installedJson;
5860

5961
$classMapPaths = array_merge(
60-
$this->prefixPaths($this->packageToClassMapPaths($composer), $installationPath . '/'),
61-
...array_map(function (array $package) use ($installationPath): array {
62+
$this->prefixPaths($this->packageToClassMapPaths($composer), $projectInstallationPath . '/'),
63+
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
6264
return $this->prefixPaths(
6365
$this->packageToClassMapPaths($package),
64-
$this->packagePrefixPath($installationPath, $package)
66+
$this->packagePrefixPath($projectInstallationPath, $installedJsonDirectoryPath, $package)
6567
);
6668
}, $installed)
6769
);
6870
$classMapFiles = array_filter($classMapPaths, 'is_file');
6971
$classMapDirectories = array_filter($classMapPaths, 'is_dir');
7072
$filePaths = array_merge(
71-
$this->prefixPaths($this->packageToFilePaths($composer), $installationPath . '/'),
72-
...array_map(function (array $package) use ($installationPath): array {
73+
$this->prefixPaths($this->packageToFilePaths($composer), $projectInstallationPath . '/'),
74+
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
7375
return $this->prefixPaths(
7476
$this->packageToFilePaths($package),
75-
$this->packagePrefixPath($installationPath, $package)
77+
$this->packagePrefixPath($projectInstallationPath, $installedJsonDirectoryPath, $package)
7678
);
7779
}, $installed)
7880
);
7981

8082
$locators = [];
8183
$locators[] = $this->optimizedPsrAutoloaderLocatorFactory->create(
8284
Psr4Mapping::fromArrayMappings(array_merge_recursive(
83-
$this->prefixWithInstallationPath($this->packageToPsr4AutoloadNamespaces($composer), $installationPath),
84-
...array_map(function (array $package) use ($installationPath): array {
85+
$this->prefixWithInstallationPath($this->packageToPsr4AutoloadNamespaces($composer), $projectInstallationPath),
86+
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
8587
return $this->prefixWithPackagePath(
8688
$this->packageToPsr4AutoloadNamespaces($package),
87-
$installationPath,
89+
$projectInstallationPath,
90+
$installedJsonDirectoryPath,
8891
$package
8992
);
9093
}, $installed)
@@ -93,11 +96,12 @@ public function create(string $installationPath): ?SourceLocator
9396

9497
$locators[] = $this->optimizedPsrAutoloaderLocatorFactory->create(
9598
Psr0Mapping::fromArrayMappings(array_merge_recursive(
96-
$this->prefixWithInstallationPath($this->packageToPsr0AutoloadNamespaces($composer), $installationPath),
97-
...array_map(function (array $package) use ($installationPath): array {
99+
$this->prefixWithInstallationPath($this->packageToPsr0AutoloadNamespaces($composer), $projectInstallationPath),
100+
...array_map(function (array $package) use ($projectInstallationPath, $installedJsonDirectoryPath): array {
98101
return $this->prefixWithPackagePath(
99102
$this->packageToPsr0AutoloadNamespaces($package),
100-
$installationPath,
103+
$projectInstallationPath,
104+
$installedJsonDirectoryPath,
101105
$package
102106
);
103107
}, $installed)
@@ -162,9 +166,17 @@ private function packageToFilePaths(array $package): array
162166
/**
163167
* @param mixed[] $package
164168
*/
165-
private function packagePrefixPath(string $trimmedInstallationPath, array $package): string
169+
private function packagePrefixPath(
170+
string $projectInstallationPath,
171+
string $installedJsonDirectoryPath,
172+
array $package
173+
): string
166174
{
167-
return $trimmedInstallationPath . '/vendor/' . $package['name'] . '/';
175+
if (array_key_exists('install-path', $package)) {
176+
return $installedJsonDirectoryPath . '/' . $package['install-path'] . '/';
177+
}
178+
179+
return $projectInstallationPath . '/vendor/' . $package['name'] . '/';
168180
}
169181

170182
/**
@@ -173,9 +185,9 @@ private function packagePrefixPath(string $trimmedInstallationPath, array $packa
173185
*
174186
* @return array<string, array<int, string>>
175187
*/
176-
private function prefixWithPackagePath(array $paths, string $trimmedInstallationPath, array $package): array
188+
private function prefixWithPackagePath(array $paths, string $projectInstallationPath, string $installedJsonDirectoryPath, array $package): array
177189
{
178-
$prefix = $this->packagePrefixPath($trimmedInstallationPath, $package);
190+
$prefix = $this->packagePrefixPath($projectInstallationPath, $installedJsonDirectoryPath, $package);
179191

180192
return array_map(function (array $paths) use ($prefix): array {
181193
return $this->prefixPaths($paths, $prefix);

0 commit comments

Comments
 (0)