RequestStack.php 2.3 KB

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