vendor/symfony/messenger/Middleware/SendMessageMiddleware.php line 69

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Messenger\Middleware;
  11. use Psr\Log\LoggerAwareTrait;
  12. use Psr\Log\NullLogger;
  13. use Symfony\Component\EventDispatcher\Event;
  14. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  15. use Symfony\Component\Messenger\Envelope;
  16. use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
  17. use Symfony\Component\Messenger\Stamp\ReceivedStamp;
  18. use Symfony\Component\Messenger\Stamp\SentStamp;
  19. use Symfony\Component\Messenger\Transport\Sender\SendersLocatorInterface;
  20. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  21. /**
  22.  * @author Samuel Roze <samuel.roze@gmail.com>
  23.  * @author Tobias Schultze <http://tobion.de>
  24.  */
  25. class SendMessageMiddleware implements MiddlewareInterface
  26. {
  27.     use LoggerAwareTrait;
  28.     private $sendersLocator;
  29.     private $eventDispatcher;
  30.     public function __construct(SendersLocatorInterface $sendersLocatorEventDispatcherInterface $eventDispatcher null)
  31.     {
  32.         $this->sendersLocator $sendersLocator;
  33.         $this->eventDispatcher class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($eventDispatcher) : $eventDispatcher;
  34.         $this->logger = new NullLogger();
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      */
  39.     public function handle(Envelope $envelopeStackInterface $stack): Envelope
  40.     {
  41.         $context = [
  42.             'message' => $envelope->getMessage(),
  43.             'class' => \get_class($envelope->getMessage()),
  44.         ];
  45.         $sender null;
  46.         if ($envelope->all(ReceivedStamp::class)) {
  47.             // it's a received message, do not send it back
  48.             $this->logger->info('Received message {class}'$context);
  49.         } else {
  50.             $shouldDispatchEvent true;
  51.             foreach ($this->sendersLocator->getSenders($envelope) as $alias => $sender) {
  52.                 if (null !== $this->eventDispatcher && $shouldDispatchEvent) {
  53.                     $event = new SendMessageToTransportsEvent($envelope);
  54.                     $this->eventDispatcher->dispatch($event);
  55.                     $envelope $event->getEnvelope();
  56.                     $shouldDispatchEvent false;
  57.                 }
  58.                 $this->logger->info('Sending message {class} with {alias} sender using {sender}'$context + ['alias' => $alias'sender' => \get_class($sender)]);
  59.                 $envelope $sender->send($envelope->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias null)));
  60.             }
  61.         }
  62.         if (null === $sender) {
  63.             return $stack->next()->handle($envelope$stack);
  64.         }
  65.         // message should only be sent and not be handled by the next middleware
  66.         return $envelope;
  67.     }
  68. }