minor #29525 [Messenger] Make MessengerPass less strict when auto-register handlers (nicholasruunu)

This PR was merged into the 4.3-dev branch.

Discussion
----------

[Messenger] Make MessengerPass less strict when auto-register handlers

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| License       | MIT
| Doc PR        | not needed

This allows you to auto-register handlers that have more than one argument, which is useful when having custom middleware to pass more parameters.

#symfonyconhackday2018

Commits
-------

49ab2cd9d9 Make MessengerPass less strict when auto-register handlers
This commit is contained in:
Samuel ROZE 2019-04-06 12:29:02 +02:00
commit 9793522f6e
2 changed files with 4 additions and 4 deletions

View File

@ -201,11 +201,11 @@ class MessengerPass implements CompilerPassInterface
throw new RuntimeException(sprintf('Invalid handler service "%s": class "%s" must have an "__invoke()" method.', $serviceId, $handlerClass->getName())); throw new RuntimeException(sprintf('Invalid handler service "%s": class "%s" must have an "__invoke()" method.', $serviceId, $handlerClass->getName()));
} }
$parameters = $method->getParameters(); if (0 === $method->getNumberOfRequiredParameters()) {
if (1 !== \count($parameters)) { throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s::__invoke()" requires at least one argument, first one being the message it handles.', $serviceId, $handlerClass->getName()));
throw new RuntimeException(sprintf('Invalid handler service "%s": method "%s::__invoke()" must have exactly one argument corresponding to the message it handles.', $serviceId, $handlerClass->getName()));
} }
$parameters = $method->getParameters();
if (!$type = $parameters[0]->getType()) { if (!$type = $parameters[0]->getType()) {
throw new RuntimeException(sprintf('Invalid handler service "%s": argument "$%s" of method "%s::__invoke()" must have a type-hint corresponding to the message class it handles.', $serviceId, $parameters[0]->getName(), $handlerClass->getName())); throw new RuntimeException(sprintf('Invalid handler service "%s": argument "$%s" of method "%s::__invoke()" must have a type-hint corresponding to the message class it handles.', $serviceId, $parameters[0]->getName(), $handlerClass->getName()));
} }

View File

@ -405,7 +405,7 @@ class MessengerPassTest extends TestCase
/** /**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\MissingArgumentHandler": method "Symfony\Component\Messenger\Tests\DependencyInjection\MissingArgumentHandler::__invoke()" must have exactly one argument corresponding to the message it handles. * @expectedExceptionMessage Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\MissingArgumentHandler": method "Symfony\Component\Messenger\Tests\DependencyInjection\MissingArgumentHandler::__invoke()" requires at least one argument, first one being the message it handles.
*/ */
public function testMissingArgumentHandler() public function testMissingArgumentHandler()
{ {