>|null */ private ?array $modules = null; /** * @return array> key => class */ public function modules(): array { if ($this->modules === null) { $this->modules = []; foreach ((array) config('process.modules', []) as $class) { $this->modules[$class::key()] = $class; } } return $this->modules; } public function hasModule(string $key): bool { return array_key_exists($key, $this->modules()); } public function module(string $key): ProcessModule { $class = $this->modules()[$key] ?? throw new InvalidArgumentException("Unknown process module [{$key}]."); return app($class); } /** * Whether the registered class can serve a plan that froze the module in the given version. */ public function serves(string $key, int $version): bool { $class = $this->modules()[$key] ?? null; return $class !== null && ($class::version() === $version || in_array($version, (array) config("process.compatible.{$key}", []), true)); } /** * @return array>> */ public function modelVersions(): array { return (array) config('process.models', []); } public function hasModel(string $key, ?int $version = null): bool { $versions = $this->modelVersions()[$key] ?? []; return $version === null ? $versions !== [] : array_key_exists($version, $versions); } /** * A model in a version (null: the latest). */ public function model(string $key, ?int $version = null): ProcessModel { $versions = $this->modelVersions()[$key] ?? throw new InvalidArgumentException("Unknown process model [{$key}]."); $version ??= max(array_keys($versions)); $class = $versions[$version] ?? throw new InvalidArgumentException("Process model [{$key}] has no version {$version}."); return app($class); } /** * The latest version of every model. * * @return array */ public function models(): array { $models = []; foreach (array_keys($this->modelVersions()) as $key) { $models[$key] = $this->model($key); } return $models; } /** * @return array>> */ public function templateVersions(): array { return (array) config('process.templates', []); } /** * The class of a template named by key (latest version) or key@version; null when it is not registered. * * @return class-string|null */ public function templateClass(string $reference): ?string { [$key, $version] = array_pad(explode('@', $reference, 2), 2, null); $versions = $this->templateVersions()[$key] ?? []; if ($versions === []) { return null; } return $versions[$version === null ? max(array_keys($versions)) : (int) $version] ?? null; } public function hasTemplate(string $reference): bool { return $this->templateClass($reference) !== null; } /** * The latest version of every template, optionally of one area. * * @return array> */ public function templates(?TemplateArea $area = null): array { $templates = []; foreach (array_keys($this->templateVersions()) as $key) { $class = $this->templateClass($key); if ($area === null || $class::area() === $area) { $templates[$key] = $class; } } return $templates; } /** * The model a case follows when nobody chose one: derived from its frozen profile exactly as the v0.11 course * branched (legacy mapping, docs/Prozessarchitektur/Architektur/06 §4). */ public function derivedModel(ProcessProfile $profile): string { return match ($profile->intake) { IntakeKind::Internal => 'a6.internal', IntakeKind::Inquiry => ($profile->caseType === CaseType::A2 ? 'a2' : 'a1').($profile->inquiryAnswer() === InquiryAnswer::Offer ? '.offer' : '.information'), IntakeKind::Booking => match ($profile->caseType) { CaseType::A5 => 'a5.portal_booking', CaseType::A4 => 'a4.portal_booking', default => 'a3.portal_booking', }, }; } /** * The models admissible for a case type and intake kind. * * @return array */ public function admissible(IntakeKind $intake, ?CaseType $caseType = null): array { return array_filter($this->models(), fn (ProcessModel $model): bool => in_array($intake, $model::intakes(), true) && ($caseType === null || in_array($caseType, $model::caseTypes(), true))); } /** * Everything that is inconsistent in the catalogue (empty = consistent). * * @return list */ public function problems(): array { $problems = []; foreach ($this->modules() as $key => $class) { $definition = $class::definition(); foreach ($definition->requires as $dependency) { if (! $this->hasModule($dependency->module)) { $problems[] = "Modul {$key}: Abhängigkeit {$dependency->module} ist nicht registriert."; } } if (! in_array($definition->phase, ['P01', 'P02', 'P03', 'P04', 'P05'], true)) { $problems[] = "Modul {$key}: unbekannte Phase {$definition->phase}."; } if (is_subclass_of($class, RunsAutomatically::class) && $definition->modes === []) { $problems[] = "Modul {$key}: läuft automatisch, nennt aber keine Ausführungsart."; } } foreach ($this->cycles() as $cycle) { $problems[] = 'Zyklische harte Abhängigkeit: '.implode(' → ', $cycle); } foreach ($this->templateVersions() as $key => $versions) { foreach ($versions as $version => $class) { if ($class::key() !== $key || $class::version() !== $version) { $problems[] = "Schablone {$key}@{$version}: Klasse {$class} meldet ".$class::reference().'.'; } foreach ($class::modules() as $module) { if (! $this->hasModule($module)) { $problems[] = "Schablone {$class::reference()}: Modul {$module} ist nicht registriert."; } } if (! in_array($class::area()->result(), $class::provides(), true)) { $problems[] = "Schablone {$class::reference()}: liefert das Ergebnis ihres Bereichs ({$class::area()->result()}) nicht."; } $allowed = array_map(fn ($setting): string => $setting->value, $class::area()->settings()); if (array_diff([...array_keys($class::fixes()), ...array_keys($class::defaults())], $allowed) !== []) { $problems[] = "Schablone {$class::reference()}: darf nur Einstellungen ihres Bereichs ({$class::area()->label()}) festlegen oder vorschlagen."; } foreach ($class::configures() as $module) { if (! $this->hasModule($module)) { $problems[] = "Schablone {$class::reference()}: konfiguriertes Modul {$module} ist nicht registriert."; } } } } foreach ($this->modelVersions() as $key => $versions) { foreach ($versions as $version => $class) { if ($class::key() !== $key || $class::version() !== $version) { $problems[] = "Modell {$key}@{$version}: Klasse {$class} meldet ".$class::key().'@'.$class::version().'.'; } $model = app($class); foreach ($model->slots() as $area) { $default = $class::defaultTemplates()[$area->value] ?? null; if ($default === null || ! $this->hasTemplate($default) || $this->templateClass($default)::area() !== $area) { $problems[] = "Modell {$key}@{$version}: Platz {$area->value} hat keine gültige Standardschablone."; } } foreach ($class::presetTemplates() as $area => $reference) { if (! $this->hasTemplate($reference) || $this->templateClass($reference)::area()->value !== $area) { $problems[] = "Modell {$key}@{$version}: Voreinstellung {$area} nennt die Schablone {$reference} nicht (richtig)."; } } foreach ($model->variants() as $variant) { foreach ($model->modules($variant->key) as $module) { if (! $this->hasModule($module)) { $problems[] = "Modell {$key}@{$version}, Variante {$variant->key}: Modul {$module} ist nicht registriert."; } } foreach ($variant->inserts() as $point => $modules) { if (! in_array($point, array_map(fn ($p) => $p->value, $model->points()), true)) { $problems[] = "Modell {$key}@{$version}, Variante {$variant->key}: Erweiterungspunkt {$point} gibt es im Modell nicht."; } foreach ($modules as $module) { $points = $this->hasModule($module) ? $this->modules()[$module]::definition()->points : []; if ($points !== [] && ! in_array($point, $points, true)) { $problems[] = "Modell {$key}@{$version}: Modul {$module} darf nicht an {$point} stehen."; } if ($this->hasModule($module) && $this->modules()[$module]::definition()->isTesterOnly() && ! $variant->isTesterOnly()) { $problems[] = "Modell {$key}@{$version}: Modul {$module} ist nur für den Prozess-Tester, Variante {$variant->key} aber nicht."; } } } } } } return $problems; } /** * @return list> */ private function cycles(): array { $edges = []; foreach ($this->modules() as $key => $class) { $edges[$key] = array_values(array_map(fn ($dependency): string => $dependency->module, array_filter($class::definition()->requires, fn ($dependency): bool => $dependency->hard))); } $cycles = []; $visit = function (string $node, array $path) use (&$visit, &$cycles, $edges): void { if (in_array($node, $path, true)) { $cycles[] = [...array_slice($path, (int) array_search($node, $path, true)), $node]; return; } foreach ($edges[$node] ?? [] as $next) { $visit($next, [...$path, $node]); } }; foreach (array_keys($edges) as $node) { $visit($node, []); } return array_values(array_unique($cycles, SORT_REGULAR)); } }