-
-
Notifications
You must be signed in to change notification settings - Fork 357
[Turbo] Pass EventSource options to turbo_stream_listen
#2447
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,25 +12,28 @@ | |
namespace Symfony\UX\Turbo\Bridge\Mercure; | ||
|
||
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\TurboStreamListenRendererInterface; | ||
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. | ||
* | ||
* @author Kévin Dunglas <kevin@dunglas.fr> | ||
*/ | ||
final class TurboStreamListenRenderer implements TurboStreamListenRendererInterface | ||
final class TurboStreamListenRenderer implements TurboStreamListenRendererWithOptionsInterface | ||
{ | ||
private StimulusHelper $stimulusHelper; | ||
|
||
public function __construct( | ||
private HubInterface $hub, | ||
StimulusHelper|StimulusTwigExtension $stimulus, | ||
private IdAccessor $idAccessor, | ||
private Environment $twig, | ||
) { | ||
if ($stimulus instanceof StimulusTwigExtension) { | ||
trigger_deprecation('symfony/ux-turbo', '2.9', 'Passing an instance of "%s" as second argument of "%s" is deprecated, pass an instance of "%s" instead.', StimulusTwigExtension::class, __CLASS__, StimulusHelper::class); | ||
|
@@ -42,8 +45,12 @@ public function __construct( | |
$this->stimulusHelper = $stimulus; | ||
} | ||
|
||
public function renderTurboStreamListen(Environment $env, $topic): string | ||
public function renderTurboStreamListen(Environment $env, $topic /* array $eventSourceOptions = [] */): string | ||
{ | ||
if (\func_num_args() > 2) { | ||
$eventSourceOptions = func_get_arg(2); | ||
} | ||
|
||
$topics = $topic instanceof TopicSet | ||
? array_map($this->resolveTopic(...), $topic->getTopics()) | ||
: [$this->resolveTopic($topic)]; | ||
|
@@ -55,6 +62,18 @@ public function renderTurboStreamListen(Environment $env, $topic): string | |
$controllerAttributes['topic'] = current($topics); | ||
} | ||
|
||
if (isset($eventSourceOptions)) { | ||
try { | ||
$mercure = $this->twig->getExtension(MercureExtension::class); | ||
$mercure->mercure($topics, $eventSourceOptions); | ||
|
||
if (isset($eventSourceOptions['withCredentials'])) { | ||
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials']; | ||
} | ||
} catch (RuntimeError $e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be thrown by |
||
} | ||
} | ||
|
||
$stimulusAttributes = $this->stimulusHelper->createStimulusAttributes(); | ||
$stimulusAttributes->addController( | ||
'symfony/ux-turbo/mercure-turbo-stream', | ||
|
53 changes: 53 additions & 0 deletions
53
src/Turbo/src/DependencyInjection/Compiler/RegisterMercureHubsPass.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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<pierre27.ambroise@gmail.com> | ||
*/ | ||
final class RegisterMercureHubsPass implements CompilerPassInterface | ||
{ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
foreach ($container->findTaggedServiceIds('mercure.hub') as $hubId => $tags) { | ||
$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]); | ||
|
||
foreach ($tags as $tag) { | ||
if (isset($tag['default']) && $tag['default'] && 'default' !== $name) { | ||
$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'); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Twig; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\UX\Turbo\Bridge\Mercure\TopicSet; | ||
use Twig\Environment; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
|
||
/** | ||
* @author Kévin Dunglas <kevin@dunglas.fr> | ||
* @author Pierre Ambroise <pierre27.ambroise@gmail.com> | ||
* | ||
* @internal | ||
*/ | ||
final class TurboRuntime implements RuntimeExtensionInterface | ||
{ | ||
public function __construct( | ||
private ContainerInterface $turboStreamListenRenderers, | ||
private readonly string $defaultTransport, | ||
) { | ||
} | ||
|
||
/** | ||
* @param object|string|array<object|string> $topic | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function renderTurboStreamListen(Environment $env, $topic, ?string $transport = null, array $options = []): string | ||
{ | ||
$options['transport'] = $transport ??= $this->defaultTransport; | ||
|
||
if (!$this->turboStreamListenRenderers->has($transport)) { | ||
throw new \InvalidArgumentException(\sprintf('The Turbo stream transport "%s" does not exist.', $transport)); | ||
} | ||
|
||
if (\is_array($topic)) { | ||
$topic = new TopicSet($topic); | ||
} | ||
|
||
$renderer = $this->turboStreamListenRenderers->get($transport); | ||
|
||
return $renderer instanceof TurboStreamListenRendererWithOptionsInterface | ||
? $renderer->renderTurboStreamListen($env, $topic, $options) // @phpstan-ignore-line | ||
: $renderer->renderTurboStreamListen($env, $topic); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/Turbo/src/Twig/TurboStreamListenRendererWithOptionsInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\UX\Turbo\Twig; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface TurboStreamListenRendererWithOptionsInterface extends TurboStreamListenRendererInterface | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.