0
0

RequestStack.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 $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. public function push(Request $request)
  31. {
  32. $this->requests[] = $request;
  33. }
  34. /**
  35. * Pops the current request from the stack.
  36. *
  37. * This operation lets the current request go out of scope.
  38. *
  39. * This method should generally not be called directly as the stack
  40. * management should be taken care of by the application itself.
  41. *
  42. * @return Request|null
  43. */
  44. public function pop()
  45. {
  46. if (!$this->requests) {
  47. return null;
  48. }
  49. return array_pop($this->requests);
  50. }
  51. /**
  52. * @return Request|null
  53. */
  54. public function getCurrentRequest()
  55. {
  56. return end($this->requests) ?: null;
  57. }
  58. /**
  59. * Gets the main request.
  60. *
  61. * Be warned that making your code aware of the main request
  62. * might make it un-compatible with other features of your framework
  63. * like ESI support.
  64. */
  65. public function getMainRequest(): ?Request
  66. {
  67. if (!$this->requests) {
  68. return null;
  69. }
  70. return $this->requests[0];
  71. }
  72. /**
  73. * Gets the master request.
  74. *
  75. * @return Request|null
  76. *
  77. * @deprecated since symfony/http-foundation 5.3, use getMainRequest() instead
  78. */
  79. public function getMasterRequest()
  80. {
  81. trigger_deprecation('symfony/http-foundation', '5.3', '"%s()" is deprecated, use "getMainRequest()" instead.', __METHOD__);
  82. return $this->getMainRequest();
  83. }
  84. /**
  85. * Returns the parent request of the current.
  86. *
  87. * Be warned that making your code aware of the parent request
  88. * might make it un-compatible with other features of your framework
  89. * like ESI support.
  90. *
  91. * If current Request is the main request, it returns null.
  92. *
  93. * @return Request|null
  94. */
  95. public function getParentRequest()
  96. {
  97. $pos = \count($this->requests) - 2;
  98. return $this->requests[$pos] ?? null;
  99. }
  100. /**
  101. * Gets the current session.
  102. *
  103. * @throws SessionNotFoundException
  104. */
  105. public function getSession(): SessionInterface
  106. {
  107. if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
  108. return $request->getSession();
  109. }
  110. throw new SessionNotFoundException();
  111. }
  112. }