@@ -43,6 +43,30 @@ public function __construct(
43
43
}
44
44
45
45
public function renderTurboStreamListen (Environment $ env , $ topic ): string
46
+ {
47
+ if ($ topic instanceof TopicSet) {
48
+ $ topics = array_map (\Closure::fromCallable ([$ this , 'resolveTopic ' ]), $ topic ->getTopics ());
49
+ } else {
50
+ $ topics [] = $ this ->resolveTopic ($ topic );
51
+ }
52
+
53
+ $ controllerAttributes = ['hub ' => $ this ->hub ->getPublicUrl ()];
54
+ if (1 < \count ($ topics )) {
55
+ $ controllerAttributes ['topics ' ] = $ topics ;
56
+ } else {
57
+ $ controllerAttributes ['topic ' ] = current ($ topics );
58
+ }
59
+
60
+ $ stimulusAttributes = $ this ->stimulusHelper ->createStimulusAttributes ();
61
+ $ stimulusAttributes ->addController (
62
+ 'symfony/ux-turbo/mercure-turbo-stream ' ,
63
+ $ controllerAttributes ,
64
+ );
65
+
66
+ return (string ) $ stimulusAttributes ;
67
+ }
68
+
69
+ private function resolveTopic (object |string $ topic ): string
46
70
{
47
71
if (\is_object ($ topic )) {
48
72
$ class = $ topic ::class;
@@ -51,18 +75,14 @@ public function renderTurboStreamListen(Environment $env, $topic): string
51
75
throw new \LogicException (\sprintf ('Cannot listen to entity of class "%s" as the PropertyAccess component is not installed. Try running "composer require symfony/property-access". ' , $ class ));
52
76
}
53
77
54
- $ topic = \sprintf (Broadcaster::TOPIC_PATTERN , rawurlencode ($ class ), rawurlencode (implode ('- ' , $ id )));
55
- } elseif (!preg_match ('/[^a-zA-Z0-9_\x7f-\xff \\\\]/ ' , $ topic ) && class_exists ($ topic )) {
56
- // Generate a URI template to subscribe to updates for all objects of this class
57
- $ topic = \sprintf (Broadcaster::TOPIC_PATTERN , rawurlencode ($ topic ), '{id} ' );
78
+ return \sprintf (Broadcaster::TOPIC_PATTERN , rawurlencode ($ class ), rawurlencode (implode ('- ' , $ id )));
58
79
}
59
80
60
- $ stimulusAttributes = $ this ->stimulusHelper ->createStimulusAttributes ();
61
- $ stimulusAttributes ->addController (
62
- 'symfony/ux-turbo/mercure-turbo-stream ' ,
63
- ['topic ' => $ topic , 'hub ' => $ this ->hub ->getPublicUrl ()]
64
- );
81
+ if (!preg_match ('/[^a-zA-Z0-9_\x7f-\xff \\\\]/ ' , $ topic ) && class_exists ($ topic )) {
82
+ // Generate a URI template to subscribe to updates for all objects of this class
83
+ return \sprintf (Broadcaster::TOPIC_PATTERN , rawurlencode ($ topic ), '{id} ' );
84
+ }
65
85
66
- return ( string ) $ stimulusAttributes ;
86
+ return $ topic ;
67
87
}
68
88
}
0 commit comments