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>
This commit is contained in:
2026-05-03 20:28:50 +02:00
parent 00a64c5871
commit 1d014ae3b7
10 changed files with 348 additions and 8 deletions

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace App\Controller;
use App\ReadModel\TodoSummaryReadModel;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
/**
* Read-only endpoint for the TodoSummary projection.
* Auto-generated by `make:bridge:read-model` — the read-model owns
* the query; this controller just normalises the result to JSON.
*/
final class TodoSummaryController
{
public function __construct(
private readonly TodoSummaryReadModel $readModel,
) {
}
#[Route('/api/todo-summaries', name: 'todo_summary_read', methods: ['GET'])]
public function __invoke(): JsonResponse
{
return new JsonResponse($this->readModel->query());
}
}

View File

@@ -0,0 +1,28 @@
// Auto-generated by `bin/console make:bridge:read-model TodoSummary`.
// Read-only projection — no Mercure topic, no auto-updates.
//
// For invalidation: when the underlying data changes, dispatch a
// domain event from PHP (see `make:bridge:event`) and call
// `todoSummaryList.refresh()` from the event-handler in QML.
import QtQuick
import QtQuick.Controls
import PhpQml.Bridge
ListView {
id: todoSummaryList
model: ReactiveListModel {
baseUrl: BackendConnection.url
token: BackendConnection.token
source: "/api/todo-summaries"
// topic: intentionally unset — read-models are queries, not
// reactive resources. Drive refreshes from domain events.
}
delegate: ItemDelegate {
// Replace with your projection's columns.
text: String(model.modelData)
width: ListView.view.width
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace App\ReadModel;
use Doctrine\ORM\EntityManagerInterface;
/**
* Auto-generated query service for the TodoSummary read-model.
*
* Read-models are server-side projections — joined fetches, aggregates,
* denormalised views — that QML reads without going through a writable
* `#[BridgeResource]`. Replace the body of `query()` with the actual
* DQL / raw SQL / joined fetch.
*
* Per PLAN.md §4 *Pagination*, return an array of associative arrays so
* the controller can normalise to JSON without a serializer; or wire up
* a normalizer if you prefer typed DTOs in the projection.
*/
final readonly class TodoSummaryReadModel
{
public function __construct(
private EntityManagerInterface $em,
) {
}
/**
* @return list<array<string, mixed>>
*/
public function query(): array
{
// TODO: implement the read query — DQL, ->createQueryBuilder(),
// or ->getConnection()->executeQuery() as fits.
return [];
}
}

View File

@@ -51,16 +51,20 @@ clear_outputs
&& bin/console make:bridge:resource Todo --no-interaction >/dev/null \
&& bin/console make:bridge:command MarkAllDone --no-interaction >/dev/null \
&& bin/console make:bridge:window Todo --no-interaction >/dev/null \
&& bin/console make:bridge:event TodoCompleted --no-interaction >/dev/null )
&& bin/console make:bridge:event TodoCompleted --no-interaction >/dev/null \
&& bin/console make:bridge:read-model TodoSummary --no-interaction >/dev/null )
check "$APP/symfony/src/Entity/Todo.php" "$SCRIPT_DIR/Todo.php"
check "$APP/symfony/src/Controller/TodoController.php" "$SCRIPT_DIR/TodoController.php"
check "$APP/qml/TodoList.qml" "$SCRIPT_DIR/TodoList.qml"
check "$APP/symfony/src/Controller/MarkAllDoneController.php" "$SCRIPT_DIR/MarkAllDoneController.php"
check "$APP/qml/TodoWindow.qml" "$SCRIPT_DIR/TodoWindow.qml"
check "$APP/symfony/src/Event/TodoCompletedEvent.php" "$SCRIPT_DIR/TodoCompletedEvent.php"
check "$APP/symfony/src/Entity/Todo.php" "$SCRIPT_DIR/Todo.php"
check "$APP/symfony/src/Controller/TodoController.php" "$SCRIPT_DIR/TodoController.php"
check "$APP/qml/TodoList.qml" "$SCRIPT_DIR/TodoList.qml"
check "$APP/symfony/src/Controller/MarkAllDoneController.php" "$SCRIPT_DIR/MarkAllDoneController.php"
check "$APP/qml/TodoWindow.qml" "$SCRIPT_DIR/TodoWindow.qml"
check "$APP/symfony/src/Event/TodoCompletedEvent.php" "$SCRIPT_DIR/TodoCompletedEvent.php"
check "$APP/symfony/src/EventSubscriber/TodoCompletedSubscriber.php" "$SCRIPT_DIR/TodoCompletedSubscriber.php"
check "$APP/qml/TodoCompletedEventHandler.qml" "$SCRIPT_DIR/TodoCompletedEventHandler.qml"
check "$APP/qml/TodoCompletedEventHandler.qml" "$SCRIPT_DIR/TodoCompletedEventHandler.qml"
check "$APP/symfony/src/ReadModel/TodoSummaryReadModel.php" "$SCRIPT_DIR/TodoSummaryReadModel.php"
check "$APP/symfony/src/Controller/TodoSummaryController.php" "$SCRIPT_DIR/TodoSummaryController.php"
check "$APP/qml/TodoSummaryList.qml" "$SCRIPT_DIR/TodoSummaryList.qml"
# ── Mode 2: --with-dto (re-runs make:bridge:resource only) ────────────
# The entity + QML output is byte-identical between modes; only the