VirtualRequestStack.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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\HttpKernel\Debug;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. /**
  14. * A stack able to deal with virtual requests.
  15. *
  16. * @internal
  17. *
  18. * @author Jules Pietri <jules@heahprod.com>
  19. */
  20. final class VirtualRequestStack extends RequestStack
  21. {
  22. public function __construct(
  23. private readonly RequestStack $decorated,
  24. ) {
  25. }
  26. public function push(Request $request): void
  27. {
  28. if ($request->attributes->has('_virtual_type')) {
  29. if ($this->decorated->getCurrentRequest()) {
  30. throw new \LogicException('Cannot mix virtual and HTTP requests.');
  31. }
  32. parent::push($request);
  33. return;
  34. }
  35. $this->decorated->push($request);
  36. }
  37. public function pop(): ?Request
  38. {
  39. return $this->decorated->pop() ?? parent::pop();
  40. }
  41. public function getCurrentRequest(): ?Request
  42. {
  43. return $this->decorated->getCurrentRequest() ?? parent::getCurrentRequest();
  44. }
  45. public function getMainRequest(): ?Request
  46. {
  47. return $this->decorated->getMainRequest() ?? parent::getMainRequest();
  48. }
  49. public function getParentRequest(): ?Request
  50. {
  51. return $this->decorated->getParentRequest() ?? parent::getParentRequest();
  52. }
  53. }