@@ -43,6 +43,30 @@ public function __construct(
4343 }
4444
4545 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
4670 {
4771 if (\is_object ($ topic )) {
4872 $ class = $ topic ::class;
@@ -51,18 +75,14 @@ public function renderTurboStreamListen(Environment $env, $topic): string
5175 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 ));
5276 }
5377
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 )));
5879 }
5980
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+ }
6585
66- return ( string ) $ stimulusAttributes ;
86+ return $ topic ;
6787 }
6888}
0 commit comments