Skip to content

Commit 8da6ada

Browse files
authored
Allowing use with WebpackEncoreBundle or StimulusBundle (#80)
* Allowing use with WebpackEncoreBundle or StimulusBundle * swapping order so older one takes precedence if installed
1 parent 262c922 commit 8da6ada

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
0.4.0
5+
-----
6+
7+
* Support for `symfony/ux-turbo` 2.9 using `symfony/stimulus-bundle`
8+
49
0.3.6
510
-----
611

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Mercure Component project.
5+
*
6+
* (c) Kévin Dunglas <[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+
declare(strict_types=1);
13+
14+
namespace Symfony\Bundle\MercureBundle\DependencyInjection\CompilerPass;
15+
16+
use Symfony\Component\DependencyInjection\Alias;
17+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\UX\Turbo\Bridge\Mercure\Broadcaster;
20+
21+
/**
22+
* Registers a dynamic alias to use a service from WebpackEncoreBundle or StimulusBundle.
23+
*
24+
* Depending on the version of symfony/ux-turbo installed, one of these bundles
25+
* will be available.
26+
*/
27+
final class StimulusHelperPass implements CompilerPassInterface
28+
{
29+
public function process(ContainerBuilder $container)
30+
{
31+
if (!class_exists(Broadcaster::class)) {
32+
return;
33+
}
34+
35+
if ($container->hasDefinition('webpack_encore.twig_stimulus_extension')) {
36+
$id = 'webpack_encore.twig_stimulus_extension';
37+
} else {
38+
$id = 'stimulus.helper';
39+
}
40+
$container->setAlias('turbo.mercure.stimulus_helper', new Alias($id));
41+
}
42+
}

src/DependencyInjection/MercureExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ public function load(array $configs, ContainerBuilder $container): void
213213
if (class_exists(Broadcaster::class)) {
214214
$container->register("turbo.mercure.{$name}.renderer", TurboStreamListenRenderer::class)
215215
->addArgument(new Reference($hubId))
216-
->addArgument(new Reference('webpack_encore.twig_stimulus_extension'))
216+
// uses an alias dynamically registered in a compiler pass
217+
->addArgument(new Reference('turbo.mercure.stimulus_helper'))
217218
->addArgument(new Reference('turbo.id_accessor'))
218219
->addTag('turbo.renderer.stream_listen', ['transport' => $name]);
219220

src/MercureBundle.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace Symfony\Bundle\MercureBundle;
1515

16+
use Symfony\Bundle\MercureBundle\DependencyInjection\CompilerPass\StimulusHelperPass;
1617
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1718
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -28,6 +29,8 @@ public function build(ContainerBuilder $container): void
2829
{
2930
parent::build($container);
3031

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

0 commit comments

Comments
 (0)