Files
php-qml/framework/php/src/Maker/templates/ReadModelController.tpl.php

30 lines
769 B
PHP
Raw Normal View History

v0.2.0 (8/N): make:bridge:read-model maker Implements PLAN.md §8's fourth makers-table row. Read-models are server-side projections — joined fetches, aggregates, denormalised views — that QML reads without going through a writable #[BridgeResource]. The maker emits: - src/ReadModel/<Name>ReadModel.php — query service stub injecting EntityManagerInterface; user fills query() with DQL / QueryBuilder / raw SQL as fits. - src/Controller/<Name>Controller.php — single GET handler at /api/<kebab-plural>, just normalises the read-model output to JSON. - {qml_path}/<Name>List.qml — ReactiveListModel bound to the route, deliberately no Mercure topic. The "no topic" choice is the design call worth documenting: read-models are queries, not reactive resources, and pretending otherwise would either auto-publish stale aggregates on every entity change or require the user to invent invalidation logic in the listener. Better: pair the read-model with `make:bridge:event` and call refresh() from the QML event-handler when the underlying data really changes. Naming convention: kebab-PLURAL routes (`/api/todo-summaries`) for consistency with REST list semantics; resource path stays singular under `src/ReadModel/`. Wired into services.yaml's when@dev block. Three new snapshot baselines (TodoSummaryReadModel.php / TodoSummaryController.php / TodoSummaryList.qml) plus runner extension. All 14 maker outputs verify on the committed state. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 20:28:50 +02:00
<?= "<?php\n" ?>
declare(strict_types=1);
namespace App\Controller;
use App\ReadModel\<?= $singular ?>ReadModel;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
/**
* Read-only endpoint for the <?= $singular ?> projection.
* Auto-generated by `make:bridge:read-model` the read-model owns
* the query; this controller just normalises the result to JSON.
*/
final class <?= $singular ?>Controller
{
public function __construct(
private readonly <?= $singular ?>ReadModel $readModel,
) {
}
#[Route('<?= $route ?>', name: '<?= $resource ?>_read', methods: ['GET'])]
public function __invoke(): JsonResponse
{
return new JsonResponse($this->readModel->query());
}
}