RequestStack.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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\HttpFoundation;
  11. use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. /**
  14. * Request stack that controls the lifecycle of requests.
  15. *
  16. * @author Benjamin Eberlei <kontakt@beberlei.de>
  17. */
  18. class RequestStack
  19. {
  20. /**
  21. * @var Request[]
  22. */
  23. private array $requests = [];
  24. /**
  25. * Pushes a Request on the stack.
  26. *
  27. * This method should generally not be called directly as the stack
  28. * management should be taken care of by the application itself.
  29. *
  30. * @return void
  31. */
  32. public function push(Request $request)
  33. {
  34. $this->requests[] = $request;
  35. }
  36. /**
  37. * Pops the current request from the stack.
  38. *
  39. * This operation lets the current request go out of scope.
  40. *
  41. * This method should generally not be called directly as the stack
  42. * management should be taken care of by the application itself.
  43. */
  44. public function pop(): ?Request
  45. {
  46. if (!$this->requests) {
  47. return null;
  48. }
  49. return array_pop($this->requests);
  50. }
  51. public function getCurrentRequest(): ?Request
  52. {
  53. return end($this->requests) ?: null;
  54. }
  55. /**
  56. * Gets the main request.
  57. *
  58. * Be warned that making your code aware of the main request
  59. * might make it un-compatible with other features of your framework
  60. * like ESI support.
  61. */
  62. public function getMainRequest(): ?Request
  63. {
  64. if (!$this->requests) {
  65. return null;
  66. }
  67. return $this->requests[0];
  68. }
  69. /**
  70. * Returns the parent request of the current.
  71. *
  72. * Be warned that making your code aware of the parent request
  73. * might make it un-compatible with other features of your framework
  74. * like ESI support.
  75. *
  76. * If current Request is the main request, it returns null.
  77. */
  78. public function getParentRequest(): ?Request
  79. {
  80. $pos = \count($this->requests) - 2;
  81. return $this->requests[$pos] ?? null;
  82. }
  83. /**
  84. * Gets the current session.
  85. *
  86. * @throws SessionNotFoundException
  87. */
  88. public function getSession(): SessionInterface
  89. {
  90. if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
  91. return $request->getSession();
  92. }
  93. throw new SessionNotFoundException();
  94. }
  95. }