Passed
Push — master ( 436c58...98654e )
by Anton
06:21 queued 03:52
created

SessionBootloader::session()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Bootloader\Http;
13
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\Session\Handler\FileHandler;
19
use Spiral\Session\Middleware\SessionMiddleware;
20
21
//use Spiral\Session\SectionInterface;
22
23
final class SessionBootloader extends Bootloader
24
{
25
    protected const DEPENDENCIES = [
26
        HttpBootloader::class,
27
        CookiesBootloader::class
28
    ];
29
30
    /** @var ConfiguratorInterface */
31
    private $config;
32
33
    /**
34
     * @param ConfiguratorInterface $config
35
     */
36
    public function __construct(ConfiguratorInterface $config)
37
    {
38
        $this->config = $config;
39
    }
40
41
    /**
42
     * Automatically registers session starter middleware and excludes session cookie from
43
     * cookie protection.
44
     *
45
     * @param ConfiguratorInterface $config
46
     * @param CookiesBootloader     $cookies
47
     * @param HttpBootloader        $http
48
     * @param DirectoriesInterface  $directories
49
     */
50
    public function boot(
51
        ConfiguratorInterface $config,
52
        CookiesBootloader $cookies,
53
        HttpBootloader $http,
54
        DirectoriesInterface $directories
55
    ): void {
56
        $config->setDefaults('session', [
57
            'lifetime' => 86400,
58
            'cookie'   => 'sid',
59
            'secure'   => false,
60
            'handler'  => new Autowire(
61
                FileHandler::class,
62
                [
63
                    'directory' => $directories->get('runtime') . 'session',
64
                    'lifetime'  => 86400
65
                ]
66
            )
67
        ]);
68
69
        $session = $config->getConfig('session');
70
71
        $http->addMiddleware(SessionMiddleware::class);
72
        $cookies->whitelistCookie($session['cookie']);
73
    }
74
}
75