Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Messenger] Allow interfaces to be type-hinted as well #28271

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
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 @@ -114,7 +114,7 @@ private function registerHandlers(ContainerBuilder $container, array $busIds)
$method = $method[0];
}

if (!\class_exists($messageClass)) {
if (!\class_exists($messageClass) && !\interface_exists($messageClass)) {
$messageClassLocation = isset($tag['handles']) ? 'declared in your tag attribute "handles"' : $r->implementsInterface(MessageSubscriberInterface::class) ? sprintf('returned by method "%s::getHandledMessages()"', $r->getName()) : sprintf('used as argument type in method "%s::%s()"', $r->getName(), $method);

throw new RuntimeException(sprintf('Invalid handler service "%s": message class "%s" %s does not exist.', $serviceId, $messageClass, $messageClassLocation));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,39 @@ public function __construct(ContainerInterface $container)
public function resolve($message): callable
{
$messageClass = \get_class($message);
$handlerKey = 'handler.'.$messageClass;

if (!$this->container->has($handlerKey)) {
if (null === $handler = $this->resolveFromClass($messageClass)) {
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', $messageClass));
}

return $this->container->get($handlerKey);
return $handler;
}

private function resolveFromClass($class): ?callable
{
if ($handler = $this->getHandler($class)) {
return $handler;
}

foreach (class_implements($class, false) as $interface) {
if ($handler = $this->getHandler($interface)) {
return $handler;
}
}

foreach (class_parents($class, false) as $parent) {
if ($handler = $this->getHandler($parent)) {
return $handler;
}
}

return null;
}

private function getHandler($class)
{
$handlerKey = 'handler.'.$class;

return $this->container->has($handlerKey) ? $this->container->get($handlerKey) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Tests\Asynchronous\Routing\ChildDummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\ChildDummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageInterface;
use Symfony\Component\Messenger\Transport\SenderInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocator;
use Symfony\Component\Messenger\Tests\Fixtures\ChildDummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageInterface;
use Symfony\Component\Messenger\Tests\Fixtures\SecondMessage;
Expand Down Expand Up @@ -88,7 +89,3 @@ public function testItSupportsAWildcardInsteadOfTheMessageClass()
$this->assertSame($apiSender, $locator->getSenderForMessage(new SecondMessage()));
}
}

class ChildDummyMessage extends DummyMessage
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Messenger\Tests\Fixtures;

class ChildDummyMessage extends DummyMessage
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Symfony\Component\Messenger\Tests\Handler\Locator;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Messenger\Handler\Locator\ContainerHandlerLocator;
use Symfony\Component\Messenger\Tests\Fixtures\ChildDummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessage;
use Symfony\Component\Messenger\Tests\Fixtures\DummyMessageInterface;

class ContainerHandlerLocatorTest extends TestCase
{
public function testItLocatesHandlerUsingTheMessageClass()
{
$handler = function () {};

$container = new Container();
$container->set('handler.'.DummyMessage::class, $handler);

$locator = new ContainerHandlerLocator($container);
$resolvedHandler = $locator->resolve(new DummyMessage('Hey'));

$this->assertSame($handler, $resolvedHandler);
}

/**
* @expectedException \Symfony\Component\Messenger\Exception\NoHandlerForMessageException
* @expectedExceptionMessage No handler for message "Symfony\Component\Messenger\Tests\Fixtures\DummyMessage"
*/
public function testThrowsNoHandlerException()
{
$locator = new ContainerHandlerLocator(new Container());
$locator->resolve(new DummyMessage('Hey'));
}

public function testResolveMessageViaTheirInterface()
{
$handler = function () {};

$container = new Container();
$container->set('handler.'.DummyMessageInterface::class, $handler);

$locator = new ContainerHandlerLocator($container);
$resolvedHandler = $locator->resolve(new DummyMessage('Hey'));

$this->assertSame($handler, $resolvedHandler);
}

public function testResolveMessageViaTheirParentClass()
{
$handler = function () {};

$container = new Container();
$container->set('handler.'.DummyMessage::class, $handler);

$locator = new ContainerHandlerLocator($container);
$resolvedHandler = $locator->resolve(new ChildDummyMessage('Hey'));

$this->assertSame($handler, $resolvedHandler);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.