-
-
Notifications
You must be signed in to change notification settings - Fork 324
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
11 changed files
with
115 additions
and
34 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?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; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
use Symfony\Component\Mercure\Authorization; | ||
use Symfony\UX\Turbo\Bridge\Mercure\TopicSet; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
use Twig\Environment; | ||
|
||
/** | ||
* @author Pierre Ambroise <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
class TurboRuntime implements RuntimeExtensionInterface | ||
{ | ||
public function __construct( | ||
private ContainerInterface $turboStreamListenRenderers, | ||
private string $default, | ||
private ?Authorization $authorization = null, | ||
private ?RequestStack $requestStack = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @param object|string|array<object|string> $topic | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function renderTurboStreamListen(Environment $env, $topic, ?string $transport = null, array $options = []): string | ||
{ | ||
$transport ??= $this->default; | ||
|
||
if (!$this->turboStreamListenRenderers->has($transport)) { | ||
throw new \InvalidArgumentException(\sprintf('The Turbo stream transport "%s" does not exist.', $transport)); | ||
} | ||
|
||
if (\is_array($topic)) { | ||
$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, | ||
); | ||
|
||
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
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 |
---|---|---|
|
@@ -11,45 +11,20 @@ | |
|
||
namespace Symfony\UX\Turbo\Twig; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use Symfony\UX\Turbo\Bridge\Mercure\TopicSet; | ||
use Twig\Environment; | ||
use Symfony\UX\Turbo\Twig\TurboRuntime; | ||
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, | ||
) { | ||
} | ||
|
||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('turbo_stream_listen', $this->turboStreamListen(...), ['needs_environment' => true, 'is_safe' => ['html']]), | ||
new TwigFunction('turbo_stream_listen', [TurboRuntime::class, 'renderTurboStreamListen'], ['needs_environment' => true, 'is_safe' => ['html']]), | ||
]; | ||
} | ||
|
||
/** | ||
* @param object|string|array<object|string> $topic | ||
*/ | ||
public function turboStreamListen(Environment $env, $topic, ?string $transport = null): string | ||
{ | ||
$transport ??= $this->default; | ||
|
||
if (!$this->turboStreamListenRenderers->has($transport)) { | ||
throw new \InvalidArgumentException(\sprintf('The Turbo stream transport "%s" does not exist.', $transport)); | ||
} | ||
|
||
if (\is_array($topic)) { | ||
$topic = new TopicSet($topic); | ||
} | ||
|
||
return $this->turboStreamListenRenderers->get($transport)->renderTurboStreamListen($env, $topic); | ||
} | ||
} |
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