|
10 | 10 |
|
11 | 11 | namespace OpenCodeModeling\CodeAst\Package; |
12 | 12 |
|
13 | | -use Composer\Autoload\ClassLoader; |
14 | | - |
15 | 13 | final class Psr4Info implements ClassInfo |
16 | 14 | { |
17 | 15 | /** |
@@ -172,30 +170,67 @@ private function normalizePath(string $path): string |
172 | 170 | /** |
173 | 171 | * Creates an instance of the class Psr4Info based on the Composer configuration. |
174 | 172 | * |
175 | | - * @param ClassLoader $classLoader Composer ClassLoader instance |
| 173 | + * @param string $basePath Usually the composer.json root folder |
| 174 | + * @param string $composerFileContent Content of composer.json to detect registered namespaces |
176 | 175 | * @param callable $filterDirectoryToNamespace Callable to filter a directory to a namespace |
177 | | - * @param callable $filterNamespaceToDirectory Callable to filter a namespace to a directory |
| 176 | + * @param callable $filterNamespaceToDirectory Callable to filter a namespace to a directory |
178 | 177 | * @param string $exclude Specifies which path should be ignored |
179 | | - * @return array |
| 178 | + * @return Psr4Info[] |
| 179 | + * @throws \JsonException |
180 | 180 | */ |
181 | 181 | public static function fromComposer( |
182 | | - ClassLoader $classLoader, |
| 182 | + string $basePath, |
| 183 | + string $composerFileContent, |
183 | 184 | callable $filterDirectoryToNamespace, |
184 | 185 | callable $filterNamespaceToDirectory, |
185 | 186 | string $exclude = 'vendor' . DIRECTORY_SEPARATOR |
186 | 187 | ): array { |
187 | 188 | $namespaces = []; |
188 | 189 |
|
189 | | - foreach ($classLoader->getPrefixesPsr4() as $namespace => $paths) { |
190 | | - $realpath = \preg_replace('/^' . \addcslashes(\getcwd(), '/') . '\//', '', \realpath($paths[0])); |
191 | | - if (false !== \stripos($realpath, $exclude)) { |
| 190 | + $composer = \json_decode($composerFileContent, true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); |
| 191 | + |
| 192 | + foreach ($composer['autoload']['psr-4'] ?? [] as $namespace => $paths) { |
| 193 | + $namespaces[] = self::fromNamespace($basePath, $namespace, (array) $paths, $filterDirectoryToNamespace, $filterNamespaceToDirectory, $exclude); |
| 194 | + } |
| 195 | + foreach ($composer['autoload-dev']['psr-4'] ?? [] as $namespace => $paths) { |
| 196 | + $namespaces[] = self::fromNamespace($basePath, $namespace, (array) $paths, $filterDirectoryToNamespace, $filterNamespaceToDirectory, $exclude); |
| 197 | + } |
| 198 | + |
| 199 | + return \array_merge(...$namespaces); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * @param string $basePath Usually the composer.json root folder |
| 204 | + * @param string $namespace |
| 205 | + * @param array $paths |
| 206 | + * @param callable $filterDirectoryToNamespace |
| 207 | + * @param callable $filterNamespaceToDirectory |
| 208 | + * @param string $exclude |
| 209 | + * @return Psr4Info[] |
| 210 | + */ |
| 211 | + public static function fromNamespace( |
| 212 | + string $basePath, |
| 213 | + string $namespace, |
| 214 | + array $paths, |
| 215 | + callable $filterDirectoryToNamespace, |
| 216 | + callable $filterNamespaceToDirectory, |
| 217 | + string $exclude = 'vendor' . DIRECTORY_SEPARATOR |
| 218 | + ): array { |
| 219 | + $classInfoList = []; |
| 220 | + |
| 221 | + $basePath = \rtrim($basePath, '/') . DIRECTORY_SEPARATOR; |
| 222 | + |
| 223 | + foreach ($paths as $path) { |
| 224 | + $path = $basePath . $path; |
| 225 | + |
| 226 | + if (false !== \stripos($path, $exclude)) { |
192 | 227 | continue; |
193 | 228 | } |
194 | | - $classInfo = new self($realpath, $namespace, $filterDirectoryToNamespace, $filterNamespaceToDirectory); |
| 229 | + $classInfo = new self($path, $namespace, $filterDirectoryToNamespace, $filterNamespaceToDirectory); |
195 | 230 |
|
196 | | - $namespaces[] = $classInfo; |
| 231 | + $classInfoList[] = $classInfo; |
197 | 232 | } |
198 | 233 |
|
199 | | - return $namespaces; |
| 234 | + return $classInfoList; |
200 | 235 | } |
201 | 236 | } |
0 commit comments