1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Bootloader\Http; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
9
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
10
|
|
|
use Spiral\Boot\DirectoriesInterface; |
11
|
|
|
use Spiral\Bootloader\Http\Exception\ContextualObjectNotFoundException; |
12
|
|
|
use Spiral\Bootloader\Http\Exception\InvalidRequestScopeException; |
13
|
|
|
use Spiral\Config\ConfiguratorInterface; |
14
|
|
|
use Spiral\Core\BinderInterface; |
15
|
|
|
use Spiral\Core\Config\Proxy; |
16
|
|
|
use Spiral\Core\Container\Autowire; |
17
|
|
|
use Spiral\Framework\Spiral; |
18
|
|
|
use Spiral\Session\Config\SessionConfig; |
19
|
|
|
use Spiral\Session\Handler\FileHandler; |
20
|
|
|
use Spiral\Session\Middleware\SessionMiddleware; |
21
|
|
|
use Spiral\Session\SessionFactory; |
22
|
|
|
use Spiral\Session\SessionFactoryInterface; |
23
|
|
|
use Spiral\Session\SessionInterface; |
24
|
|
|
|
25
|
|
|
final class SessionBootloader extends Bootloader |
26
|
|
|
{ |
27
|
356 |
|
public function __construct( |
28
|
|
|
private readonly BinderInterface $binder, |
29
|
|
|
) { |
30
|
356 |
|
} |
31
|
|
|
|
32
|
356 |
|
public function defineBindings(): array |
33
|
|
|
{ |
34
|
356 |
|
$this->binder->getBinder(Spiral::HttpRequest)->bind(SessionInterface::class, $this->resolveSession(...)); |
|
|
|
|
35
|
356 |
|
$this->binder->getBinder(Spiral::Http) |
36
|
356 |
|
->bind( |
37
|
356 |
|
SessionInterface::class, |
38
|
356 |
|
new Proxy(SessionInterface::class, false, $this->resolveSession(...)), |
39
|
356 |
|
); |
40
|
356 |
|
$this->binder->bind(SessionInterface::class, new Proxy(SessionInterface::class, true), ); |
41
|
|
|
|
42
|
356 |
|
return []; |
43
|
|
|
} |
44
|
|
|
|
45
|
356 |
|
public function defineSingletons(): array |
46
|
|
|
{ |
47
|
356 |
|
$http = $this->binder->getBinder(Spiral::Http); |
48
|
356 |
|
$http->bindSingleton(SessionFactory::class, SessionFactory::class); |
49
|
356 |
|
$http->bindSingleton(SessionFactoryInterface::class, SessionFactory::class); |
50
|
|
|
|
51
|
356 |
|
$this->binder->bind(SessionFactoryInterface::class, new Proxy(SessionFactoryInterface::class, true)); |
52
|
|
|
|
53
|
356 |
|
return []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Automatically registers session starter middleware and excludes session cookie from |
58
|
|
|
* cookie protection. |
59
|
|
|
*/ |
60
|
356 |
|
public function init( |
61
|
|
|
ConfiguratorInterface $config, |
62
|
|
|
DirectoriesInterface $directories |
63
|
|
|
): void { |
64
|
356 |
|
$config->setDefaults( |
65
|
356 |
|
SessionConfig::CONFIG, |
66
|
356 |
|
[ |
67
|
356 |
|
'lifetime' => 86400, |
68
|
356 |
|
'cookie' => 'sid', |
69
|
356 |
|
'secure' => true, |
70
|
356 |
|
'sameSite' => null, |
71
|
356 |
|
'handler' => new Autowire( |
72
|
356 |
|
FileHandler::class, |
73
|
356 |
|
[ |
74
|
356 |
|
'directory' => $directories->get('runtime') . 'session', |
75
|
356 |
|
'lifetime' => 86400, |
76
|
356 |
|
] |
77
|
356 |
|
), |
78
|
356 |
|
] |
79
|
356 |
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
356 |
|
public function boot( |
83
|
|
|
ConfiguratorInterface $config, |
84
|
|
|
CookiesBootloader $cookies |
85
|
|
|
): void { |
86
|
356 |
|
$session = $config->getConfig(SessionConfig::CONFIG); |
87
|
|
|
|
88
|
356 |
|
$cookies->whitelistCookie($session['cookie']); |
89
|
|
|
} |
90
|
|
|
|
91
|
10 |
|
private function resolveSession(ContainerInterface $container): SessionInterface |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
try { |
94
|
|
|
/** @var ServerRequestInterface $request */ |
95
|
10 |
|
$request = $container->get(ServerRequestInterface::class); |
96
|
10 |
|
return $request->getAttribute(SessionMiddleware::ATTRIBUTE) ?? throw new ContextualObjectNotFoundException( |
97
|
10 |
|
SessionInterface::class, |
98
|
10 |
|
SessionMiddleware::ATTRIBUTE, |
99
|
10 |
|
); |
100
|
2 |
|
} catch (InvalidRequestScopeException $e) { |
101
|
1 |
|
throw new InvalidRequestScopeException(SessionInterface::class, previous: $e); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|