2026-05-02 01:05:19 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace PhpQml\Bridge;
|
|
|
|
|
|
|
|
|
|
use Symfony\Component\Mercure\HubInterface;
|
|
|
|
|
use Symfony\Component\Mercure\Update;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-05-03 19:50:01 +02:00
|
|
|
* Default implementation of {@see PublisherInterface}: a thin wrapper over
|
|
|
|
|
* `symfony/mercure`'s `HubInterface` that JSON-encodes the envelope and
|
|
|
|
|
* forwards the publish.
|
2026-05-02 01:05:19 +02:00
|
|
|
*
|
|
|
|
|
* Topic conventions and envelope shape are defined in PLAN.md §4.
|
2026-05-03 19:50:01 +02:00
|
|
|
* Application code should typehint `PublisherInterface` instead of this
|
|
|
|
|
* concrete class so swappable implementations (offline buffer, multi-hub
|
|
|
|
|
* fan-out) remain a non-breaking change.
|
2026-05-02 01:05:19 +02:00
|
|
|
*/
|
2026-05-03 19:50:01 +02:00
|
|
|
final readonly class Publisher implements PublisherInterface
|
2026-05-02 01:05:19 +02:00
|
|
|
{
|
|
|
|
|
public function __construct(
|
|
|
|
|
private HubInterface $hub,
|
|
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function publish(string $topic, array $envelope, bool $private = false): string
|
|
|
|
|
{
|
|
|
|
|
return $this->hub->publish(new Update(
|
|
|
|
|
$topic,
|
2026-05-02 02:15:06 +02:00
|
|
|
json_encode($envelope, \JSON_THROW_ON_ERROR),
|
2026-05-02 01:05:19 +02:00
|
|
|
$private,
|
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
}
|