feat(form-builder): introduce purpose registry and definition value object

Lands the v1.0 purpose registry (WS-2 of the consolidation sprint) as a
first-class concept: a `PurposeDefinition` value object, a
`PurposeRegistry` service keyed by slug, and a declarative
`config/form_builder/purposes.php` registry with exactly the seven
purposes from ARCH-CONSOLIDATION §6.4.

Also rebuilds the morph-map in `AppServiceProvider::boot` into three
labelled blocks: (1) domain subject types derived from
`PurposeRegistry::allSubjectTypes()`, (2) non-purpose domain types
hardcoded with comments (form_schemas owner_types, activity-log
subjects), (3) framework types (spatie/activitylog; Sanctum stays
absent per addendum Q4).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-24 14:35:18 +02:00
parent ad941cc944
commit e93207765b
5 changed files with 341 additions and 69 deletions

View File

@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace App\FormBuilder\Purposes\Exceptions;
use RuntimeException;
final class PurposeNotFoundException extends RuntimeException
{
public static function forSlug(string $slug): self
{
return new self("Purpose '{$slug}' is not registered in config('form_builder.purposes').");
}
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace App\FormBuilder\Purposes;
use App\Enums\FormBuilder\FormSubmissionMode;
final readonly class PurposeDefinition
{
/**
* @param list<string> $requiredBindings Binding paths in `{entity}.{attribute}` form
* (Pattern A notation used in `form_fields.binding` JSON).
* Consumed by the pre-publish check (FormSchemaService::publish)
* and, after WS-5/WS-6, by `form_field_bindings`.
*/
public function __construct(
public string $slug,
public string $label,
public string $subjectType,
public FormSubmissionMode $defaultSubmissionMode,
public bool $allowsPublicAccess,
public array $requiredBindings,
) {}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace App\FormBuilder\Purposes;
use App\Enums\FormBuilder\FormSubmissionMode;
use App\FormBuilder\Purposes\Exceptions\PurposeNotFoundException;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
final class PurposeRegistry
{
/** @var array<string, PurposeDefinition>|null */
private ?array $cache = null;
public function __construct(private readonly ConfigRepository $config) {}
/** @return array<string, PurposeDefinition> keyed by slug */
public function all(): array
{
if ($this->cache !== null) {
return $this->cache;
}
/** @var array<string, array<string, mixed>> $raw */
$raw = (array) $this->config->get('form_builder.purposes', []);
$definitions = [];
foreach ($raw as $slug => $attrs) {
$mode = $attrs['default_submission_mode'] ?? null;
if (! $mode instanceof FormSubmissionMode) {
throw new \InvalidArgumentException(
"Purpose '{$slug}' has invalid default_submission_mode; expected FormSubmissionMode enum case."
);
}
$definitions[(string) $slug] = new PurposeDefinition(
slug: (string) $slug,
label: (string) ($attrs['label'] ?? ''),
subjectType: (string) ($attrs['subject_type'] ?? ''),
defaultSubmissionMode: $mode,
allowsPublicAccess: (bool) ($attrs['allows_public_access'] ?? false),
requiredBindings: array_values((array) ($attrs['required_bindings'] ?? [])),
);
}
return $this->cache = $definitions;
}
public function get(string $slug): PurposeDefinition
{
$all = $this->all();
if (! isset($all[$slug])) {
throw PurposeNotFoundException::forSlug($slug);
}
return $all[$slug];
}
public function has(string $slug): bool
{
return isset($this->all()[$slug]);
}
/** @return list<string> unique sorted list of subject_type aliases */
public function allSubjectTypes(): array
{
$types = array_values(array_unique(array_map(
static fn (PurposeDefinition $p): string => $p->subjectType,
$this->all(),
)));
sort($types);
return $types;
}
/** @return list<string> slugs with allows_public_access === true */
public function publicAccessibleSlugs(): array
{
return array_values(array_keys(array_filter(
$this->all(),
static fn (PurposeDefinition $p): bool => $p->allowsPublicAccess,
)));
}
}