12
12
namespace Symfony \UX \Turbo \Bridge \Mercure ;
13
13
14
14
use Symfony \Component \Mercure \HubInterface ;
15
+ use Symfony \Component \Mercure \Twig \MercureExtension ;
15
16
use Symfony \UX \StimulusBundle \Helper \StimulusHelper ;
16
17
use Symfony \UX \Turbo \Broadcaster \IdAccessor ;
17
- use Symfony \UX \Turbo \Twig \TurboStreamListenRendererInterface ;
18
+ use Symfony \UX \Turbo \Twig \TurboStreamListenRendererWithOptionsInterface ;
18
19
use Symfony \WebpackEncoreBundle \Twig \StimulusTwigExtension ;
19
20
use Twig \Environment ;
21
+ use Twig \Error \RuntimeError ;
20
22
21
23
/**
22
24
* Renders the attributes to load the "mercure-turbo-stream" controller.
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 Environment $ twig ,
34
37
) {
35
38
if ($ stimulus instanceof StimulusTwigExtension) {
36
39
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 +45,12 @@ public function __construct(
42
45
$ this ->stimulusHelper = $ stimulus ;
43
46
}
44
47
45
- public function renderTurboStreamListen (Environment $ env , $ topic ): string
48
+ public function renderTurboStreamListen (Environment $ env , $ topic /* array $eventSourceOptions = [] */ ): string
46
49
{
50
+ if (\func_num_args () > 2 ) {
51
+ $ eventSourceOptions = func_get_arg (2 );
52
+ }
53
+
47
54
$ topics = $ topic instanceof TopicSet
48
55
? array_map ($ this ->resolveTopic (...), $ topic ->getTopics ())
49
56
: [$ this ->resolveTopic ($ topic )];
@@ -55,6 +62,18 @@ public function renderTurboStreamListen(Environment $env, $topic): string
55
62
$ controllerAttributes ['topic ' ] = current ($ topics );
56
63
}
57
64
65
+ if (isset ($ eventSourceOptions )) {
66
+ try {
67
+ $ mercure = $ this ->twig ->getExtension (MercureExtension::class);
68
+ $ mercure ->mercure ($ topics , $ eventSourceOptions );
69
+
70
+ if (isset ($ eventSourceOptions ['withCredentials ' ])) {
71
+ $ controllerAttributes ['withCredentials ' ] = $ eventSourceOptions ['withCredentials ' ];
72
+ }
73
+ } catch (RuntimeError $ e ) {
74
+ }
75
+ }
76
+
58
77
$ stimulusAttributes = $ this ->stimulusHelper ->createStimulusAttributes ();
59
78
$ stimulusAttributes ->addController (
60
79
'symfony/ux-turbo/mercure-turbo-stream ' ,
0 commit comments