You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #27034 [Messenger][DX] Uses custom method names for handlers (sroze)
This PR was merged into the 4.1 branch.
Discussion
----------
[Messenger][DX] Uses custom method names for handlers
| Q | A
| ------------- | ---
| Branch? | master
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | symfony/symfony#26685 (comment)
| License | MIT
| Doc PR | ø
This has been discussed mostly in the [`MessageHandlerInterface` pull-request](symfony/symfony#26685). For consistency reasons and convenience, this PR adds the ability to configure the method to be used on handlers:
```php
use Symfony\Component\Messenger\Handler\MessageSubscriberInterface;
use Symfony\Component\Messenger\Handler\MessageSubscriberConfiguration;
class CreateNumberMessageHandler implements MessageSubscriberInterface
{
/**
* {@inheritdoc}
*/
public static function getHandledMessages(): array
{
return [
CreateNumber::class => ['createNumber', 10],
AnotherMessage::class => 'anotherMethod',
];
}
public function createNumber(CreateNumber $command)
{
// ...
}
}
```
Commits
-------
2461e5119a [Messenger][DX] Uses custom method names for handlers
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : sprintf($r->implementsInterface(MessageHandlerInterface::class) ? 'returned by method "%s::getHandledMessages()"' : 'used as argument type in method "%s::__invoke()"', $r->getName());
105
+
if (\is_array($method)) {
106
+
$messagePriority = $method[1];
107
+
$method = $method[0];
108
+
}
109
+
110
+
if (!\class_exists($messageClass)) {
111
+
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageHandlerInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);
94
112
95
113
thrownewRuntimeException(sprintf('Invalid handler service "%s": message class "%s" %s does not exist.', $serviceId, $messageClass, $messageClassLocation));
96
114
}
97
115
116
+
if (!$r->hasMethod($method)) {
117
+
thrownewRuntimeException(sprintf('Invalid handler service "%s": method "%s::%s()" does not exist.', $serviceId, $r->getName(), $method));
* @expectedExceptionMessage Invalid handler service "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerMappingWithNonExistentMethod": method "Symfony\Component\Messenger\Tests\DependencyInjection\HandlerMappingWithNonExistentMethod::dummyMethod()" does not exist.
0 commit comments