-
-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
59 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,19 +12,24 @@ | |
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\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
/** | ||
* @author Kévin Dunglas <[email protected]> | ||
* @author Pierre Ambroise <[email protected]> | ||
*/ | ||
final class TwigExtension extends AbstractExtension | ||
{ | ||
public function __construct( | ||
private ContainerInterface $turboStreamListenRenderers, | ||
private string $default, | ||
private ?Authorization $authorization = null, | ||
private ?RequestStack $requestStack = null, | ||
) { | ||
} | ||
|
||
|
@@ -37,8 +42,9 @@ public function getFunctions(): array | |
|
||
/** | ||
* @param object|string|array<object|string> $topic | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function turboStreamListen(Environment $env, $topic, ?string $transport = null): string | ||
public function turboStreamListen(Environment $env, $topic, ?string $transport = null, array $options = []): string | ||
{ | ||
$transport ??= $this->default; | ||
|
||
|
@@ -50,6 +56,24 @@ public function turboStreamListen(Environment $env, $topic, ?string $transport = | |
$topic = new TopicSet($topic); | ||
} | ||
|
||
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic); | ||
if ( | ||
null !== $this->authorization | ||
&& null !== $this->requestStack | ||
&& (isset($options['subscribe']) || isset($options['publish']) || isset($options['additionalClaims'])) | ||
// @phpstan-ignore-next-line | ||
&& null !== $request = method_exists($this->requestStack, 'getMainRequest') ? $this->requestStack->getMainRequest() : $this->requestStack->getMasterRequest() | ||
) { | ||
$this->authorization->setCookie( | ||
$request, | ||
$options['subscribe'] ?? [], | ||
$options['publish'] ?? [], | ||
$options['additionalClaims'] ?? [], | ||
$transport, | ||
); | ||
|
||
unset($options['subscribe'], $options['publish'], $options['additionalClaims']); | ||
} | ||
|
||
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters