You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.1 KiB

3 years ago
  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. if (!isset($this->requests[$pos])) {
  99. return null;
  100. }
  101. return $this->requests[$pos];
  102. }
  103. /**
  104. * Gets the current session.
  105. *
  106. * @throws SessionNotFoundException
  107. */
  108. public function getSession(): SessionInterface
  109. {
  110. if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
  111. return $request->getSession();
  112. }
  113. throw new SessionNotFoundException();
  114. }
  115. }