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