35 lines
1.0 KiB
PHP
35 lines
1.0 KiB
PHP
<?php
|
|
|
|
namespace App\Base\Util;
|
|
|
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
|
use Symfony\Component\Mercure\HubInterface;
|
|
use Symfony\Component\Mercure\Update;
|
|
use Twig\Environment;
|
|
|
|
readonly class Broadcaster
|
|
{
|
|
public function __construct(
|
|
#[Autowire(service: 'twig')]
|
|
private Environment $renderer,
|
|
private HubInterface $hub,
|
|
private RequestStack $requestStack,
|
|
) {}
|
|
|
|
public function alert(string $title, string $message, string $type = "success"): void
|
|
{
|
|
$userAlertTopic = $this->requestStack->getCurrentRequest()->getSession()->get('mercure_alert_topic');
|
|
$update = new Update(
|
|
$userAlertTopic,
|
|
$this->renderer->render('broadcast/Alert.stream.html.twig', [
|
|
'alert_id' => uniqid(),
|
|
'title' => $title,
|
|
'message' => $message,
|
|
'type' => $type,
|
|
])
|
|
);
|
|
$this->hub->publish($update);
|
|
}
|
|
}
|