ActivationMiddleware.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 Symfony\Component\Messenger\Envelope;
  12. /**
  13. * Execute the inner middleware according to an activation strategy.
  14. *
  15. * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  16. */
  17. class ActivationMiddleware implements MiddlewareInterface
  18. {
  19. private $inner;
  20. private $activated;
  21. /**
  22. * @param bool|callable $activated
  23. */
  24. public function __construct(MiddlewareInterface $inner, $activated)
  25. {
  26. $this->inner = $inner;
  27. $this->activated = $activated;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function handle(Envelope $envelope, StackInterface $stack): Envelope
  33. {
  34. if (\is_callable($this->activated) ? ($this->activated)($envelope) : $this->activated) {
  35. return $this->inner->handle($envelope, $stack);
  36. }
  37. return $stack->next()->handle($envelope, $stack);
  38. }
  39. }