Skip to content

Commit 583aab6

Browse files
committed
Use mercure extension for creating the cookie
1 parent 961ef08 commit 583aab6

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

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

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpFoundation\RequestStack;
1515
use Symfony\Component\Mercure\Authorization;
1616
use Symfony\Component\Mercure\HubInterface;
17+
use Symfony\Component\Mercure\Twig\MercureExtension;
1718
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
1819
use Symfony\UX\Turbo\Broadcaster\IdAccessor;
1920
use Symfony\UX\Turbo\Twig\TurboStreamListenRendererWithOptionsInterface;
@@ -33,8 +34,7 @@ public function __construct(
3334
private HubInterface $hub,
3435
StimulusHelper|StimulusTwigExtension $stimulus,
3536
private IdAccessor $idAccessor,
36-
private ?Authorization $authorization = null,
37-
private ?RequestStack $requestStack = null,
37+
private Environment $twig,
3838
) {
3939
if ($stimulus instanceof StimulusTwigExtension) {
4040
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);
@@ -64,25 +64,12 @@ public function renderTurboStreamListen(Environment $env, $topic /* array $event
6464
}
6565

6666
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-
}
67+
if ($mercure = $this->twig->getExtension(MercureExtension::class)) {
68+
$mercure->mercure($topic, $eventSourceOptions);
8369

84-
if (isset($eventSourceOptions['withCredentials'])) {
85-
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
70+
if (isset($eventSourceOptions['withCredentials'])) {
71+
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
72+
}
8673
}
8774
}
8875

0 commit comments

Comments
 (0)