Skip to content

Commit

Permalink
Try to prevent BC
Browse files Browse the repository at this point in the history
  • Loading branch information
Fan2Shrek committed Dec 14, 2024
1 parent e95cbab commit f92e643
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 30 deletions.
3 changes: 0 additions & 3 deletions src/Turbo/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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')

Expand Down
31 changes: 27 additions & 4 deletions src/Turbo/src/Bridge/Mercure/TurboStreamListenRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -23,14 +25,16 @@
*
* @author Kévin Dunglas <[email protected]>
*/
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 ?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);
Expand Down Expand Up @@ -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']);
}

if (isset($eventSourceOptions['withCredentials'])) {
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials'];
}
}

$stimulusAttributes = $this->stimulusHelper->createStimulusAttributes();
Expand Down
27 changes: 5 additions & 22 deletions src/Turbo/src/Twig/TurboRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
) {
}

Expand All @@ -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));
Expand All @@ -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);
}
}
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 <[email protected]>
*
* 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
{
}
1 change: 0 additions & 1 deletion src/Turbo/src/Twig/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

/**
* @author Kévin Dunglas <[email protected]>
* @author Pierre Ambroise <[email protected]>
*/
final class TwigExtension extends AbstractExtension
{
Expand Down

0 comments on commit f92e643

Please sign in to comment.