Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
@@ -0,0 +1,55 @@
<?php

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

namespace Symfony\Component\Messenger\Handler\Locator;

use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;

/**
* @author Miha Vrhovnik <[email protected]>
* @author Samuel Roze <[email protected]>
*/
abstract class AbstractHandlerLocator implements HandlerLocatorInterface
{
public function resolve($message): callable
{
$messageClass = \get_class($message);

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

return $handler;
}

private function resolveFromClass(string $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;
}

abstract protected function getHandler(string $class);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
namespace Symfony\Component\Messenger\Handler\Locator;

use Psr\Container\ContainerInterface;
use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;

/**
* @author Miha Vrhovnik <[email protected]>
* @author Samuel Roze <[email protected]>
*/
class ContainerHandlerLocator implements HandlerLocatorInterface
class ContainerHandlerLocator extends AbstractHandlerLocator
{
private $container;

Expand All @@ -27,39 +26,7 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

public function resolve($message): callable
{
$messageClass = \get_class($message);

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

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)
protected function getHandler(string $class)
{
$handlerKey = 'handler.'.$class;

Expand Down
14 changes: 3 additions & 11 deletions src/Symfony/Component/Messenger/Handler/Locator/HandlerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@

namespace Symfony\Component\Messenger\Handler\Locator;

use Symfony\Component\Messenger\Exception\NoHandlerForMessageException;

/**
* @author Samuel Roze <[email protected]>
*/
class HandlerLocator implements HandlerLocatorInterface
class HandlerLocator extends AbstractHandlerLocator
{
/**
* Maps a message (its class) to a given handler.
Expand All @@ -28,14 +26,8 @@ public function __construct(array $messageToHandlerMapping = array())
$this->messageToHandlerMapping = $messageToHandlerMapping;
}

public function resolve($message): callable
protected function getHandler(string $class)
{
$messageKey = \get_class($message);

if (!isset($this->messageToHandlerMapping[$messageKey])) {
throw new NoHandlerForMessageException(sprintf('No handler for message "%s".', $messageKey));
}

return $this->messageToHandlerMapping[$messageKey];
return $this->messageToHandlerMapping[$class] ?? null;
}
}