Skip to content

Commit

Permalink
Register mercure hub services in Turbo
Browse files Browse the repository at this point in the history
  • Loading branch information
Fan2Shrek committed Feb 28, 2025
1 parent 5b429a7 commit a59fd33
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@

namespace Symfony\UX\Turbo\Bridge\Mercure;

use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Mercure\Authorization;
use Symfony\Component\Mercure\HubInterface;
use Symfony\Component\Mercure\Twig\MercureExtension;
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererWithOptionsInterface;
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
use Twig\Environment;
use Twig\Error\RuntimeError;

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

if (isset($eventSourceOptions)) {
if ($mercure = $this->twig->getExtension(MercureExtension::class)) {
$mercure->mercure($topic, $eventSourceOptions);
}

if (isset($eventSourceOptions['withCredentials'])) {
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
try {
$mercure = $this->twig->getExtension(MercureExtension::class);
$mercure->mercure($topics, $eventSourceOptions);

if (isset($eventSourceOptions['withCredentials'])) {
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
}
} catch (RuntimeError $e) {
}
}

Expand Down
51 changes: 51 additions & 0 deletions src/Turbo/src/DependencyInjection/Compiler/RegisterMercureHubs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\UX\Turbo\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\UX\Turbo\Bridge\Mercure\Broadcaster;
use Symfony\UX\Turbo\Bridge\Mercure\TurboStreamListenRenderer;

/**
* This compiler pass ensures that TurboStreamListenRenderer
* and Broadcast are registered per Mercure hub.
*
* @author Pierre Ambroise<[email protected]>
*/
final class RegisterMercureHubs implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
foreach ($container->findTaggedServiceIds('mercure.hub') as $hubId => $tag) {
$name = str_replace('mercure.hub.', '', $hubId);

$container->register("turbo.mercure.$name.renderer", TurboStreamListenRenderer::class)
->addArgument(new Reference($hubId))
->addArgument(new Reference('turbo.mercure.stimulus_helper'))
->addArgument(new Reference('turbo.id_accessor'))
->addArgument(new Reference('twig'))
->addTag('turbo.renderer.stream_listen', ['transport' => $name]);

if (isset($tag['default']) && $tag['default']) {
$container->getDefinition("turbo.mercure.{$name}.renderer")
->addTag('turbo.renderer.stream_listen', ['transport' => 'default']);
}

$container->register("turbo.mercure.$name.broadcaster", Broadcaster::class)
->addArgument($name)
->addArgument(new Reference($hubId))
->addTag('turbo.broadcaster');
}
}
}
3 changes: 3 additions & 0 deletions src/Turbo/src/TurboBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\UX\Turbo\DependencyInjection\Compiler\RegisterMercureHubs;

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

$container->addCompilerPass(new RegisterMercureHubs());

$container->addCompilerPass(new class implements CompilerPassInterface {
public function process(ContainerBuilder $container): void
{
Expand Down

0 comments on commit a59fd33

Please sign in to comment.