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] Envelope as first class citizen in middleware too #27322

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 2 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
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,7 @@ public function __construct(ManagerRegistry $managerRegistry, ?string $entityMan
$this->entityManagerName = $entityManagerName;
}

public function handle($message, callable $next)
public function handle(Envelope $envelope, callable $next)
{
$entityManager = $this->managerRegistry->getManager($this->entityManagerName);

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

$entityManager->getConnection()->beginTransaction();
try {
$result = $next($message);
$result = $next($envelope);
$entityManager->flush();
$entityManager->getConnection()->commit();
} catch (\Throwable $exception) {
Expand Down
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 @@ -36,12 +35,11 @@ public function __construct(SenderLocatorInterface $senderLocator, array $messag
/**
* {@inheritdoc}
*/
public function handle($message, callable $next)
public function handle(Envelope $envelope, callable $next)
{
$envelope = Envelope::wrap($message);
if ($envelope->get(ReceivedMessage::class)) {
// It's a received message. Do not send it back:
return $next($message);
return $next($envelope);
}

$sender = $this->senderLocator->getSenderForMessage($envelope->getMessage());
Expand All @@ -54,7 +52,7 @@ public function handle($message, callable $next)
}
}

return $next($message);
return $next($envelope);
}

private function mustSendAndHandle($message): bool
Expand Down
23 changes: 0 additions & 23 deletions 23 src/Symfony/Component/Messenger/EnvelopeAwareInterface.php

This file was deleted.

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

return \call_user_func($this->callableForNextMiddleware(0, Envelope::wrap($message)), $message);
return \call_user_func($this->callableForNextMiddleware(0), Envelope::wrap($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);
Expand All @@ -59,19 +59,8 @@ private function callableForNextMiddleware(int $index, Envelope $currentEnvelope

$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();
}

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

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)
public function handle(Envelope $envelope, callable $next)
{
try {
return $next($message);
return $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 @@ -11,6 +11,7 @@

namespace Symfony\Component\Messenger\Middleware;

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

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

$next($message);
$next($envelope);

return $result;
}
Expand Down
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,14 @@ public function __construct(LoggerInterface $logger)
/**
* {@inheritdoc}
*/
public function handle($message, callable $next)
public function handle(Envelope $envelope, callable $next)
{
$message = $envelope->getMessage();

$this->logger->debug('Starting handling message {class}', $this->createContext($message));

try {
$result = $next($message);
$result = $next($envelope);
} catch (\Throwable $e) {
$this->logger->warning('An exception occurred while handling message {class}', array_merge(
$this->createContext($message),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Messenger\Middleware;

use Symfony\Component\Messenger\Envelope;

/**
* @author Samuel Roze <samuel.roze@gmail.com>
*
Expand All @@ -19,9 +21,9 @@
interface MiddlewareInterface
{
/**
* @param object $message
* @param Envelope $envelope
*
* @return mixed
*/
public function handle($message, callable $next);
public function handle(Envelope $envelope, callable $next);
}
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\Middleware\Configuration\ValidationConfiguration;
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 @@ -29,21 +28,20 @@ public function __construct(ValidatorInterface $validator)
$this->validator = $validator;
}

public function handle($message, callable $next)
public function handle(Envelope $envelope, callable $next)
{
$envelope = Envelope::wrap($message);
$subject = $envelope->getMessage();
$message = $envelope->getMessage();
$groups = null;
/** @var ValidationConfiguration|null $validationConfig */
if ($validationConfig = $envelope->get(ValidationConfiguration::class)) {
$groups = $validationConfig->getGroups();
}

$violations = $this->validator->validate($subject, null, $groups);
$violations = $this->validator->validate($message, null, $groups);
if (\count($violations)) {
throw new ValidationFailedException($subject, $violations);
throw new ValidationFailedException($message, $violations);
}

return $next($message);
return $next($envelope);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,6 @@
class SendMessageMiddlewareTest extends TestCase
{
public function testItSendsTheMessageToAssignedSender()
{
$message = new DummyMessage('Hey');
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));

$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender));

$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
$next->expects($this->never())->method($this->anything());

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

public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage()
{
$envelope = Envelope::wrap(new DummyMessage('Hey'));
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
Expand All @@ -53,78 +39,78 @@ public function testItSendsTheMessageToAssignedSenderWithPreWrappedMessage()

public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageClass()
{
$message = new DummyMessage('Hey');
$envelope = Envelope::wrap(new DummyMessage('Hey'));
Copy link
Member

Choose a reason for hiding this comment

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

It feels like new Envelope(...) makes more sense here if you're sure that the message isn't another envelope instance, it also seems natural to me, IMHO. (same for other tests)

Copy link
Contributor Author

@ogizanagi ogizanagi May 20, 2018

Choose a reason for hiding this comment

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

You're right. I think I'll remove this named construct as well as it doesn't really serve a purpose anymore, appart the fluent api without extra ( )

$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));

$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
DummyMessage::class => true,
));

$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
$sender->expects($this->once())->method('send')->with($envelope);
$next->expects($this->once())->method($this->anything());

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

public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageParentClass()
{
$message = new ChildDummyMessage('Hey');
$envelope = Envelope::wrap(new ChildDummyMessage('Hey'));
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));

$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
DummyMessage::class => true,
));

$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
$sender->expects($this->once())->method('send')->with($envelope);
$next->expects($this->once())->method($this->anything());

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

public function testItAlsoCallsTheNextMiddlewareBasedOnTheMessageInterface()
{
$message = new DummyMessage('Hey');
$envelope = Envelope::wrap(new DummyMessage('Hey'));
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));

$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
DummyMessageInterface::class => true,
));

$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
$sender->expects($this->once())->method('send')->with($envelope);
$next->expects($this->once())->method($this->anything());

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

public function testItAlsoCallsTheNextMiddlewareBasedOnWildcard()
{
$message = new DummyMessage('Hey');
$envelope = Envelope::wrap(new DummyMessage('Hey'));
$sender = $this->getMockBuilder(SenderInterface::class)->getMock();
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));

$middleware = new SendMessageMiddleware(new InMemorySenderLocator($sender), array(
'*' => true,
));

$sender->expects($this->once())->method('send')->with(Envelope::wrap($message));
$next->expects($this->once())->method($this->anything());
$sender->expects($this->once())->method('send')->with($envelope);
$next->expects($this->once())->method($this->anything())->with($envelope);

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

public function testItCallsTheNextMiddlewareWhenNoSenderForThisMessage()
{
$message = new DummyMessage('Hey');
$envelope = Envelope::wrap(new DummyMessage('Hey'));
$next = $this->createPartialMock(\stdClass::class, array('__invoke'));

$middleware = new SendMessageMiddleware(new InMemorySenderLocator(null));

$next->expects($this->once())->method($this->anything());
$next->expects($this->once())->method($this->anything())->with($envelope);

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

public function testItSkipsReceivedMessages()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,8 @@ public function __invoke()

class UselessMiddleware implements MiddlewareInterface
{
public function handle($message, callable $next)
public function handle(Envelope $envelope, callable $next)
{
return $next($message);
return $next($envelope);
}
}
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.