Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
30 / 30 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
SessionServiceProvider | |
100.00% |
30 / 30 |
|
100.00% |
3 / 3 |
6 | |
100.00% |
1 / 1 |
register | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
getSessionStorage | |
100.00% |
17 / 17 |
|
100.00% |
1 / 1 |
2 | |||
isCli | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | declare(strict_types=1); |
4 | |
5 | namespace Engelsystem\Http; |
6 | |
7 | use Engelsystem\Config\Config; |
8 | use Engelsystem\Container\ServiceProvider; |
9 | use Engelsystem\Http\SessionHandlers\DatabaseHandler; |
10 | use Illuminate\Support\Str; |
11 | use Symfony\Component\HttpFoundation\Session\Session; |
12 | use Symfony\Component\HttpFoundation\Session\SessionInterface; |
13 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
14 | use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; |
15 | use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface; |
16 | |
17 | class SessionServiceProvider extends ServiceProvider |
18 | { |
19 | public function register(): void |
20 | { |
21 | /** @var Request $request */ |
22 | $request = $this->app->get('request'); |
23 | |
24 | $sessionStorage = $this->getSessionStorage($request); |
25 | $this->app->instance('session.storage', $sessionStorage); |
26 | $this->app->bind(SessionStorageInterface::class, 'session.storage'); |
27 | |
28 | $session = $this->app->make(Session::class); |
29 | $this->app->instance(Session::class, $session); |
30 | $this->app->instance('session', $session); |
31 | $this->app->bind(SessionInterface::class, Session::class); |
32 | |
33 | if (!$session->has('_token')) { |
34 | $session->set('_token', Str::random(42)); |
35 | } |
36 | |
37 | $request->setSession($session); |
38 | |
39 | $session->start(); |
40 | } |
41 | |
42 | /** |
43 | * Returns the session storage |
44 | */ |
45 | protected function getSessionStorage(Request $request): SessionStorageInterface |
46 | { |
47 | if ($this->isCli()) { |
48 | return $this->app->make(MockArraySessionStorage::class); |
49 | } |
50 | |
51 | /** @var Config $config */ |
52 | $config = $this->app->get('config'); |
53 | $sessionConfig = $config->get('session'); |
54 | |
55 | $handler = match ($sessionConfig['driver']) { |
56 | 'pdo' => $this->app->make(DatabaseHandler::class), |
57 | default => null, |
58 | }; |
59 | |
60 | return $this->app->make(NativeSessionStorage::class, [ |
61 | 'options' => [ |
62 | 'name' => $sessionConfig['name'], |
63 | 'cookie_secure' => $request->isSecure(), |
64 | 'cookie_httponly' => true, |
65 | 'cookie_lifetime' => (int) ($sessionConfig['lifetime'] * 24 * 60 * 60), |
66 | ], |
67 | 'handler' => $handler, |
68 | ]); |
69 | } |
70 | |
71 | /** |
72 | * Test if is called from cli |
73 | */ |
74 | protected function isCli(): bool |
75 | { |
76 | return PHP_SAPI == 'cli' || PHP_SAPI == 'phpdbg'; |
77 | } |
78 | } |