RequestStack.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. 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. public function pop(): ?Request
  43. {
  44. if (!$this->requests) {
  45. return null;
  46. }
  47. return array_pop($this->requests);
  48. }
  49. public function getCurrentRequest(): ?Request
  50. {
  51. return end($this->requests) ?: null;
  52. }
  53. /**
  54. * Gets the main request.
  55. *
  56. * Be warned that making your code aware of the main request
  57. * might make it un-compatible with other features of your framework
  58. * like ESI support.
  59. */
  60. public function getMainRequest(): ?Request
  61. {
  62. if (!$this->requests) {
  63. return null;
  64. }
  65. return $this->requests[0];
  66. }
  67. /**
  68. * Returns the parent request of the current.
  69. *
  70. * Be warned that making your code aware of the parent request
  71. * might make it un-compatible with other features of your framework
  72. * like ESI support.
  73. *
  74. * If current Request is the main request, it returns null.
  75. */
  76. public function getParentRequest(): ?Request
  77. {
  78. $pos = \count($this->requests) - 2;
  79. return $this->requests[$pos] ?? null;
  80. }
  81. /**
  82. * Gets the current session.
  83. *
  84. * @throws SessionNotFoundException
  85. */
  86. public function getSession(): SessionInterface
  87. {
  88. if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) {
  89. return $request->getSession();
  90. }
  91. throw new SessionNotFoundException();
  92. }
  93. }