22
33namespace Illuminate \Broadcasting \Broadcasters ;
44
5+ use Exception ;
6+ use ReflectionClass ;
57use ReflectionFunction ;
68use Illuminate \Support \Str ;
79use Illuminate \Container \Container ;
@@ -56,6 +58,7 @@ protected function verifyUserCanAccessChannel($request, $channel)
5658 }
5759
5860 $ parameters = $ this ->extractAuthParameters ($ pattern , $ channel , $ callback );
61+
5962 $ handler = $ this ->normalizeChannelHandlerToCallable ($ callback );
6063
6164 if ($ result = $ handler ($ request ->user (), ...$ parameters )) {
@@ -71,7 +74,7 @@ protected function verifyUserCanAccessChannel($request, $channel)
7174 *
7275 * @param string $pattern
7376 * @param string $channel
74- * @param callable $callback
77+ * @param callable|string $callback
7578 * @return array
7679 */
7780 protected function extractAuthParameters ($ pattern , $ channel , $ callback )
@@ -88,23 +91,37 @@ protected function extractAuthParameters($pattern, $channel, $callback)
8891 /**
8992 * Extracts the parameters out of what the user passed to handle the channel authentication.
9093 *
91- * @param callable|string $callback
94+ * @param callable|string $callback
9295 * @return \ReflectionParameter[]
9396 * @throws \Exception
9497 */
9598 protected function extractParameters ($ callback )
9699 {
97100 if (is_callable ($ callback )) {
98101 return (new ReflectionFunction ($ callback ))->getParameters ();
102+ } elseif (is_string ($ callback )) {
103+ return $ this ->extractParametersFromClass ($ callback );
99104 }
100105
101- if (is_string ($ callback )) {
102- return (new \ReflectionClass ($ callback ))
103- ->getMethod ('join ' )
104- ->getParameters ();
106+ throw new Exception ('Given channel handler is an unknown type. ' );
107+ }
108+
109+ /**
110+ * Extracts the parameters out of a class channel's "join" method.
111+ *
112+ * @param string $callback
113+ * @return \ReflectionParameter[]
114+ * @throws \Exception
115+ */
116+ protected function extractParametersFromClass ($ callback )
117+ {
118+ $ reflection = new ReflectionClass ($ callback );
119+
120+ if (! $ reflection ->hasMethod ('join ' )) {
121+ throw new Exception ('Class based channel must define a "join" method. ' );
105122 }
106123
107- throw new \ Exception ( ' Unknown channel handler type. ' );
124+ return $ reflection -> getMethod ( ' join ' )-> getParameters ( );
108125 }
109126
110127 /**
@@ -226,17 +243,17 @@ protected function binder()
226243 }
227244
228245 /**
229- * @param $callback
246+ * Normalize the given callback into a callable.
247+ *
248+ * @param mixed $callback
230249 * @return callable|\Closure
231250 */
232251 protected function normalizeChannelHandlerToCallable ($ callback )
233252 {
234- return is_callable ($ callback )
235- ? $ callback
236- : function (...$ args ) use ($ callback ) {
237- return Container::getInstance ()
238- ->make ($ callback )
239- ->join (...$ args );
240- };
253+ return is_callable ($ callback ) ? $ callback : function (...$ args ) use ($ callback ) {
254+ return Container::getInstance ()
255+ ->make ($ callback )
256+ ->join (...$ args );
257+ };
241258 }
242259}
0 commit comments