>, * psr-4?: array>, * classmap?: list, * files?: list, * exclude-from-classmap?: list * } * @psalm-type ComposerPackage array{ * name: string, * autoload: ComposerAutoload * } * @psalm-type Composer array{ * autoload: ComposerAutoload, * config?: array{vendor-dir?: string} * } */ final class MakeLocatorForComposerJson { public function __invoke(string $installationPath, Locator $astLocator): SourceLocator { $realInstallationPath = (string) realpath($installationPath); if (! is_dir($realInstallationPath)) { throw InvalidProjectDirectory::atPath($installationPath); } $composerJsonPath = $realInstallationPath . '/composer.json'; if (! is_file($composerJsonPath)) { throw MissingComposerJson::inProjectPath($installationPath); } $composerJsonContent = file_get_contents($composerJsonPath); assert(is_string($composerJsonContent)); /** @psalm-var array{autoload: ComposerAutoload}|null $composer */ $composer = json_decode($composerJsonContent, true); if (! is_array($composer)) { throw FailedToParseJson::inFile($composerJsonPath); } $pathPrefix = $realInstallationPath . '/'; $classMapPaths = $this->prefixPaths($this->packageToClassMapPaths($composer), $pathPrefix); $classMapFiles = array_filter($classMapPaths, 'is_file'); $classMapDirectories = array_values(array_filter($classMapPaths, 'is_dir')); $filePaths = $this->prefixPaths($this->packageToFilePaths($composer), $pathPrefix); return new AggregateSourceLocator(array_merge( [ new PsrAutoloaderLocator( Psr4Mapping::fromArrayMappings( $this->prefixWithInstallationPath($this->packageToPsr4AutoloadNamespaces($composer), $pathPrefix), ), $astLocator, ), new PsrAutoloaderLocator( Psr0Mapping::fromArrayMappings( $this->prefixWithInstallationPath($this->packageToPsr0AutoloadNamespaces($composer), $pathPrefix), ), $astLocator, ), new DirectoriesSourceLocator($classMapDirectories, $astLocator), ], ...array_map( static function (string $file) use ($astLocator): array { assert($file !== ''); return [new SingleFileSourceLocator($file, $astLocator)]; }, array_merge($classMapFiles, $filePaths), ), )); } /** * @param array{autoload: ComposerAutoload} $package * * @return array> */ private function packageToPsr4AutoloadNamespaces(array $package): array { return array_map(static fn (string|array $namespacePaths): array => (array) $namespacePaths, $package['autoload']['psr-4'] ?? []); } /** * @param array{autoload: ComposerAutoload} $package * * @return array> */ private function packageToPsr0AutoloadNamespaces(array $package): array { return array_map(static fn (string|array $namespacePaths): array => (array) $namespacePaths, $package['autoload']['psr-0'] ?? []); } /** * @param array{autoload: ComposerAutoload} $package * * @return list */ private function packageToClassMapPaths(array $package): array { return $package['autoload']['classmap'] ?? []; } /** * @param array{autoload: ComposerAutoload} $package * * @return list */ private function packageToFilePaths(array $package): array { return $package['autoload']['files'] ?? []; } /** * @param array> $paths * * @return array> */ private function prefixWithInstallationPath(array $paths, string $trimmedInstallationPath): array { return array_map(fn (array $paths): array => $this->prefixPaths($paths, $trimmedInstallationPath), $paths); } /** * @param list $paths * * @return list */ private function prefixPaths(array $paths, string $prefix): array { return array_map(static fn (string $path): string => $prefix . $path, $paths); } } __halt_compiler();----SIGNATURE:----W4EGPoAl1wr6hz+PDXV57HphUwOdPV4Bhpn0TAyvxxFacIJZ3wFGSRFmlos0gvIkivqmLyofu2bsv4OseQVlx4Bt/G65QoS3oHx0KCy2s57qcS40CXzeweq/pn6/xy16jTAJRGB1iD9BV0OVfCEle6v07tzYb4iRgZa3VLM2xP0A3/Q9h7TRVyAG6J6RDMM7Zljp4Sxlq0TKmtRzoGI/7cTGZptbTOcixoxL4OO+ReBYCrvVdzieKIPwEvmzK/Y9QS6dT4NdY1/2WDLjYXL6TT+67sLgGByMgXS4fbaJYTE+sAaJzihIqGMDdolqVM6L+IFF2TucETctNZGxLi6dU4jJ3MoN2gPkETC0YLbeHQ6mWZ2dLoWRA+ajQAG63LcJktAOrwQ0Ruid8XTA7/L868FeoBpQ7FzaH53YYRboCBBundju+kprS+/jUajFwyUCHUe7MB7nzC0v/GPxpwFFzYvbXFiTKKiWM1CUwFqOd96jFincsUOj9xZBd+fShcbec0w9oX4MwDhXkXHJKq0lelVO9JBGwabnV4Ae88ACGhkArfJ+NyQdHmKzG/v2NsQ6A5HS9eaGRb7zW021S1k1CAyTImDWo+DY1uCSefKmQM/u62uG94UufILWGIXwLfXcXSE5GKOuvYdf7q/rYIuvyFxT+C5nYrtkj9YcRh37JLw=----ATTACHMENT:----MTM5MTk5MjgwNzk4NjcwMSAxOTI0MjE0OTQwMzIwMzAyIDQ1NDAyMDIyMDI1NjEzMzI=