8000 Find The Message automatically by mvrhov · Pull Request #1 · sroze/symfony · GitHub
[go: up one dir, main page]

Skip to content

Find The Message automatically #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/Symfony/Component/Message/DependencyInjection/MessagePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,29 @@ private function findHandlers(ContainerBuilder $container): array

foreach ($container->findTaggedServiceIds($this->handlerTag, true) as $serviceId => $tags) {
foreach ($tags as $tag) {
if (!isset($tag['handles'])) {
throw new RuntimeException(sprintf('Tag "%s" on service "%s" should have an `handles` attribute', $this->handlerTag, $serviceId));
$reflection = new \ReflectionClass($container->getDefinition($serviceId)->getClass());

try {
$method = $reflection->getMethod('__invoke');
} catch (\ReflectionException $e) {
throw new RuntimeException(sprintf('Service "%s" should have an `__invoke` function', $serviceId));
}

$parameters = $method->getParameters();
if (count($parameters) !== 1) {
throw new RuntimeException(sprintf('`__invoke` function of service "%s" must have exactly one parameter', $serviceId));
}

$parameter = $parameters[0];
if (null === $parameter->getClass()) {
throw new RuntimeException(sprintf('The parameter of `__invoke` function of service "%s" must type hint the Message class it handles', $serviceId));
}
if (!class_exists($handles = $parameter->getClass()->getName())) {
throw new RuntimeException(sprintf('The message class "%s" declared in `__invoke` function of service "%s" does not exist', $handles, $serviceId));
}

$priority = isset($tag['priority']) ? $tag['priority'] : 0;
$handlersByMessage[$tag['handles']][$priority][] = new Reference($serviceId);
$handlersByMessage[$handles][$priority][] = new Reference($serviceId);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Message/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function __construct(array $middlewares = array())
*/
public function dispatch($message)
{
call_user_func($this->callableForNextMiddleware(0), $message);
return call_user_func($this->callableForNextMiddleware(0), $message);
}

private function callableForNextMiddleware($index): callable
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Message/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class MyMessageHandler

```xml
<service id="App\Handler\MyMessageHandler">
<tag name="message_handler" handles="App\Message\MyMessage" />
<tag name="message_handler" />
</service>
```

Expand Down
0