|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use HansOtt\PSR7Cookies\RequestCookies; |
|
6
|
|
|
use HansOtt\PSR7Cookies\SetCookie; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use React\Cache\CacheInterface; |
|
10
|
|
|
use Throwable; |
|
11
|
|
|
use function React\Promise\resolve; |
|
12
|
|
|
|
|
13
|
|
|
final class SessionMiddleware |
|
14
|
|
|
{ |
|
15
|
|
|
const ATTRIBUTE_NAME = 'wyrihaximus.react.http.middleware.session'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $cookieName; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var CacheInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $cache; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $cookieName |
|
29
|
|
|
* @param CacheInterface $cache |
|
30
|
|
|
*/ |
|
31
|
1 |
|
public function __construct(string $cookieName, CacheInterface $cache) |
|
32
|
|
|
{ |
|
33
|
1 |
|
$this->cookieName = $cookieName; |
|
34
|
1 |
|
$this->cache = $cache; |
|
35
|
1 |
|
} |
|
36
|
|
|
|
|
37
|
1 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
|
38
|
|
|
{ |
|
39
|
1 |
|
$id = $this->getId($request); |
|
40
|
|
|
|
|
41
|
|
|
return $this->cache->get($id)->otherwise(function () { |
|
42
|
|
|
return resolve([]); |
|
43
|
|
|
})->then(function ($sessionData) use ($next, $request, $id) { |
|
44
|
1 |
|
$session = new Session($sessionData); |
|
45
|
1 |
|
$request = $request->withAttribute(self::ATTRIBUTE_NAME, $session); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
1 |
|
return resolve($next($request))->then(function (ResponseInterface $response) use ($id, $session) { |
|
48
|
1 |
|
$this->cache->set($id, $session->getContents()); |
|
49
|
1 |
|
$cookie = new SetCookie($this->cookieName, $id); |
|
50
|
1 |
|
$response = $cookie->addToResponse($response); |
|
51
|
|
|
|
|
52
|
1 |
|
return $response; |
|
53
|
1 |
|
}); |
|
54
|
1 |
|
}); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
private function getId(ServerRequestInterface $request): string |
|
58
|
|
|
{ |
|
59
|
1 |
|
$cookies = RequestCookies::createFromRequest($request); |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
1 |
|
if ($cookies->has($this->cookieName)) { |
|
63
|
1 |
|
return $cookies->get($this->cookieName)->getValue(); |
|
64
|
|
|
} |
|
65
|
|
|
} catch (Throwable $et) { |
|
66
|
|
|
// Do nothing, only a not found will be thrown so generating our own id now |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return bin2hex(random_bytes(128)); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
It seems like you are assigning to a variable which was imported through a
usestatement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope