| 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 |
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; |
| 15 |
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
| 16 |
|
| 17 |
/** |
| 18 |
* Request stack that controls the lifecycle of requests. |
| 19 |
* |
| 20 |
* @author Benjamin Eberlei <kontakt@beberlei.de> |
| 21 |
*/ |
| 22 |
class RequestStack |
| 23 |
{ |
| 24 |
/** |
| 25 |
* @var Request[] |
| 26 |
*/ |
| 27 |
private $requests = []; |
| 28 |
|
| 29 |
/** |
| 30 |
* Pushes a Request on the stack. |
| 31 |
* |
| 32 |
* This method should generally not be called directly as the stack |
| 33 |
* management should be taken care of by the application itself. |
| 34 |
*/ |
| 35 |
public function push(Request $request) |
| 36 |
{ |
| 37 |
$this->requests[] = $request; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Pops the current request from the stack. |
| 42 |
* |
| 43 |
* This operation lets the current request go out of scope. |
| 44 |
* |
| 45 |
* This method should generally not be called directly as the stack |
| 46 |
* management should be taken care of by the application itself. |
| 47 |
* |
| 48 |
* @return Request|null |
| 49 |
*/ |
| 50 |
public function pop() |
| 51 |
{ |
| 52 |
if (!$this->requests) { |
| 53 |
return null; |
| 54 |
} |
| 55 |
|
| 56 |
return array_pop($this->requests); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* @return Request|null |
| 61 |
*/ |
| 62 |
public function getCurrentRequest() |
| 63 |
{ |
| 64 |
return end($this->requests) ?: null; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Gets the main request. |
| 69 |
* |
| 70 |
* Be warned that making your code aware of the main request |
| 71 |
* might make it un-compatible with other features of your framework |
| 72 |
* like ESI support. |
| 73 |
*/ |
| 74 |
public function getMainRequest(): ?Request |
| 75 |
{ |
| 76 |
if (!$this->requests) { |
| 77 |
return null; |
| 78 |
} |
| 79 |
|
| 80 |
return $this->requests[0]; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Gets the master request. |
| 85 |
* |
| 86 |
* @return Request|null |
| 87 |
* |
| 88 |
* @deprecated since symfony/http-foundation 5.3, use getMainRequest() instead |
| 89 |
*/ |
| 90 |
public function getMasterRequest() |
| 91 |
{ |
| 92 |
trigger_deprecation('symfony/http-foundation', '5.3', '"%s()" is deprecated, use "getMainRequest()" instead.', __METHOD__); |
| 93 |
|
| 94 |
return $this->getMainRequest(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Returns the parent request of the current. |
| 99 |
* |
| 100 |
* Be warned that making your code aware of the parent request |
| 101 |
* might make it un-compatible with other features of your framework |
| 102 |
* like ESI support. |
| 103 |
* |
| 104 |
* If current Request is the main request, it returns null. |
| 105 |
* |
| 106 |
* @return Request|null |
| 107 |
*/ |
| 108 |
public function getParentRequest() |
| 109 |
{ |
| 110 |
$pos = \count($this->requests) - 2; |
| 111 |
|
| 112 |
return $this->requests[$pos] ?? null; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Gets the current session. |
| 117 |
* |
| 118 |
* @throws SessionNotFoundException |
| 119 |
*/ |
| 120 |
public function getSession(): SessionInterface |
| 121 |
{ |
| 122 |
if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) { |
| 123 |
return $request->getSession(); |
| 124 |
} |
| 125 |
|
| 126 |
throw new SessionNotFoundException(); |
| 127 |
} |
| 128 |
} |
| 129 |
|