RequestStack.php
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\HttpFoundation; |
| 13 | |
| 14 | /** |
| 15 | * Request stack that controls the lifecycle of requests. |
| 16 | * |
| 17 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
| 18 | */ |
| 19 | class RequestStack |
| 20 | { |
| 21 | /** |
| 22 | * @var Request[] |
| 23 | */ |
| 24 | private $requests = []; |
| 25 | |
| 26 | /** |
| 27 | * Pushes a Request on the stack. |
| 28 | * |
| 29 | * This method should generally not be called directly as the stack |
| 30 | * management should be taken care of by the application itself. |
| 31 | */ |
| 32 | public function push(Request $request) |
| 33 | { |
| 34 | $this->requests[] = $request; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Pops the current request from the stack. |
| 39 | * |
| 40 | * This operation lets the current request go out of scope. |
| 41 | * |
| 42 | * This method should generally not be called directly as the stack |
| 43 | * management should be taken care of by the application itself. |
| 44 | * |
| 45 | * @return Request|null |
| 46 | */ |
| 47 | public function pop() |
| 48 | { |
| 49 | if (!$this->requests) { |
| 50 | return null; |
| 51 | } |
| 52 | |
| 53 | return array_pop($this->requests); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @return Request|null |
| 58 | */ |
| 59 | public function getCurrentRequest() |
| 60 | { |
| 61 | return end($this->requests) ?: null; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Gets the master Request. |
| 66 | * |
| 67 | * Be warned that making your code aware of the master request |
| 68 | * might make it un-compatible with other features of your framework |
| 69 | * like ESI support. |
| 70 | * |
| 71 | * @return Request|null |
| 72 | */ |
| 73 | public function getMasterRequest() |
| 74 | { |
| 75 | if (!$this->requests) { |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | return $this->requests[0]; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns the parent request of the current. |
| 84 | * |
| 85 | * Be warned that making your code aware of the parent request |
| 86 | * might make it un-compatible with other features of your framework |
| 87 | * like ESI support. |
| 88 | * |
| 89 | * If current Request is the master request, it returns null. |
| 90 | * |
| 91 | * @return Request|null |
| 92 | */ |
| 93 | public function getParentRequest() |
| 94 | { |
| 95 | $pos = \count($this->requests) - 2; |
| 96 | |
| 97 | if (!isset($this->requests[$pos])) { |
| 98 | return null; |
| 99 | } |
| 100 | |
| 101 | return $this->requests[$pos]; |
| 102 | } |
| 103 | } |
| 104 |