Skip to content

Commit a59fd33

Browse files
committed
Register mercure hub services in Turbo
1 parent 5b429a7 commit a59fd33

File tree

3 files changed

+63
-8
lines changed

3 files changed

+63
-8
lines changed

src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php

+9-8
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
namespace Symfony\UX\Turbo\Bridge\Mercure;
1313

14-
use Symfony\Component\HttpFoundation\RequestStack;
15-
use Symfony\Component\Mercure\Authorization;
1614
use Symfony\Component\Mercure\HubInterface;
1715
use Symfony\Component\Mercure\Twig\MercureExtension;
1816
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
1917
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
2018
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererWithOptionsInterface;
2119
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
2220
use Twig\Environment;
21+
use Twig\Error\RuntimeError;
2322

2423
/**
2524
* Renders the attributes to load the "mercure-turbo-stream" controller.
@@ -64,12 +63,14 @@ public function renderTurboStreamListen(Environment $env, $topic /* array $event
6463
}
6564

6665
if (isset($eventSourceOptions)) {
67-
if ($mercure = $this->twig->getExtension(MercureExtension::class)) {
68-
$mercure->mercure($topic, $eventSourceOptions);
69-
}
70-
71-
if (isset($eventSourceOptions['withCredentials'])) {
72-
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
66+
try {
67+
$mercure = $this->twig->getExtension(MercureExtension::class);
68+
$mercure->mercure($topics, $eventSourceOptions);
69+
70+
if (isset($eventSourceOptions['withCredentials'])) {
71+
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
72+
}
73+
} catch (RuntimeError $e) {
7374
}
7475
}
7576

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\UX\Turbo\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\UX\Turbo\Bridge\Mercure\Broadcaster;
18+
use Symfony\UX\Turbo\Bridge\Mercure\TurboStreamListenRenderer;
19+
20+
/**
21+
* This compiler pass ensures that TurboStreamListenRenderer
22+
* and Broadcast are registered per Mercure hub.
23+
*
24+
* @author Pierre Ambroise<[email protected]>
25+
*/
26+
final class RegisterMercureHubs implements CompilerPassInterface
27+
{
28+
public function process(ContainerBuilder $container)
29+
{
30+
foreach ($container->findTaggedServiceIds('mercure.hub') as $hubId => $tag) {
31+
$name = str_replace('mercure.hub.', '', $hubId);
32+
33+
$container->register("turbo.mercure.$name.renderer", TurboStreamListenRenderer::class)
34+
->addArgument(new Reference($hubId))
35+
->addArgument(new Reference('turbo.mercure.stimulus_helper'))
36+
->addArgument(new Reference('turbo.id_accessor'))
37+
->addArgument(new Reference('twig'))
38+
->addTag('turbo.renderer.stream_listen', ['transport' => $name]);
39+
40+
if (isset($tag['default']) && $tag['default']) {
41+
$container->getDefinition("turbo.mercure.{$name}.renderer")
42+
->addTag('turbo.renderer.stream_listen', ['transport' => 'default']);
43+
}
44+
45+
$container->register("turbo.mercure.$name.broadcaster", Broadcaster::class)
46+
->addArgument($name)
47+
->addArgument(new Reference($hubId))
48+
->addTag('turbo.broadcaster');
49+
}
50+
}
51+
}

src/Turbo/src/TurboBundle.php

+3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\HttpKernel\Bundle\Bundle;
18+
use Symfony\UX\Turbo\DependencyInjection\Compiler\RegisterMercureHubs;
1819

1920
/**
2021
* @author Kévin Dunglas <[email protected]>
@@ -28,6 +29,8 @@ public function build(ContainerBuilder $container): void
2829
{
2930
parent::build($container);
3031

32+
$container->addCompilerPass(new RegisterMercureHubs());
33+
3134
$container->addCompilerPass(new class implements CompilerPassInterface {
3235
public function process(ContainerBuilder $container): void
3336
{

0 commit comments

Comments
 (0)