Lines
0.00%
0 / 10
Functions and Methods
0.00%
0 / 3
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Psr4Map | 0.00% | 0 / 10 | 0.00% | 0 / 3 | 110 | 0.00% | 0 / 1 | |||
| __construct | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| listDirectoriesFor | 0.00% | 0 / 1 | 0.00% | 0 / 1 | 2 | |||||
| findAutoloadRootContaining | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 72 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Config; | |
| 6 | ||
| 7 | final readonly class Psr4Map | |
| 8 | { | |
| 9 | /** @param array<string, array<int, string>> $directoriesByNamespace */ | |
| 10 | public function __construct(private array $directoriesByNamespace) {} | |
| 11 | ||
| 12 | /** @return list<string> */ | |
| 13 | public function listDirectoriesFor(string $namespacePrefix): array | |
| 14 | { | |
| 15 | return array_values($this->directoriesByNamespace[$namespacePrefix] ?? []); | |
| 16 | } | |
| 17 | ||
| 18 | // The deepest mapped directory wins, as Composer resolves the most specific prefix. | |
| 19 | public function findAutoloadRootContaining(string $directory): ?AutoloadRoot | |
| 20 | { | |
| 21 | $found = null; | |
| 22 | ||
| 23 | foreach ($this->directoriesByNamespace as $namespacePrefix => $directories) { | |
| 24 | foreach ($directories as $mapped) { | |
| 25 | $candidate = realpath($mapped); | |
| 26 | ||
| 27 | if ($candidate === false || ($candidate !== $directory && !str_starts_with($directory, $candidate . \DIRECTORY_SEPARATOR))) { | |
| 28 | continue; | |
| 29 | } | |
| 30 | ||
| 31 | if ($found === null || \strlen($candidate) > \strlen($found->directory)) { | |
| 32 | $found = new AutoloadRoot($namespacePrefix, $candidate); | |
| 33 | } | |
| 34 | } | |
| 35 | } | |
| 36 | ||
| 37 | return $found; | |
| 38 | } | |
| 39 | } |