Debian Codegraph @

lib/3rdParty/composer/symfony/http-foundation/RequestStack.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 <[email protected]>
21 */
22 class RequestStack
23 {
24 /**
25 * @var Request[]
26 */
27 private array $requests = [];
28
29 /**
30 * @param Request[] $requests
31 */
32 public function __construct(array $requests = [])
33 {
34 foreach ($requests as $request) {
35 $this->push($request);
36 }
37 }
38
39 /**
40 * Pushes a Request on the stack.
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 public function push(Request $request): void
46 {
47 $this->requests[] = $request;
48 }
49
50 /**
51 * Pops the current request from the stack.
52 *
53 * This operation lets the current request go out of scope.
54 *
55 * This method should generally not be called directly as the stack
56 * management should be taken care of by the application itself.
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 * Gets the main request.
74 *
75 * Be warned that making your code aware of the main request
76 * might make it un-compatible with other features of your framework
77 * like ESI support.
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 * Returns the parent request of the current.
90 *
91 * Be warned that making your code aware of the parent request
92 * might make it un-compatible with other features of your framework
93 * like ESI support.
94 *
95 * If current Request is the main request, it returns null.
96 */
97 public function getParentRequest(): ?Request
98 {
99 $pos = \count($this->requests) - 2;
100
101 return $this->requests[$pos] ?? null;
102 }
103
104 /**
105 * Gets the current session.
106 *
107 * @throws SessionNotFoundException
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 }