Files
php-qml/framework/php/src/Publisher.php
T

35 lines
994 B
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace PhpQml\Bridge;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Update;
/**
* Default implementation of {@see PublisherInterface}: a thin wrapper over
* `symfony/mercure`'s `HubInterface` that JSON-encodes the envelope and
* forwards the publish.
*
* Topic conventions and envelope shape are defined in PLAN.md §4.
* Application code should typehint `PublisherInterface` instead of this
* concrete class so swappable implementations (offline buffer, multi-hub
* fan-out) remain a non-breaking change.
*/
final readonly class Publisher implements PublisherInterface
{
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),
$private,
));
}
}