Files
php-qml/framework/php/src/CorrelationContext.php
magdev 1c5a5761f6
Some checks failed
CI / Quality (push) Failing after 1m54s
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

37 lines
814 B
PHP

<?php
declare(strict_types=1);
namespace PhpQml\Bridge;
/**
* Per-request correlation key holder.
*
* The HTTP request's `Idempotency-Key` (PLAN.md §4 *Idempotency*) is
* stashed here on RequestEvent and read back by ModelPublisher when
* it builds Mercure envelopes, so QML clients can match Mercure echoes
* to the optimistic mutation that originated them (§5).
*
* Cleared on TerminateEvent. CLI commands and out-of-request mutations
* see no correlation key, which is the correct behaviour.
*/
final class CorrelationContext
{
private ?string $key = null;
public function set(?string $key): void
{
$this->key = $key;
}
public function get(): ?string
{
return $this->key;
}
public function clear(): void
{
$this->key = null;
}
}