Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| SessionWrapper | |
100.00% |
10 / 10 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| remove | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Session wrapper to use Symfony Session instead of direct $_SESSION access. |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 8 | * obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2025-2026 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2025-08-04 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Session; |
| 21 | |
| 22 | use Symfony\Component\HttpFoundation\Session\Session; |
| 23 | use Symfony\Component\HttpFoundation\Session\Storage\PhpBridgeSessionStorage; |
| 24 | |
| 25 | class SessionWrapper |
| 26 | { |
| 27 | private Session $session; |
| 28 | |
| 29 | public function __construct(?Session $session = null) |
| 30 | { |
| 31 | if ($session instanceof Session) { |
| 32 | $this->session = $session; |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | // If no session is provided, create one with PhpBridgeSessionStorage |
| 37 | // This connects to the existing PHP session |
| 38 | $this->session = new Session(new PhpBridgeSessionStorage()); |
| 39 | if (!$this->session->isStarted()) { |
| 40 | $this->session->start(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Get a value from the session |
| 46 | */ |
| 47 | public function get(string $key, mixed $default = null): mixed |
| 48 | { |
| 49 | return $this->session->get($key, $default); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Set a value in the session |
| 54 | */ |
| 55 | public function set(string $key, mixed $value): void |
| 56 | { |
| 57 | $this->session->set($key, $value); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Check if a key exists in the session |
| 62 | */ |
| 63 | public function has(string $key): bool |
| 64 | { |
| 65 | return $this->session->has($key); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Remove a key from the session |
| 70 | */ |
| 71 | public function remove(string $key): mixed |
| 72 | { |
| 73 | return $this->session->remove($key); |
| 74 | } |
| 75 | } |