1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | /* |
||
6 | * This file is part of the Valkyrja Framework package. |
||
7 | * |
||
8 | * (c) Melech Mizrachi <[email protected]> |
||
9 | * |
||
10 | * For the full copyright and license information, please view the LICENSE |
||
11 | * file that was distributed with this source code. |
||
12 | */ |
||
13 | |||
14 | namespace Valkyrja\Session; |
||
15 | |||
16 | use Override; |
||
0 ignored issues
–
show
|
|||
17 | use Valkyrja\Session\Data\CookieParams; |
||
18 | use Valkyrja\Session\Exception\InvalidSessionId; |
||
19 | use Valkyrja\Session\Exception\SessionIdFailure; |
||
20 | use Valkyrja\Session\Exception\SessionNameFailure; |
||
21 | use Valkyrja\Session\Exception\SessionStartFailure; |
||
22 | |||
23 | use function headers_sent; |
||
24 | use function preg_match; |
||
25 | use function session_id; |
||
26 | use function session_name; |
||
27 | use function session_start; |
||
28 | use function session_status; |
||
29 | use function session_unset; |
||
30 | |||
31 | use const PHP_SESSION_ACTIVE; |
||
32 | |||
33 | /** |
||
34 | * Class PhpSession. |
||
35 | * |
||
36 | * @author Melech Mizrachi |
||
37 | */ |
||
38 | class PhpSession extends NullSession |
||
39 | { |
||
40 | public function __construct( |
||
41 | protected CookieParams $cookieParams, |
||
42 | string|null $sessionId = null, |
||
43 | string|null $sessionName = null, |
||
44 | ) { |
||
45 | parent::__construct( |
||
46 | sessionId: $sessionId, |
||
47 | sessionName: $sessionName |
||
48 | ); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @inheritDoc |
||
53 | */ |
||
54 | #[Override] |
||
55 | public function start(): void |
||
56 | { |
||
57 | // If the session is already active |
||
58 | if ($this->isActive() || headers_sent()) { |
||
59 | // No need to reactivate |
||
60 | return; |
||
61 | } |
||
62 | |||
63 | // Set the session cookie parameters |
||
64 | session_set_cookie_params([ |
||
65 | 'path' => $this->cookieParams->path, |
||
66 | 'domain' => $this->cookieParams->domain, |
||
67 | 'lifetime' => $this->cookieParams->lifetime, |
||
68 | 'secure' => $this->cookieParams->secure, |
||
69 | 'httponly' => $this->cookieParams->httpOnly, |
||
70 | 'samesite' => $this->cookieParams->sameSite->value, |
||
71 | ]); |
||
72 | |||
73 | // If the session failed to start |
||
74 | if (! session_start()) { |
||
75 | // Throw a new exception |
||
76 | throw new SessionStartFailure('The session failed to start'); |
||
77 | } |
||
78 | |||
79 | // Set the data |
||
80 | $this->data = &$_SESSION; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @inheritDoc |
||
85 | */ |
||
86 | #[Override] |
||
87 | public function getId(): string |
||
88 | { |
||
89 | $sessionId = session_id(); |
||
90 | |||
91 | if ($sessionId === false) { |
||
92 | throw new SessionIdFailure('Retrieval of session id failed'); |
||
93 | } |
||
94 | |||
95 | return $sessionId; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | #[Override] |
||
102 | public function setId(string $id): void |
||
103 | { |
||
104 | if (! preg_match('/^[-,a-zA-Z0-9]{1,128}$/', $id)) { |
||
105 | throw new InvalidSessionId( |
||
106 | "The session id, '$id', is invalid! " |
||
107 | . 'Session id can only contain alpha numeric characters, dashes, commas, ' |
||
108 | . 'and be at least 1 character in length but up to 128 characters long.' |
||
109 | ); |
||
110 | } |
||
111 | |||
112 | session_id($id); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @inheritDoc |
||
117 | */ |
||
118 | #[Override] |
||
119 | public function getName(): string |
||
120 | { |
||
121 | $sessionName = session_name(); |
||
122 | |||
123 | if ($sessionName === false) { |
||
124 | throw new SessionNameFailure('Retrieval of session id failed'); |
||
125 | } |
||
126 | |||
127 | return $sessionName; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @inheritDoc |
||
132 | */ |
||
133 | #[Override] |
||
134 | public function setName(string $name): void |
||
135 | { |
||
136 | session_name($name); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @inheritDoc |
||
141 | */ |
||
142 | #[Override] |
||
143 | public function isActive(): bool |
||
144 | { |
||
145 | return session_status() === PHP_SESSION_ACTIVE; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @inheritDoc |
||
150 | */ |
||
151 | #[Override] |
||
152 | public function clear(): void |
||
153 | { |
||
154 | parent::clear(); |
||
155 | |||
156 | session_unset(); |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @inheritDoc |
||
161 | */ |
||
162 | #[Override] |
||
163 | public function destroy(): void |
||
164 | { |
||
165 | parent::destroy(); |
||
166 | |||
167 | session_unset(); |
||
168 | } |
||
169 | } |
||
170 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths