8000 [Messenger] Envelope as first class citizen in middleware too by ogizanagi · Pull Request #27322 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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 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,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
8000
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
10 changes: 0 additions & 10 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, array $items = array())
}
}

/**
* 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 item
*/
Expand Down
23 changes: 0 additions & 23 deletions src/Symfony/Component/Messenger/EnvelopeAwareInterface.php

This file was deleted.

19 changes: 4 additions & 15 deletions 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), $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);
Expand All @@ -59,19 +59,8 @@ private function callableForNextMiddleware(int $index, Envelope $currentEnvelope

< 9E88 /td>
$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.
}
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);
}
}
Loading
0