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