1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Spiral\Bootloader\Http; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
14
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
15
|
|
|
use Spiral\Boot\DirectoriesInterface; |
16
|
|
|
use Spiral\Config\ConfiguratorInterface; |
17
|
|
|
use Spiral\Core\Container\Autowire; |
18
|
|
|
use Spiral\Core\Exception\ScopeException; |
19
|
|
|
use Spiral\Session\Handler\FileHandler; |
20
|
|
|
use Spiral\Session\Middleware\SessionMiddleware; |
21
|
|
|
use Spiral\Session\SessionInterface; |
22
|
|
|
|
23
|
|
|
//use Spiral\Session\SectionInterface; |
24
|
|
|
|
25
|
|
|
final class SessionBootloader extends Bootloader |
26
|
|
|
{ |
27
|
|
|
protected const DEPENDENCIES = [ |
28
|
|
|
HttpBootloader::class, |
29
|
|
|
CookiesBootloader::class |
30
|
|
|
]; |
31
|
|
|
|
32
|
|
|
protected const BINDINGS = [ |
33
|
|
|
SessionInterface::class => [self::class, 'session'] |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
/** @var ConfiguratorInterface */ |
37
|
|
|
private $config; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param ConfiguratorInterface $config |
41
|
|
|
*/ |
42
|
|
|
public function __construct(ConfiguratorInterface $config) |
43
|
|
|
{ |
44
|
|
|
$this->config = $config; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Automatically registers session starter middleware and excludes session cookie from |
49
|
|
|
* cookie protection. |
50
|
|
|
* |
51
|
|
|
* @param ConfiguratorInterface $config |
52
|
|
|
* @param CookiesBootloader $cookies |
53
|
|
|
* @param HttpBootloader $http |
54
|
|
|
* @param DirectoriesInterface $directories |
55
|
|
|
*/ |
56
|
|
|
public function boot( |
57
|
|
|
ConfiguratorInterface $config, |
58
|
|
|
CookiesBootloader $cookies, |
59
|
|
|
HttpBootloader $http, |
60
|
|
|
DirectoriesInterface $directories |
61
|
|
|
): void { |
62
|
|
|
$config->setDefaults('session', [ |
63
|
|
|
'lifetime' => 86400, |
64
|
|
|
'cookie' => 'sid', |
65
|
|
|
'secure' => false, |
66
|
|
|
'handler' => new Autowire( |
67
|
|
|
FileHandler::class, |
68
|
|
|
[ |
69
|
|
|
'directory' => $directories->get('runtime') . 'session', |
70
|
|
|
'lifetime' => 86400 |
71
|
|
|
] |
72
|
|
|
) |
73
|
|
|
]); |
74
|
|
|
|
75
|
|
|
$session = $config->getConfig('session'); |
76
|
|
|
|
77
|
|
|
$http->addMiddleware(SessionMiddleware::class); |
78
|
|
|
$cookies->whitelistCookie($session['cookie']); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param ServerRequestInterface $request |
83
|
|
|
* @return SessionInterface |
84
|
|
|
*/ |
85
|
|
|
private function session(ServerRequestInterface $request): SessionInterface |
86
|
|
|
{ |
87
|
|
|
$session = $request->getAttribute(SessionMiddleware::ATTRIBUTE, null); |
88
|
|
|
if ($session === null) { |
89
|
|
|
throw new ScopeException('Unable to resolve Session, invalid request scope'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $session; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|