SendMessageMiddleware.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 $sendersLocator, EventDispatcherInterface $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 $envelope, StackInterface $stack): Envelope
  40. {
  41. $context = [
  42. 'class' => \get_class($envelope->getMessage()),
  43. ];
  44. $sender = null;
  45. if ($envelope->all(ReceivedStamp::class)) {
  46. // it's a received message, do not send it back
  47. $this->logger->info('Received message {class}', $context);
  48. } else {
  49. $shouldDispatchEvent = true;
  50. foreach ($this->sendersLocator->getSenders($envelope) as $alias => $sender) {
  51. if (null !== $this->eventDispatcher && $shouldDispatchEvent) {
  52. $event = new SendMessageToTransportsEvent($envelope);
  53. $this->eventDispatcher->dispatch($event);
  54. $envelope = $event->getEnvelope();
  55. $shouldDispatchEvent = false;
  56. }
  57. $this->logger->info('Sending message {class} with {alias} sender using {sender}', $context + ['alias' => $alias, 'sender' => \get_class($sender)]);
  58. $envelope = $sender->send($envelope->with(new SentStamp(\get_class($sender), \is_string($alias) ? $alias : null)));
  59. }
  60. }
  61. if (null === $sender) {
  62. return $stack->next()->handle($envelope, $stack);
  63. }
  64. // message should only be sent and not be handled by the next middleware
  65. return $envelope;
  66. }
  67. }