Skip to content

[Messenger] make Envelope first class citizen for middleware handlers #28914

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
Oct 21, 2018
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 @@ -13,6 +13,7 @@

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;

/**
Expand All @@ -31,7 +32,10 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
$this->entityManagerName = $entityManagerName;
}

public function handle($message, callable $next): void
/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, callable $next): void
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

Expand All @@ -41,7 +45,7 @@ public function handle($message, callable $next): void

$entityManager->getConnection()->beginTransaction();
try {
$next($message);
$next($envelope);
$entityManager->flush();
$entityManager->getConnection()->commit();
} catch (\Throwable $exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@
use Symfony\Component\Messenger\Asynchronous\Routing\AbstractSenderLocator;
use Symfony\Component\Messenger\Asynchronous\Routing\SenderLocatorInterface;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Stamp\ReceivedStamp;

/**
* @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(ReceivedStamp::class)) {
// It's a received message. Do not send it back:
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Messenger/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CHANGELOG
* The component is not experimental anymore
* All the changes below are BC BREAKS
* `MessageBusInterface::dispatch()` and `MiddlewareInterface::handle()` now return `void`
* `MiddlewareInterface::handle()` now require an `Envelope` as first argument
* `EnvelopeAwareInterface` has been removed
* The signature of `Amqp*` classes changed to take a `Connection` as a first argument and an optional
`Serializer` as a second argument.
* `SenderLocator` has been renamed to `ContainerSenderLocator`
Expand Down
30 changes: 0 additions & 30 deletions src/Symfony/Component/Messenger/Envelope.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ public function __construct($message, StampInterface ...$stamps)
}
}

/**
* Wrap a message into an envelope if not already wrapped.
*
* @param Envelope|object $message
*/
public static function wrap($message): self
{
return $message instanceof self ? $message : new self($message);
}

/**
* @return Envelope a new Envelope instance with additional stamp
*/
Expand All @@ -59,15 +49,6 @@ public function with(StampInterface ...$stamps): self
return $cloned;
}

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

$cloned->message = $message;

return $cloned;
}

public function get(string $stampFqcn): ?StampInterface
{
return $this->stamps[$stampFqcn] ?? null;
Expand All @@ -88,15 +69,4 @@ public function getMessage()
{
return $this->message;
}

/**
* @param object $target
*
* @return Envelope|object The original message or the envelope if the target supports it
* (i.e implements {@link EnvelopeAwareInterface}).
*/
public function getMessageFor($target)
{
return $target instanceof EnvelopeAwareInterface ? $this : $this->message;
}
}
21 changes: 0 additions & 21 deletions src/Symfony/Component/Messenger/EnvelopeAwareInterface.php

This file was deleted.

23 changes: 5 additions & 18 deletions src/Symfony/Component/Messenger/MessageBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,34 +44,21 @@ public function dispatch($message): void
throw new InvalidArgumentException(sprintf('Invalid type for message argument. Expected object, but got "%s".', \gettype($message)));
}

\call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message);
$this->callableForNextMiddleware(0)($message instanceof Envelope ? $message : new Envelope($message));
}

private function callableForNextMiddleware(int $index, Envelope $currentEnvelope): callable
private function callableForNextMiddleware(int $index): callable
{
if (null === $this->indexedMiddlewareHandlers) {
$this->indexedMiddlewareHandlers = \is_array($this->middlewareHandlers) ? array_values($this->middlewareHandlers) : iterator_to_array($this->middlewareHandlers, false);
}

if (!isset($this->indexedMiddlewareHandlers[$index])) {
return function () {};
return static function () {};
}

$middleware = $this->indexedMiddlewareHandlers[$index];

return function ($message) use ($middleware, $index, $currentEnvelope) {
if ($message instanceof Envelope) {
$currentEnvelope = $message;
} else {
$message = $currentEnvelope->withMessage($message);
}

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

$middleware->handle($message, $this->callableForNextMiddleware($index + 1, $currentEnvelope));
return function (Envelope $envelope) use ($index) {
$this->indexedMiddlewareHandlers[$index]->handle($envelope, $this->callableForNextMiddleware($index + 1));
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,21 @@

namespace Symfony\Component\Messenger\Middleware;

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

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*/
class AllowNoHandlerMiddleware implements MiddlewareInterface
{
public function handle($message, callable $next): void
/**
* {@inheritdoc}
*/
public function handle(Envelope $envelope, callable $next): void
{
try {
$next($message);
$next($envelope);
} catch (NoHandlerForMessageException $e) {
// We allow not having a handler for this message.
}
Expand Down
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, function (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 @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Middleware;

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

/**
Expand All @@ -28,11 +29,12 @@ public function __construct(HandlerLocatorInterface $messageHandlerResolver)
/**
* {@inheritdoc}
*/
public function handle($message, callable $next): void
public function handle(Envelope $envelope, callable $next): void
{
$message = $envelope->getMessage();
$handler = $this->messageHandlerResolver->resolve($message);
$handler($message);

$next($message);
$next($envelope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Messenger\Middleware;

use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Envelope;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
Expand All @@ -28,12 +29,13 @@ public function __construct(LoggerInterface $logger)
/**
* {@inheritdoc}
*/
public function handle($message, callable $next): void
public function handle(Envelope $envelope, callable $next): void
{
$message = $envelope->getMessage();
$this->logger->debug('Starting handling message {class}', $this->createContext($message));

try {
$next($message);
$next($envelope);
} catch (\Throwable $e) {
$this->logger->warning('An exception occurred while handling message {class}', array_merge(
$this->createContext($message),
Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Component/Messenger/Middleware/MiddlewareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@

namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Envelope;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*/
interface MiddlewareInterface
{
/**
* @param object $message
* @param callable|NextInterface $next
*/
public function handle($message, callable $next): void;
public function handle(Envelope $envelope, callable $next): void;
}

/**
* @internal
*/
interface NextInterface
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where it used?

Copy link
Member Author

@nicolas-grekas nicolas-grekas Oct 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the docblock above, to document the signature of the callable.

{
public function __invoke(Envelope $envelope): void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@
namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\EnvelopeAwareInterface;
use Symfony\Component\Messenger\Exception\ValidationFailedException;
use Symfony\Component\Messenger\Stamp\ValidationStamp;
use Symfony\Component\Validator\Validator\ValidatorInterface;

/**
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class ValidationMiddleware implements MiddlewareInterface, EnvelopeAwareInterface
class ValidationMiddleware implements MiddlewareInterface
{
private $validator;

Expand All @@ -30,9 +29,9 @@ public function __construct(ValidatorInterface $validator)
}

/**
* @param Envelope $envelope
* {@inheritdoc}
*/
public function handle($envelope, callable $next): void
public function handle(Envelope $envelope, callable $next): void
{
$message = $envelope->getMessage();
$groups = null;
Expand Down
Loading