Skip to content

Commit e56b74c

Browse files
committed
Try to prevent BC
1 parent e95cbab commit e56b74c

File tree

5 files changed

+51
-30
lines changed

5 files changed

+51
-30
lines changed

src/Turbo/config/services.php

-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14-
use Symfony\Component\Mercure\Authorization;
1514
use Symfony\UX\Turbo\Broadcaster\BroadcasterInterface;
1615
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
1716
use Symfony\UX\Turbo\Broadcaster\ImuxBroadcaster;
@@ -53,8 +52,6 @@
5352
->args([
5453
tagged_locator('turbo.renderer.stream_listen', 'transport'),
5554
abstract_arg('default'),
56-
service(Authorization::class)->nullOnInvalid(),
57-
service('request_stack')->nullOnInvalid(),
5855
])
5956
->tag('twig.runtime')
6057

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

+27-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111

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

14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
use Symfony\Component\Mercure\Authorization;
1416
use Symfony\Component\Mercure\HubInterface;
1517
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
1618
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
17-
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererInterface;
19+
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererWithOptionsInterface;
1820
use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
1921
use Twig\Environment;
2022

@@ -23,14 +25,16 @@
2325
*
2426
* @author Kévin Dunglas <[email protected]>
2527
*/
26-
final class TurboStreamListenRenderer implements TurboStreamListenRendererInterface
28+
final class TurboStreamListenRenderer implements TurboStreamListenRendererWithOptionsInterface
2729
{
2830
private StimulusHelper $stimulusHelper;
2931

3032
public function __construct(
3133
private HubInterface $hub,
3234
StimulusHelper|StimulusTwigExtension $stimulus,
3335
private IdAccessor $idAccessor,
36+
private ?Authorization $authorization = null,
37+
private ?RequestStack $requestStack = null,
3438
) {
3539
if ($stimulus instanceof StimulusTwigExtension) {
3640
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
5963
$controllerAttributes['topic'] = current($topics);
6064
}
6165

62-
if (isset($eventSourceOptions, $eventSourceOptions['withCredentials'])) {
63-
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
66+
if (isset($eventSourceOptions)) {
67+
if (
68+
null !== $this->authorization
69+
&& null !== $this->requestStack
70+
&& (isset($eventSourceOptions['subscribe']) || isset($eventSourceOptions['publish']) || isset($eventSourceOptions['additionalClaims']))
71+
&& null !== $request = $this->requestStack->getMainRequest()
72+
) {
73+
$this->authorization->setCookie(
74+
$request,
75+
$eventSourceOptions['subscribe'] ?? [],
76+
$eventSourceOptions['publish'] ?? [],
77+
$eventSourceOptions['additionalClaims'] ?? [],
78+
$eventSourceOptions['transport'] ?? null,
79+
);
80+
81+
unset($eventSourceOptions['subscribe'], $eventSourceOptions['publish'], $eventSourceOptions['additionalClaims'], $eventSourceOptions['transport']);
82+
}
83+
84+
if (isset($eventSourceOptions['withCredentials'])) {
85+
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
86+
}
6487
}
6588

6689
$stimulusAttributes = $this->stimulusHelper->createStimulusAttributes();

src/Turbo/src/Twig/TurboRuntime.php

+5-22
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace Symfony\UX\Turbo\Twig;
1313

1414
use Psr\Container\ContainerInterface;
15-
use Symfony\Component\HttpFoundation\RequestStack;
16-
use Symfony\Component\Mercure\Authorization;
1715
use Symfony\UX\Turbo\Bridge\Mercure\TopicSet;
1816
use Twig\Environment;
1917
use Twig\Extension\RuntimeExtensionInterface;
@@ -28,8 +26,6 @@ class TurboRuntime implements RuntimeExtensionInterface
2826
public function __construct(
2927
private ContainerInterface $turboStreamListenRenderers,
3028
private string $default,
31-
private ?Authorization $authorization = null,
32-
private ?RequestStack $requestStack = null,
3329
) {
3430
}
3531

@@ -39,7 +35,7 @@ public function __construct(
3935
*/
4036
public function renderTurboStreamListen(Environment $env, $topic, ?string $transport = null, array $options = []): string
4137
{
42-
$transport ??= $this->default;
38+
$options['transport'] = $transport ??= $this->default;
4339

4440
if (!$this->turboStreamListenRenderers->has($transport)) {
4541
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
4945
$topic = new TopicSet($topic);
5046
}
5147

52-
if (
53-
null !== $this->authorization
54-
&& null !== $this->requestStack
55-
&& (isset($options['subscribe']) || isset($options['publish']) || isset($options['additionalClaims']))
56-
&& null !== $request = $this->requestStack->getMainRequest()
57-
) {
58-
$this->authorization->setCookie(
59-
$request,
60-
$options['subscribe'] ?? [],
61-
$options['publish'] ?? [],
62-
$options['additionalClaims'] ?? [],
63-
$transport,
64-
);
48+
$renderer = $this->turboStreamListenRenderers->get($transport);
6549

66-
unset($options['subscribe'], $options['publish'], $options['additionalClaims']);
67-
}
68-
69-
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic, $options);
50+
return $renderer instanceof TurboStreamListenRendererWithOptionsInterface
51+
? $renderer->renderTurboStreamListen($env, $topic, $options) // @phpstan-ignore-line
52+
: $renderer->renderTurboStreamListen($env, $topic);
7053
}
7154
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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\Twig;
13+
14+
/**
15+
* @internal
16+
*/
17+
interface TurboStreamListenRendererWithOptionsInterface extends TurboStreamListenRendererInterface
18+
{
19+
}

src/Turbo/src/Twig/TwigExtension.php

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
/**
1818
* @author Kévin Dunglas <[email protected]>
19-
* @author Pierre Ambroise <[email protected]>
2019
*/
2120
final class TwigExtension extends AbstractExtension
2221
{

0 commit comments

Comments
 (0)