| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class RequestStack |
| 23 |
{ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private array $requests = []; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function __construct(array $requests = []) |
| 33 |
{ |
| 34 |
foreach ($requests as $request) { |
| 35 |
$this->push($request); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function push(Request $request): void |
| 46 |
{ |
| 47 |
$this->requests[] = $request; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function pop(): ?Request |
| 59 |
{ |
| 60 |
if (!$this->requests) { |
| 61 |
return null; |
| 62 |
} |
| 63 |
|
| 64 |
return array_pop($this->requests); |
| 65 |
} |
| 66 |
|
| 67 |
public function getCurrentRequest(): ?Request |
| 68 |
{ |
| 69 |
return end($this->requests) ?: null; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function getMainRequest(): ?Request |
| 80 |
{ |
| 81 |
if (!$this->requests) { |
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
return $this->requests[0]; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function getParentRequest(): ?Request |
| 98 |
{ |
| 99 |
$pos = \count($this->requests) - 2; |
| 100 |
|
| 101 |
return $this->requests[$pos] ?? null; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function getSession(): SessionInterface |
| 110 |
{ |
| 111 |
if ((null !== $request = end($this->requests) ?: null) && $request->hasSession()) { |
| 112 |
return $request->getSession(); |
| 113 |
} |
| 114 |
|
| 115 |
throw new SessionNotFoundException(); |
| 116 |
} |
| 117 |
|
| 118 |
public function resetRequestFormats(): void |
| 119 |
{ |
| 120 |
static $resetRequestFormats; |
| 121 |
$resetRequestFormats ??= \Closure::bind(static fn () => self::$formats = null, null, Request::class); |
| 122 |
$resetRequestFormats(); |
| 123 |
} |
| 124 |
} |