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>
This commit is contained in:
2026-05-03 19:50:01 +02:00
parent 4d6b9fde2c
commit 56e3d671d9
12 changed files with 168 additions and 49 deletions

View File

@@ -8,32 +8,35 @@ use PhpQml\Bridge\Attribute\BridgeResource;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/**
* Translates Doctrine entity lifecycle events into Mercure envelopes.
* Default implementation of {@see ModelPublisherInterface}: dual-publishes
* each change to the collection and entity topics for a `#[BridgeResource]`
* entity.
*
* For each change, dual-publishes to:
* For each change, publishes to:
* - `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*).
*
* Phase 2 uses a per-process counter for the envelope `version` field
* — sufficient for single-instance dev mode. Phase 4 / production should
* back this with a persistent monotonic source (e.g. Postgres SEQUENCE).
* 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.
*/
final class ModelPublisher
final class ModelPublisher implements ModelPublisherInterface
{
/** @var array<string, int> */
private array $versions = [];
public function __construct(
private readonly Publisher $publisher,
private readonly CorrelationContext $correlationContext,
private readonly PublisherInterface $publisher,
private readonly CorrelationContextInterface $correlationContext,
private readonly NormalizerInterface $normalizer,
) {
}
public function publishEntityChange(object $entity, string $op): void
public function publishEntityChange(object $entity, BridgeOp $op): void
{
$resource = $this->resolveResource($entity);
if (null === $resource) {
@@ -44,10 +47,10 @@ final class ModelPublisher
$id = (string) $this->extractId($entity);
$envelope = [
'op' => $op,
'op' => $op->value,
'id' => $id,
'version' => $this->nextVersion($name),
'data' => 'delete' === $op ? null : $this->normalize($entity),
'data' => BridgeOp::Delete === $op ? null : $this->normalize($entity),
];
if (null !== $key = $this->correlationContext->get()) {
@@ -77,10 +80,7 @@ final class ModelPublisher
$r = new \ReflectionClass($entity);
if ($r->hasProperty('id')) {
$prop = $r->getProperty('id');
$prop->setAccessible(true);
return $prop->getValue($entity);
return $r->getProperty('id')->getValue($entity);
}
throw new \LogicException(\sprintf('Cannot extract id from %s: no getId() method or $id property.', $entity::class));