@@ -59,12 +59,29 @@ private function findHandlers(ContainerBuilder $container): array
5959
6060 foreach ($ container ->findTaggedServiceIds ($ this ->handlerTag , true ) as $ serviceId => $ tags ) {
6161 foreach ($ tags as $ tag ) {
62- if (!isset ($ tag ['handles ' ])) {
63- throw new RuntimeException (sprintf ('Tag "%s" on service "%s" should have an `handles` attribute ' , $ this ->handlerTag , $ serviceId ));
62+ $ reflection = new \ReflectionClass ($ container ->getDefinition ($ serviceId )->getClass ());
63+
64+ try {
65+ $ method = $ reflection ->getMethod ('__invoke ' );
66+ } catch (\ReflectionException $ e ) {
67+ throw new RuntimeException (sprintf ('Service "%s" should have an `__invoke` function ' , $ serviceId ));
68+ }
69+
70+ $ parameters = $ method ->getParameters ();
71+ if (count ($ parameters ) !== 1 ) {
72+ throw new RuntimeException (sprintf ('`__invoke` function of service "%s" must have exactly one parameter ' , $ serviceId ));
73+ }
74+
75+ $ parameter = $ parameters [0 ];
76+ if (null === $ parameter ->getClass ()) {
77+ throw new RuntimeException (sprintf ('The parameter of `__invoke` function of service "%s" must type hint the Message class it handles ' , $ serviceId ));
78+ }
79+ if (!class_exists ($ handles = $ parameter ->getClass ()->getName ())) {
80+ throw new RuntimeException (sprintf ('The message class "%s" declared in `__invoke` function of service "%s" does not exist ' , $ handles , $ serviceId ));
6481 }
6582
6683 $ priority = isset ($ tag ['priority ' ]) ? $ tag ['priority ' ] : 0 ;
67- $ handlersByMessage [$ tag [ ' handles ' ] ][$ priority ][] = new Reference ($ serviceId );
84+ $ handlersByMessage [$ handles ][$ priority ][] = new Reference ($ serviceId );
6885 }
6986 }
7087
0 commit comments