diff --git a/src/Turbo/config/services.php b/src/Turbo/config/services.php index a9de14c32e..2eaf1f27e6 100644 --- a/src/Turbo/config/services.php +++ b/src/Turbo/config/services.php @@ -11,7 +11,6 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; -use Symfony\Component\Mercure\Authorization; use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface; use Symfony\UX\Turbo\Broadcaster\IdAccessor; use Symfony\UX\Turbo\Broadcaster\ImuxBroadcaster; @@ -53,8 +52,6 @@ ->args([ tagged_locator('turbo.renderer.stream_listen', 'transport'), abstract_arg('default'), - service(Authorization::class)->nullOnInvalid(), - service('request_stack')->nullOnInvalid(), ]) ->tag('twig.runtime') diff --git a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php index 249038c648..b9123ba3cd 100644 --- a/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php +++ b/src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php @@ -11,10 +11,12 @@ namespace Symfony\UX\Turbo\Bridge\Mercure; +use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Mercure\Authorization; use Symfony\Component\Mercure\HubInterface; 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; @@ -23,7 +25,7 @@ * * @author Kévin Dunglas */ -final class TurboStreamListenRenderer implements TurboStreamListenRendererInterface +final class TurboStreamListenRenderer implements TurboStreamListenRendererWithOptionsInterface { private StimulusHelper $stimulusHelper; @@ -31,6 +33,8 @@ public function __construct( private HubInterface $hub, StimulusHelper|StimulusTwigExtension $stimulus, private IdAccessor $idAccessor, + private ?Authorization $authorization = null, + private ?RequestStack $requestStack = null, ) { 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); @@ -59,8 +63,27 @@ public function renderTurboStreamListen(Environment $env, $topic /* array $event $controllerAttributes['topic'] = current($topics); } - if (isset($eventSourceOptions, $eventSourceOptions['withCredentials'])) { - $controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials']; + if (isset($eventSourceOptions)) { + if ( + null !== $this->authorization + && null !== $this->requestStack + && (isset($eventSourceOptions['subscribe']) || isset($eventSourceOptions['publish']) || isset($eventSourceOptions['additionalClaims'])) + && null !== $request = $this->requestStack->getMainRequest() + ) { + $this->authorization->setCookie( + $request, + $eventSourceOptions['subscribe'] ?? [], + $eventSourceOptions['publish'] ?? [], + $eventSourceOptions['additionalClaims'] ?? [], + $eventSourceOptions['transport'] ?? null, + ); + + unset($eventSourceOptions['subscribe'], $eventSourceOptions['publish'], $eventSourceOptions['additionalClaims'], $eventSourceOptions['transport']); + } + + if (isset($eventSourceOptions['withCredentials'])) { + $controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials']; + } } $stimulusAttributes = $this->stimulusHelper->createStimulusAttributes(); diff --git a/src/Turbo/src/Twig/TurboRuntime.php b/src/Turbo/src/Twig/TurboRuntime.php index 681a3627a1..2f512100b3 100644 --- a/src/Turbo/src/Twig/TurboRuntime.php +++ b/src/Turbo/src/Twig/TurboRuntime.php @@ -12,8 +12,6 @@ namespace Symfony\UX\Turbo\Twig; use Psr\Container\ContainerInterface; -use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\Mercure\Authorization; use Symfony\UX\Turbo\Bridge\Mercure\TopicSet; use Twig\Environment; use Twig\Extension\RuntimeExtensionInterface; @@ -28,8 +26,6 @@ class TurboRuntime implements RuntimeExtensionInterface public function __construct( private ContainerInterface $turboStreamListenRenderers, private string $default, - private ?Authorization $authorization = null, - private ?RequestStack $requestStack = null, ) { } @@ -39,7 +35,7 @@ public function __construct( */ public function renderTurboStreamListen(Environment $env, $topic, ?string $transport = null, array $options = []): string { - $transport ??= $this->default; + $options['transport'] = $transport ??= $this->default; if (!$this->turboStreamListenRenderers->has($transport)) { throw new \InvalidArgumentException(\sprintf('The Turbo stream transport "%s" does not exist.', $transport)); @@ -49,23 +45,10 @@ public function renderTurboStreamListen(Environment $env, $topic, ?string $trans $topic = new TopicSet($topic); } - if ( - null !== $this->authorization - && null !== $this->requestStack - && (isset($options['subscribe']) || isset($options['publish']) || isset($options['additionalClaims'])) - && null !== $request = $this->requestStack->getMainRequest() - ) { - $this->authorization->setCookie( - $request, - $options['subscribe'] ?? [], - $options['publish'] ?? [], - $options['additionalClaims'] ?? [], - $transport, - ); + $renderer = $this->turboStreamListenRenderers->get($transport); - unset($options['subscribe'], $options['publish'], $options['additionalClaims']); - } - - return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic, $options); + return $renderer instanceof TurboStreamListenRendererWithOptionsInterface + ? $renderer->renderTurboStreamListen($env, $topic, $options) // @phpstan-ignore-line + : $renderer->renderTurboStreamListen($env, $topic); } } diff --git a/src/Turbo/src/Twig/TurboStreamListenRendererWithOptionsInterface.php b/src/Turbo/src/Twig/TurboStreamListenRendererWithOptionsInterface.php new file mode 100644 index 0000000000..6364fe3b97 --- /dev/null +++ b/src/Turbo/src/Twig/TurboStreamListenRendererWithOptionsInterface.php @@ -0,0 +1,19 @@ + + * + * 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 +{ +} diff --git a/src/Turbo/src/Twig/TwigExtension.php b/src/Turbo/src/Twig/TwigExtension.php index 55e785968a..789164254f 100644 --- a/src/Turbo/src/Twig/TwigExtension.php +++ b/src/Turbo/src/Twig/TwigExtension.php @@ -16,7 +16,6 @@ /** * @author Kévin Dunglas - * @author Pierre Ambroise */ final class TwigExtension extends AbstractExtension {