8000 [Messenger] use Envelope internally, return void, add EnvelopeHandlerInterface and other cleanups by nicolas-grekas · Pull Request #28881 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Messenger] use Envelope internally, return void, add EnvelopeHandlerInterface and other cleanups #28881

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
[Messenger] use Envelope internally, return void, add EnvelopeHandler…
…Interface and other cleanups
  • Loading branch information
nicolas-grekas committed Oct 17, 2018
commit f80cbca924e565bb18254b7b0521d89221aa45a4
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
use Symfony\Component\Messenger\Asynchronous\Transport\ReceivedMessage;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
* @author Tobias Schultze <http://tobion.de>
*/
class SendMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
class SendMessageMiddleware implements MiddlewareInterface
{
private $senderLocator;
private $messagesToSendAndHandleMapping;
Expand All @@ -34,11 +33,9 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
}

/**
* @param Envelope $envelope
*
* {@inheritdoc}
*/
public function handle($envelope, callable $next): void
public function handle(Envelope $envelope, callable $next): void
{
if ($envelope->get(ReceivedMessage::class)) {
// It's a received message. Do not send it back:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ abstract class AbstractSenderLocator implements SenderLocatorInterface
{
public static function getValueFromMessageRouting(array $mapping, Envelope $envelope)
{
$name = $envelope->getMessageName();

if (null !== $name && isset($mapping[$name])) {
return $mapping[$name];
}

if (isset($mapping[$class = \get_class($envelope->getMessage())])) {
return $mapping[$class];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function getSender(Envelope $envelope): ?SenderInterface
}

if (!$sender instanceof SenderInterface) {
throw new RuntimeException(sprintf('The sender instance provided for message "%s" should be of type "%s" but got "%s".', \get_class($envelope->getMessage()), SenderInterface::class, \is_object($sender) ? \get_class($sender) : \gettype($sender)));
throw new RuntimeException(sprintf('The sender instance provided for message "%s" should be of type "%s" but got "%s".', $envelope->getMessageName() ?? \get_class($envelope->getMessage()), SenderInterface::class, \is_object($sender) ? \get_class($sender) : \gettype($sender)));
}

return $sender;
Expand Down
31 changes: 12 additions & 19 deletions src/Symfony/Component/Messenger/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,15 @@ public function __construct($message, array $items = array())
*
* @param Envelope|object $message
*/
public static function wrap($message): self
public static function wrap($message, string $name = null): self
{
return $message instanceof self ? $message : new self($message);
$envelope = $message instanceof self ? clone $message : new self($message);
if (null !== $name) {
return $envelope->with(new MessageConfiguration($name));
}
unset($envelope->items[MessageConfiguration::class]);

return $envelope;
}

/**
Expand All @@ -58,15 +64,6 @@ public function with(EnvelopeItemInterface $item): self
return $cloned;
}

public function withMessage($message): self
{
$cloned = clone $this;

$cloned->message = $message;

return $cloned;
}

public function get(string $itemFqcn): ?EnvelopeItemInterface
{
return $this->items[$itemFqcn] ?? null;
Expand All @@ -88,14 +85,10 @@ public function getMessage()
return $this->message;
}

/**
* @param object $target
*
* @return Envelope|object The original message or the envelope if the target supports it
6D47 * (i.e implements {@link EnvelopeAwareInterface}).
*/
public function getMessageFor($target)
public function getMessageName(): ?string
{
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
$config = $this->items[MessageConfiguration::class] ?? null;

return $config ? $config->getName() : null;
}
}
21 changes: 0 additions & 21 deletions src/Symfony/Component/Messenger/EnvelopeAwareInterface.php

This file was deleted.

5 changes: 4 additions & 1 deletion src/Symfony/Component/Messenger/Handler/ChainHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Handler;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\InvalidArgumentException;

/**
Expand All @@ -37,8 +38,10 @@ public function __construct(array $handlers)
$this->handlers = $handlers;
}

public function __invoke($message)
public function __invoke(Envelope $envelope)
{
$message = $envelope->getMessage();

foreach ($this->handlers as $handler) {
$handler($message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ abstract class AbstractHandlerLocator implements HandlerLocatorInterface
{
public function getHandler(Envelope $envelope, bool $allowNoHandler = false): ?callable
{
$name = $envelope->getMessageName();

if (null !== $name && $handler = $this->getHandlerByName($name)) {
return $handler;
}
$class = \get_class($envelope->getMessage());

if ($handler = $this->getHandlerByName($class)) {
Expand Down
29 changes: 5 additions & 24 deletions src/Symfony/Component/Messenger/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(iterable $middlewareHandlers = array())
/**
* {@inheritdoc}
*/
public function dispatch($message): void
public function dispatch($message, string $name = null): void
{
if (!\is_object($message)) {
throw new \TypeError(sprintf('Invalid argument provided to "%s()": expected object, but got %s.', __METHOD__, \gettype($message)));
Expand All @@ -48,34 +48,15 @@ public function dispatch($message): void
$middlewareIterator = $this->middlewareAggregate->getIterator();

foreach ($middlewareIterator as $middleware) {
$currentEnvelope = Envelope::wrap($message);

// Do not provide the envelope if the middleware cannot read it:
$message = $middleware instanceof EnvelopeAwareInterface ? $currentEnvelope : $currentEnvelope->getMessage();

$next = static function ($message) use ($middlewareIterator, &$currentEnvelope, &$next) {
$next = static function ($envelope) use ($middlewareIterator, &$next) {
$middlewareIterator->next();

if (!$middlewareIterator->valid()) {
return;
}

$middleware = $middlewareIterator->current();

if ($message instanceof Envelope) {
$currentEnvelope = $message;
} else {
$message = $currentEnvelope->withMessage($message);
}

if (!$middleware instanceof EnvelopeAwareInterface) {
$message = $message->getMessage();
if ($middlewareIterator->valid()) {
$middlewareIterator->current()->handle($envelope, $next);
}

$middleware->handle($message, $next);
};

$middleware->handle($message, $next);
$middleware->handle(Envelope::wrap($message), $next);
}
}
}
4 changes: 3 additions & 1 deletion src/Symfony/Component/Messenger/MessageBusInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface MessageBusInterface
* Dispatches the given message.
*
* @param object|Envelope $message The message or the message pre-wrapped in an envelope
* @param string|null $name The name to use as dispatching key; when not provided,
* this name is derived from the type of the message
*/
public function dispatch($message): void;
public function dispatch($message, string $name = null): void;
}
30 changes: 30 additions & 0 deletions src/Symfony/Component/Messenger/MessageConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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;

/**
* @author Nicolas Grekas <p@tchwork.com>
*/
final class MessageConfiguration implements EnvelopeItemInterface
{
private $name;

public function __construct(string $name)
{
$this->name = $name;
}

public function getName(): string
{
return $this->name;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
namespace Symfony\Component\Messenger\Middleware\Enhancers;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;

/**
* Execute the inner middleware according to an activation strategy.
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
class ActivationMiddlewareDecorator implements MiddlewareInterface, EnvelopeAwareInterface
class ActivationMiddlewareDecorator implements MiddlewareInterface
{
private $inner;
private $activated;
Expand All @@ -35,12 +34,12 @@ public function __construct(MiddlewareInterface $inner, $activated)
}

/**
* @param Envelope $envelope
* {@inheritdoc}
*/
public function handle($envelope, callable $next): void
public function handle(Envelope $envelope, callable $next): void
{
if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
$this->inner->handle($envelope->getMessageFor($this->inner), $next);
$this->inner->handle($envelope, $next);
} else {
$next($envelope);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Symfony\Component\Messenger\Middleware\Enhancers;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Stopwatch\Stopwatch;

Expand All @@ -21,7 +20,7 @@
*
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
*/
class TraceableMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
class TraceableMiddleware implements MiddlewareInterface
{
private $inner;
private $stopwatch;
Expand All @@ -37,9 +36,9 @@ public function __construct(MiddlewareInterface $inner, Stopwatch $stopwatch, st
}

/**
* @param Envelope $envelope
* {@inheritdoc}
*/
public function handle($envelope, callable $next): void
public function handle(Envelope $envelope, callable $next): void
{
$class = \get_class($this->inner);
$eventName = 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
Expand All @@ -51,9 +50,9 @@ public function handle($envelope, callable $next): void
$this->stopwatch->start($eventName, $this->eventCategory);

try {
$this->inner->handle($envelope->getMessageFor($this->inner), function ($message) use ($next, $eventName) {
$this->inner->handle($envelope, fu F54E nction (Envelope $envelope) use ($next, $eventName) {
$this->stopwatch->stop($eventName);
$next($message);
$next($envelope);
$this->stopwatch->start($eventName, $this->eventCategory);
});
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@
namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Handler\Locator\HandlerLocatorInterface;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*/
class HandleMessageMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
class HandleMessageMiddleware implements MiddlewareInterface
{
private $messageHandlerLocator;

public function __construct(HandlerLocatorInterface $messageHandlerLocator)
public function __construct(HandlerLocatorInterface $messageHandlerLocator, bool $allowNoHandler = false)
{
$this->messageHandlerLocator = $messageHandlerLocator;
$this->allowNoHandler = $allowNoHandler;
}

/**
* @param Envelope $envelope
*/
public function handle($envelope, callable $next): void
public function handle(Envelope $envelope, callable $next): void
{
$handler = $this->messageHandlerLocator->getHandler($envelope);
$handler($envelope->getMessage());
if ($handler = $this->messageHandlerLocator->getHandler($envelope, $this->allowNoHandler)) {
$handler($envelope->getMessage());
}

$next($envelope);
}
Expand Down
Loading
0