Skip to content

[Messenger] add support for abstract handlers #31429

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 1 commit into from
Feb 4, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
throw new RuntimeException(sprintf('Invalid handler service "%s": bus "%s" specified on the tag "%s" does not exist (known ones are: %s).', $serviceId, $tag['bus'], $this->handlerTag, implode(', ', $busIds)));
}

$className = $container->getDefinition($serviceId)->getClass();
$className = $this->getServiceClass($container, $serviceId);
$r = $container->getReflectionClass($className);

if (null === $r) {
Expand Down Expand Up @@ -240,7 +240,7 @@ private function registerReceivers(ContainerBuilder $container, array $busIds)
$receiverMapping = [];

foreach ($container->findTaggedServiceIds($this->receiverTag) as $id => $tags) {
$receiverClass = $container->findDefinition($id)->getClass();
$receiverClass = $this->getServiceClass($container, $id);
if (!is_subclass_of($receiverClass, ReceiverInterface::class)) {
throw new RuntimeException(sprintf('Invalid receiver "%s": class "%s" must implement interface "%s".', $id, $receiverClass, ReceiverInterface::class));
}
Expand Down Expand Up @@ -336,4 +336,19 @@ private function registerBusMiddleware(ContainerBuilder $container, string $busI

$container->getDefinition($busId)->replaceArgument(0, new IteratorArgument($middlewareReferences));
}

private function getServiceClass(ContainerBuilder $container, string $serviceId): string
{
while (true) {
$definition = $container->findDefinition($serviceId);

if (!$definition->getClass() && $definition instanceof ChildDefinition) {
$serviceId = $definition->getParent();

continue;
}

return $definition->getClass();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Tests\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -225,6 +226,34 @@ public function testGetClassesAndMethodsAndPrioritiesFromTheSubscriber()
$this->assertSame(PrioritizedHandler::class, $secondHandlerDefinition->getClass());
}

public function testRegisterAbstractHandler()
{
$container = $this->getContainerBuilder($messageBusId = 'message_bus');
$container->register($messageBusId, MessageBusInterface::class)->addTag('messenger.bus')->setArgument(0, []);

$container
->register(DummyHandler::class, DummyHandler::class)
->setAbstract(true);

$container
->setDefinition($abstractDirectChildId = 'direct_child', new ChildDefinition(DummyHandler::class))
->setAbstract(true);

$container
->setDefinition($abstractHandlerId = 'child', new ChildDefinition($abstractDirectChildId))
->addTag('messenger.message_handler');

(new MessengerPass())->process($container);

$messageHandlerMapping = $container->getDefinition($messageBusId.'.messenger.handlers_locator')->getArgument(0);
$this->assertHandlerDescriptor(
$container,
$messageHandlerMapping,
DummyMessage::class,
[$abstractHandlerId]
);
}

public function testThrowsExceptionIfTheHandlerClassDoesNotExist()
{
$this->expectException('Symfony\Component\DependencyInjection\Exception\RuntimeException');
Expand Down