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

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\EventDispatcher\EventDispatcherInterface;
  12. use Psr\Log\LoggerAwareTrait;
  13. use Psr\Log\NullLogger;
  14. use Symfony\Component\Messenger\Envelope;
  15. use Symfony\Component\Messenger\Event\SendMessageToTransportsEvent;
  16. use Symfony\Component\Messenger\Stamp\ReceivedStamp;
  17. use Symfony\Component\Messenger\Stamp\SentStamp;
  18. use Symfony\Component\Messenger\Transport\Sender\SendersLocatorInterface;
  19. /**
  20.  * @author Samuel Roze <samuel.roze@gmail.com>
  21.  * @author Tobias Schultze <http://tobion.de>
  22.  */
  23. class SendMessageMiddleware implements MiddlewareInterface
  24. {
  25.     use LoggerAwareTrait;
  26.     private SendersLocatorInterface $sendersLocator;
  27.     private ?EventDispatcherInterface $eventDispatcher;
  28.     public function __construct(SendersLocatorInterface $sendersLocatorEventDispatcherInterface $eventDispatcher null)
  29.     {
  30.         $this->sendersLocator $sendersLocator;
  31.         $this->eventDispatcher $eventDispatcher;
  32.         $this->logger = new NullLogger();
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      */
  37.     public function handle(Envelope $envelopeStackInterface $stack): Envelope
  38.     {
  39.         $context = [
  40.             'class' => \get_class($envelope->getMessage()),
  41.         ];
  42.         $sender null;
  43.         if ($envelope->all(ReceivedStamp::class)) {
  44.             // it's a received message, do not send it back
  45.             $this->logger->info('Received message {class}'$context);
  46.         } else {
  47.             $shouldDispatchEvent true;
  48.             foreach ($this->sendersLocator->getSenders($envelope) as $alias => $sender) {
  49.                 if (null !== $this->eventDispatcher && $shouldDispatchEvent) {
  50.                     $event = new SendMessageToTransportsEvent($envelope);
  51.                     $this->eventDispatcher->dispatch($event);
  52.                     $envelope $event->getEnvelope();
  53.                     $shouldDispatchEvent false;
  54.                 }
  55.                 $this->logger->info('Sending message {class} with {alias} sender using {sender}'$context + ['alias' => $alias'sender' => \get_class($sender)]);
  56.                 $envelope $sender->send($envelope->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias null)));
  57.             }
  58.         }
  59.         if (null === $sender) {
  60.             return $stack->next()->handle($envelope$stack);
  61.         }
  62.         // message should only be sent and not be handled by the next middleware
  63.         return $envelope;
  64.     }
  65. }