Skip to content

Commit 29a15e5

Browse files
mvrhovsroze
authored andcommitted
Add return to dispatch method and no need for handles parameter. We can find automatically the Message handler is supposed to handle
1 parent 099b423 commit 29a15e5

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/Symfony/Component/Message/DependencyInjection/MessagePass.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/Symfony/Component/Message/MessageBus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(array $middlewares = array())
3535
*/
3636
public function dispatch($message)
3737
{
38-
call_user_func($this->callableForNextMiddleware(0), $message);
38+
return call_user_func($this->callableForNextMiddleware(0), $message);
3939
}
4040

4141
private function callableForNextMiddleware($index): callable

src/Symfony/Component/Message/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class MyMessageHandler
7272

7373
```xml
7474
<service id="App\Handler\MyMessageHandler">
75-
<tag name="message_handler" handles="App\Message\MyMessage" />
75+
<tag name="message_handler" />
7676
</service>
7777
```
7878

0 commit comments

Comments
 (0)