Lines
90.90%
20 / 22
Methods
75.00%
3 / 4
Classes
0.00%
0 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| findSourcePaths | 100.00% | 4 / 4 | 100.00% | 1 / 1 | 1 | ||
| listAutoloadDirectories | 100.00% | 7 / 7 | 100.00% | 1 / 1 | 7 | ||
| readAutoloadRoots | 80.00% | 8 / 10 | 0.00% | 0 / 1 | 7.39 | ||
| 13 | final readonly class SourcePathFinder | |
| 14 | { | |
| 15 | private const string COMPOSER_FILE = 'composer.json'; | |
| 16 | ||
| 17 | // The container's environment and the daemon's own dependencies come from these, so only a restart applies them. | |
| 18 | private const array RESTART_ONLY_FILES = ['.env', 'composer.lock']; | |
| 19 | ||
| 20 | public function __construct( | |
| 21 | private ProjectRoot $projectRoot, | |
| 22 | private ConfigLoader $configs, | |
| 23 | private DaemonLaunchOptions $launch, | |
| 24 | private string $userServicesFile, | |
| 25 | ) {} | |
| 26 | ||
| 27 | public function findSourcePaths(): SourcePaths | |
| 28 | { | |
| 29 | return new SourcePaths( | |
| 30 | [...$this->listAutoloadDirectories(), $this->userServicesFile, $this->configs->locateConfigFile($this->launch->configFile)], | |
| 31 | array_map($this->projectRoot->resolvePath(...), self::RESTART_ONLY_FILES), | |
| 32 | ); | |
| 33 | } | |
| 34 | ||
| 35 | // The project's own autoload roots, without autoload-dev, so editing a test reloads nothing. | |
| 36 | /** @return list<string> */ | |
| 37 | private function listAutoloadDirectories(): array | |
| 38 | { | |
| 39 | $directories = []; | |
| 40 | ||
| 41 | foreach ($this->readAutoloadRoots() as $paths) { | |
| 42 | foreach (\is_array($paths) ? $paths : [$paths] as $path) { | |
| 43 | $directory = \is_string($path) ? rtrim($this->projectRoot->resolvePath($path), '/') : ''; | |
| 44 | ||
| 45 | if ($directory !== '' && is_dir($directory)) { | |
| 46 | $directories[] = $directory; | |
| 47 | } | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | return array_values(array_unique($directories)); | |
| 52 | } | |
| 53 | ||
| 54 | /** @return array<array-key, mixed> */ | |
| 55 | private function readAutoloadRoots(): array | |
| 56 | { | |
| 57 | $file = $this->projectRoot->resolvePath(self::COMPOSER_FILE); | |
| 58 | $json = is_file($file) ? file_get_contents($file) : false; | |
| 59 | ||
| 60 | if ($json === false) { | |
| 61 | return []; | |
| 62 | } | |
| 63 | ||
| 64 | try { | |
| 65 | $composer = JsonDecoder::decodeJson($json); | |
| 66 | } catch (JsonException) { | |
| 67 | return []; | |
| 68 | } | |
| 69 | ||
| 70 | $autoload = \is_array($composer) ? $composer['autoload'] ?? null : null; | |
| 71 | $roots = \is_array($autoload) ? $autoload['psr-4'] ?? null : null; | |
| 72 | ||
| 73 | return \is_array($roots) ? $roots : []; | |
| 74 | } | |
| 75 | } |