RouterContextMiddleware.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;
  13. use Symfony\Component\Messenger\Stamp\RouterContextStamp;
  14. use Symfony\Component\Routing\RequestContextAwareInterface;
  15. /**
  16. * Restore the Router context when processing the message.
  17. *
  18. * @author Jérémy Derussé <jeremy@derusse.com>
  19. */
  20. class RouterContextMiddleware implements MiddlewareInterface
  21. {
  22. private $router;
  23. public function __construct(RequestContextAwareInterface $router)
  24. {
  25. $this->router = $router;
  26. }
  27. public function handle(Envelope $envelope, StackInterface $stack): Envelope
  28. {
  29. if (!$envelope->last(ConsumedByWorkerStamp::class) || !$contextStamp = $envelope->last(RouterContextStamp::class)) {
  30. $context = $this->router->getContext();
  31. $envelope = $envelope->with(new RouterContextStamp(
  32. $context->getBaseUrl(),
  33. $context->getMethod(),
  34. $context->getHost(),
  35. $context->getScheme(),
  36. $context->getHttpPort(),
  37. $context->getHttpsPort(),
  38. $context->getPathInfo(),
  39. $context->getQueryString()
  40. ));
  41. return $stack->next()->handle($envelope, $stack);
  42. }
  43. $context = $this->router->getContext();
  44. $currentBaseUrl = $context->getBaseUrl();
  45. $currentMethod = $context->getMethod();
  46. $currentHost = $context->getHost();
  47. $currentScheme = $context->getScheme();
  48. $currentHttpPort = $context->getHttpPort();
  49. $currentHttpsPort = $context->getHttpsPort();
  50. $currentPathInfo = $context->getPathInfo();
  51. $currentQueryString = $context->getQueryString();
  52. /* @var RouterContextStamp $contextStamp */
  53. $context
  54. ->setBaseUrl($contextStamp->getBaseUrl())
  55. ->setMethod($contextStamp->getMethod())
  56. ->setHost($contextStamp->getHost())
  57. ->setScheme($contextStamp->getScheme())
  58. ->setHttpPort($contextStamp->getHttpPort())
  59. ->setHttpsPort($contextStamp->getHttpsPort())
  60. ->setPathInfo($contextStamp->getPathInfo())
  61. ->setQueryString($contextStamp->getQueryString())
  62. ;
  63. try {
  64. return $stack->next()->handle($envelope, $stack);
  65. } finally {
  66. $context
  67. ->setBaseUrl($currentBaseUrl)
  68. ->setMethod($currentMethod)
  69. ->setHost($currentHost)
  70. ->setScheme($currentScheme)
  71. ->setHttpPort($currentHttpPort)
  72. ->setHttpsPort($currentHttpsPort)
  73. ->setPathInfo($currentPathInfo)
  74. ->setQueryString($currentQueryString)
  75. ;
  76. }
  77. }
  78. }