-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativeSessionContext.php
More file actions
114 lines (92 loc) · 3.03 KB
/
Copy pathNativeSessionContext.php
File metadata and controls
114 lines (92 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php declare(strict_types=1);
/*
* This file is part of Polymorphine/Session package.
*
* (c) Shudd3r <q3.shudder@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Polymorphine\Session\SessionContext;
use Psr\Http\Server\MiddlewareInterface;
use Polymorphine\Session\SessionContext;
use Polymorphine\Session\SessionStorageProvider;
use Polymorphine\Session\SessionStorage;
use Polymorphine\Session\SessionStorage\ContextSessionStorage;
use Polymorphine\Headers\Cookie;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
class NativeSessionContext implements MiddlewareInterface, SessionContext, SessionStorageProvider
{
private ContextSessionStorage $sessionData;
private Cookie $cookie;
private bool $sessionStarted = false;
private bool $regenerateId = false;
/**
* @param Cookie $cookie
*/
public function __construct(Cookie $cookie)
{
$this->cookie = $cookie;
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
if ($this->containsSessionCookie($request)) { $this->start(); }
$this->sessionData = new ContextSessionStorage($this, $_SESSION ?? []);
$response = $handler->handle($request);
$this->sessionData->commit();
return $response;
}
public function storage(): SessionStorage
{
if (!isset($this->sessionData)) {
throw new RuntimeException('Session context not started');
}
return $this->sessionData;
}
public function reset(): void
{
if (!$this->sessionStarted) { return; }
$this->regenerateId = true;
}
public function commit(array $data): void
{
if (!$data) {
$this->destroy();
return;
}
if (!$this->sessionStarted) {
$this->start();
$this->cookie->send(session_id());
} elseif ($this->regenerateId) {
session_regenerate_id(true);
$this->cookie->send(session_id());
}
if ($_SESSION === $data) { return; }
$_SESSION = $data;
session_write_close();
}
private function start(): void
{
if (session_status() !== PHP_SESSION_NONE) {
throw new RuntimeException('Session started in another context');
}
session_start();
$this->sessionStarted = true;
}
private function destroy(): void
{
if (!$this->sessionStarted) { return; }
$this->cookie->revoke();
session_destroy();
}
private function containsSessionCookie(ServerRequestInterface $request): bool
{
$sessionName = $this->cookie->name();
if (session_name() !== $sessionName) { session_name($sessionName); }
$cookies = $request->getCookieParams();
return isset($cookies[$sessionName]);
}
}