-
-
Notifications
You must be signed in to change notification settings - Fork 324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add options to turbo_stream_listen
#2447
Open
Fan2Shrek
wants to merge
2
commits into
symfony:2.x
Choose a base branch
from
Fan2Shrek:add-option-to-turbo-stream
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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); | ||
|
@@ -42,8 +46,12 @@ public function __construct( | |
$this->stimulusHelper = $stimulus; | ||
} | ||
|
||
public function renderTurboStreamListen(Environment $env, $topic): string | ||
public function renderTurboStreamListen(Environment $env, $topic /* array $eventSourceOptions = [] */): string | ||
{ | ||
if (\func_num_args() > 2) { | ||
$eventSourceOptions = func_get_arg(2); | ||
} | ||
|
||
$topics = $topic instanceof TopicSet | ||
? array_map($this->resolveTopic(...), $topic->getTopics()) | ||
: [$this->resolveTopic($topic)]; | ||
|
@@ -55,6 +63,29 @@ public function renderTurboStreamListen(Environment $env, $topic): string | |
$controllerAttributes['topic'] = current($topics); | ||
} | ||
|
||
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'], $eventSourceOptions['transport']); | ||
} | ||
|
||
if (isset($eventSourceOptions['withCredentials'])) { | ||
$controllerAttributes['withCredentials'] = $eventSourceOptions['withCredentials']; | ||
} | ||
} | ||
|
||
$stimulusAttributes = $this->stimulusHelper->createStimulusAttributes(); | ||
$stimulusAttributes->addController( | ||
'symfony/ux-turbo/mercure-turbo-stream', | ||
|
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,54 @@ | ||
<?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\UX\Turbo\Bridge\Mercure\TopicSet; | ||
use Twig\Environment; | ||
use Twig\Extension\RuntimeExtensionInterface; | ||
|
||
/** | ||
* @author Pierre Ambroise <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
class TurboRuntime implements RuntimeExtensionInterface | ||
{ | ||
public function __construct( | ||
private ContainerInterface $turboStreamListenRenderers, | ||
private string $default, | ||
) { | ||
} | ||
|
||
/** | ||
* @param object|string|array<object|string> $topic | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function renderTurboStreamListen(Environment $env, $topic, ?string $transport = null, array $options = []): string | ||
{ | ||
$options['transport'] = $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); | ||
} | ||
|
||
$renderer = $this->turboStreamListenRenderers->get($transport); | ||
|
||
return $renderer instanceof TurboStreamListenRendererWithOptionsInterface | ||
? $renderer->renderTurboStreamListen($env, $topic, $options) // @phpstan-ignore-line | ||
: $renderer->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
19 changes: 19 additions & 0 deletions
19
src/Turbo/src/Twig/TurboStreamListenRendererWithOptionsInterface.php
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,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 | ||
{ | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will throw if the method is called twice during the render of the page.
What are the goal of the various options here ?
Or, say differently, why not use the mercure extension here ? (real question i'm not sure to get)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean the Mercure Twig extension?
If so, it doesn't seem to work 😅. The dependency isn't being resolved, and to be honest, I'm not sure why.