|
11 | 11 |
|
12 | 12 | namespace Symfony\UX\Turbo\Bridge\Mercure;
|
13 | 13 |
|
| 14 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 15 | +use Symfony\Component\Mercure\Authorization; |
14 | 16 | use Symfony\Component\Mercure\HubInterface;
|
15 | 17 | use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
|
16 | 18 | use Symfony\UX\Turbo\Broadcaster\IdAccessor;
|
17 |
| -use Symfony\UX\Turbo\Twig\TurboStreamListenRendererInterface; |
| 19 | +use Symfony\UX\Turbo\Twig\TurboStreamListenRendererWithOptionsInterface; |
18 | 20 | use Symfony\WebpackEncoreBundle\Twig\StimulusTwigExtension;
|
19 | 21 | use Twig\Environment;
|
20 | 22 |
|
|
23 | 25 | *
|
24 | 26 | * @author Kévin Dunglas <[email protected]>
|
25 | 27 | */
|
26 |
| -final class TurboStreamListenRenderer implements TurboStreamListenRendererInterface |
| 28 | +final class TurboStreamListenRenderer implements TurboStreamListenRendererWithOptionsInterface |
27 | 29 | {
|
28 | 30 | private StimulusHelper $stimulusHelper;
|
29 | 31 |
|
30 | 32 | public function __construct(
|
31 | 33 | private HubInterface $hub,
|
32 | 34 | StimulusHelper|StimulusTwigExtension $stimulus,
|
33 | 35 | private IdAccessor $idAccessor,
|
| 36 | + private ?Authorization $authorization = null, |
| 37 | + private ?RequestStack $requestStack = null, |
34 | 38 | ) {
|
35 | 39 | if ($stimulus instanceof StimulusTwigExtension) {
|
36 | 40 | 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
|
59 | 63 | $controllerAttributes['topic'] = current($topics);
|
60 | 64 | }
|
61 | 65 |
|
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 | + } |
64 | 87 | }
|
65 | 88 |
|
66 | 89 | $stimulusAttributes = $this->stimulusHelper->createStimulusAttributes();
|
|
0 commit comments