Files
php-qml/framework/php/src/ModelPublisher.php

118 lines
3.7 KiB
PHP
Raw Normal View History

Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
<?php
declare(strict_types=1);
namespace PhpQml\Bridge;
use PhpQml\Bridge\Attribute\BridgeResource;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
* Default implementation of {@see ModelPublisherInterface}: dual-publishes
* each change to the collection and entity topics for a `#[BridgeResource]`
* entity.
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
*
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
* For each change, publishes to:
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
* - `app://model/{name}` collection topic, watched by ReactiveListModel
* - `app://model/{name}/{id}` entity topic, watched by ReactiveObject
*
* Topic / envelope shape per PLAN.md §4. The `correlationKey` echoes
* the originating request's `Idempotency-Key` (§5 *Optimistic updates*).
*
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
* Uses a per-process counter for the envelope `version` field sufficient
* for single-instance bundled mode. Multi-instance / production deployments
* should back this with a persistent monotonic source (e.g. Postgres
* SEQUENCE); deferred to v0.2.0+ §13.
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
*/
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
final class ModelPublisher implements ModelPublisherInterface
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
{
/** @var array<string, int> */
private array $versions = [];
public function __construct(
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
private readonly PublisherInterface $publisher,
private readonly CorrelationContextInterface $correlationContext,
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
private readonly NormalizerInterface $normalizer,
) {
}
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
public function publishEntityChange(object $entity, BridgeOp $op): void
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
{
$resource = $this->resolveResource($entity);
if (null === $resource) {
return; // not a #[BridgeResource]
}
$name = $resource->name ?? self::deriveName($entity::class);
$id = (string) $this->extractId($entity);
$envelope = [
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
'op' => $op->value,
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
'id' => $id,
'version' => $this->nextVersion($name),
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
'data' => BridgeOp::Delete === $op ? null : $this->normalize($entity),
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
];
if (null !== $key = $this->correlationContext->get()) {
$envelope['correlationKey'] = $key;
}
$this->publisher->publish("app://model/{$name}", $envelope);
$this->publisher->publish("app://model/{$name}/{$id}", $envelope);
}
private function resolveResource(object $entity): ?BridgeResource
{
$reflection = new \ReflectionClass($entity);
$attrs = $reflection->getAttributes(BridgeResource::class);
if ([] === $attrs) {
return null;
}
return $attrs[0]->newInstance();
}
private function extractId(object $entity): mixed
{
if (method_exists($entity, 'getId')) {
return $entity->getId();
}
$r = new \ReflectionClass($entity);
if ($r->hasProperty('id')) {
v0.2.0 (1/N): public API surface — interfaces + BridgeOp enum Establishes the contract layer the rest of v0.2.0 builds on. Pre-1.0 SemVer break: ModelPublisher::publishEntityChange() now takes BridgeOp instead of a raw string. Interfaces (Symfony idiom: same namespace as concrete, like HubInterface next to Hub): - PublisherInterface — publish(string, array, bool) - ModelPublisherInterface — publishEntityChange(object, BridgeOp) - CorrelationContextInterface — set/get/clear App code should typehint these instead of the concretes so swappable implementations (offline-buffer publisher, multi-hub fan-out, request- stamp correlation) remain non-breaking. Concrete classes implement them unchanged; autowire continues to inject the implementations transparently. BridgeOp: PHP 8.1 string-backed enum with cases Upsert / Delete / Replace / Event matching PLAN.md §4's envelope `op` wire format. Internal call sites updated; tests use the cases directly. Switched typehints: - ModelPublisher ctor: PublisherInterface + CorrelationContextInterface - DoctrineBridgeListener ctor: ModelPublisherInterface - HealthController ctor: PublisherInterface (still emits `Publisher` as bundle canary value — `::class` resolves to the concrete class name regardless of typehint, so bundled-supervisor.sh's grep stays green) - skeleton PingController ctor: PublisherInterface (canonical app pattern — example/todo has no Publisher consumer to update) Drive-by: removed deprecated setAccessible(true) call in ModelPublisher::extractId — PHP 8.1+ allows reflection without it. PHPStan + cs-fixer + PHPUnit (17/17) + maker snapshot all pass; dev container compiles in the example app. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 19:50:01 +02:00
return $r->getProperty('id')->getValue($entity);
Phase 2 sub-commit 2: ModelPublisher + #[BridgeResource] + Doctrine listener Bundle gains the model layer that bridges Doctrine entities to Mercure without per-resource glue. Three new pieces: - `#[BridgeResource(name: ?string)]` attribute marks an entity as a reactive bridge model. Topic name defaults to the lowercased class basename and can be overridden per resource. - `ModelPublisher` translates entity changes into PLAN.md §4 envelopes ({op, id, data, version, ?correlationKey}) and dual-publishes them on `app://model/{name}` (collection topic) and `app://model/{name}/{id}` (entity topic). Entity normalisation goes through Symfony's Serializer (ObjectNormalizer + DateTime + BackedEnum) for predictable JSON. The envelope `version` field is a per-process monotonic counter — fine for single-instance dev mode; production should back this with a Postgres SEQUENCE or equivalent (noted for Phase 4). - `DoctrineBridgeListener` registers `postPersist`/`postUpdate`/ `postRemove` via `#[AsDoctrineListener]` and routes events through ModelPublisher. Entities without `#[BridgeResource]` are silently skipped. Plus the correlation-key plumbing the §5 Update Semantics layer needs: - `CorrelationContext` is a per-request holder for the originating request's `Idempotency-Key`. - `CorrelationKeyListener` reads the header on `KernelEvents::REQUEST` and clears the context on `KernelEvents::TERMINATE` (worker mode hygiene). CLI mutations see no key, which is correct. Bundle composer.json picks up `doctrine/dbal`, `doctrine/orm`, `doctrine/doctrine-bundle`, `symfony/serializer`, `symfony/property-*`, `symfony/uid`. PHPStan extension `phpstan-doctrine` added so the listener's event-args types resolve. Skeleton's framework.yaml enables `serializer` and `property_info`. Tests: 5 new for ModelPublisher (dual publish, correlation echo, delete op omits data, untagged entities ignored, version increments). Total: 16 tests, 45 assertions, PHPStan clean, cs-fixer clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-02 02:32:51 +02:00
}
throw new \LogicException(\sprintf('Cannot extract id from %s: no getId() method or $id property.', $entity::class));
}
/**
* @return array<string, mixed>
*/
private function normalize(object $entity): array
{
/** @var array<string, mixed> $data */
$data = $this->normalizer->normalize($entity, 'json', [
'circular_reference_handler' => static fn (object $o): string => (string) ($o->getId() ?? ''),
]);
return $data;
}
private function nextVersion(string $name): int
{
if (!isset($this->versions[$name])) {
$this->versions[$name] = (int) (microtime(true) * 1_000_000);
}
return ++$this->versions[$name];
}
private static function deriveName(string $fqcn): string
{
$basename = substr($fqcn, (int) strrpos($fqcn, '\\') + 1);
return strtolower($basename);
}
}