12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Messenger\Middleware;
- use Symfony\Component\Messenger\Envelope;
- use Symfony\Component\Messenger\Stamp\ConsumedByWorkerStamp;
- use Symfony\Component\Messenger\Stamp\RouterContextStamp;
- use Symfony\Component\Routing\RequestContextAwareInterface;
- /**
- * Restore the Router context when processing the message.
- *
- * @author Jérémy Derussé <jeremy@derusse.com>
- */
- class RouterContextMiddleware implements MiddlewareInterface
- {
- private $router;
- public function __construct(RequestContextAwareInterface $router)
- {
- $this->router = $router;
- }
- public function handle(Envelope $envelope, StackInterface $stack): Envelope
- {
- if (!$envelope->last(ConsumedByWorkerStamp::class) || !$contextStamp = $envelope->last(RouterContextStamp::class)) {
- $context = $this->router->getContext();
- $envelope = $envelope->with(new RouterContextStamp(
- $context->getBaseUrl(),
- $context->getMethod(),
- $context->getHost(),
- $context->getScheme(),
- $context->getHttpPort(),
- $context->getHttpsPort(),
- $context->getPathInfo(),
- $context->getQueryString()
- ));
- return $stack->next()->handle($envelope, $stack);
- }
- $context = $this->router->getContext();
- $currentBaseUrl = $context->getBaseUrl();
- $currentMethod = $context->getMethod();
- $currentHost = $context->getHost();
- $currentScheme = $context->getScheme();
- $currentHttpPort = $context->getHttpPort();
- $currentHttpsPort = $context->getHttpsPort();
- $currentPathInfo = $context->getPathInfo();
- $currentQueryString = $context->getQueryString();
- /* @var RouterContextStamp $contextStamp */
- $context
- ->setBaseUrl($contextStamp->getBaseUrl())
- ->setMethod($contextStamp->getMethod())
- ->setHost($contextStamp->getHost())
- ->setScheme($contextStamp->getScheme())
- ->setHttpPort($contextStamp->getHttpPort())
- ->setHttpsPort($contextStamp->getHttpsPort())
- ->setPathInfo($contextStamp->getPathInfo())
- ->setQueryString($contextStamp->getQueryString())
- ;
- try {
- return $stack->next()->handle($envelope, $stack);
- } finally {
- $context
- ->setBaseUrl($currentBaseUrl)
- ->setMethod($currentMethod)
- ->setHost($currentHost)
- ->setScheme($currentScheme)
- ->setHttpPort($currentHttpPort)
- ->setHttpsPort($currentHttpsPort)
- ->setPathInfo($currentPathInfo)
- ->setQueryString($currentQueryString)
- ;
- }
- }
- }
|