Lines
100.00%
44 / 44
Methods
100.00%
4 / 4
Classes
100.00%
1 / 1
| Name | Lines | Methods | CRAP | ||||
|---|---|---|---|---|---|---|---|
| __construct | 100.00% | 1 / 1 | 100.00% | 1 / 1 | 1 | ||
| discover | 100.00% | 38 / 38 | 100.00% | 1 / 1 | 11 | ||
| findClassForFile | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 4 | ||
| validateAppId | 100.00% | 2 / 2 | 100.00% | 1 / 1 | 1 | ||
| 21 | final readonly class AppDiscovery | |
| 22 | { | |
| 23 | public function __construct( | |
| 24 | private Psr4Map $psr4Map, | |
| 25 | private string $appsDirectory, | |
| 26 | ) {} | |
| 27 | ||
| 28 | /** @throws AppException */ | |
| 29 | public function discover(): DiscoveryResult | |
| 30 | { | |
| 31 | $directory = realpath($this->appsDirectory); | |
| 32 | ||
| 33 | if ($directory === false || !is_dir($directory)) { | |
| 34 | return DiscoveryResult::createEmpty(); | |
| 35 | } | |
| 36 | ||
| 37 | $root = $this->psr4Map->findAutoloadRootContaining($directory) ?? throw AppException::createForReason( | |
| 38 | AppError::DirectoryNotAutoloadable, | |
| 39 | directory: $directory, | |
| 40 | ); | |
| 41 | $apps = []; | |
| 42 | $templates = []; | |
| 43 | $unmarked = []; | |
| 44 | $unloadable = []; | |
| 45 | $classesByKey = []; | |
| 46 | ||
| 47 | foreach (Finder::create()->files()->in($directory)->name('*.php')->sortByName() as $file) { | |
| 48 | try { | |
| 49 | $class = $this->findClassForFile($root, $file); | |
| 50 | } catch (Throwable $e) { | |
| 51 | $unloadable[] = new UnloadableAppFile($file->getRealPath() ?: $file->getPathname(), $e); | |
| 52 | ||
| 53 | continue; | |
| 54 | } | |
| 55 | ||
| 56 | if ($class === null) { | |
| 57 | continue; | |
| 58 | } | |
| 59 | ||
| 60 | $automation = $class->findAutomation(); | |
| 61 | ||
| 62 | if ($automation === null) { | |
| 63 | if ($class->isTemplate()) { | |
| 64 | $class->requireRunnableApp('#[AppTemplate]'); | |
| 65 | $templates[] = $class->getName(); | |
| 66 | } elseif ($class->isRunnableApp()) { | |
| 67 | $unmarked[] = new UnmarkedApp($class->getName()); | |
| 68 | } | |
| 69 | ||
| 70 | continue; | |
| 71 | } | |
| 72 | ||
| 73 | $app = new DiscoveredApp($class->getName(), $this->validateAppId($class, $automation)); | |
| 74 | $key = $app->id->toEnvironmentKey(); | |
| 75 | ||
| 76 | if (isset($classesByKey[$key])) { | |
| 77 | throw AppException::createForReason(AppError::DuplicateId, appId: $app->id->value, firstClass: $classesByKey[$key], secondClass: $app->class); | |
| 78 | } | |
| 79 | ||
| 80 | $classesByKey[$key] = $app->class; | |
| 81 | $apps[] = $app; | |
| 82 | } | |
| 83 | ||
| 84 | return new DiscoveryResult( | |
| 85 | DiscoveredAppCollection::fromApps($apps), | |
| 86 | UnmarkedAppCollection::fromList($unmarked), | |
| 87 | UnloadableAppFileCollection::fromList($unloadable), | |
| 88 | $templates, | |
| 89 | ); | |
| 90 | } | |
| 91 | ||
| 92 | private function findClassForFile(AutoloadRoot $root, SplFileInfo $file): ?AppClassReflection | |
| 93 | { | |
| 94 | $path = $file->getRealPath(); | |
| 95 | $class = $path === false ? null : $root->findClassNameForFile($path); | |
| 96 | ||
| 97 | return $class === null || !class_exists($class) ? null : new AppClassReflection(new ReflectionClass($class)); | |
| 98 | } | |
| 99 | ||
| 100 | /** @throws AppException */ | |
| 101 | private function validateAppId(AppClassReflection $class, Automation $automation): AppId | |
| 102 | { | |
| 103 | $class->requireRunnableApp('#[Automation]'); | |
| 104 | ||
| 105 | return AppId::tryFromString($automation->id) ?? throw AppException::createForReason(AppError::IdInvalid, className: $class->getName(), appId: $automation->id); | |
| 106 | } | |
| 107 | } |