-
-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathSessionExtension.php
More file actions
97 lines (76 loc) · 2.59 KB
/
SessionExtension.php
File metadata and controls
97 lines (76 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Bridges\HttpDI;
use Nette;
use Nette\Http\IResponse;
use Nette\Schema\Expect;
/**
* Session extension for Nette DI.
*/
class SessionExtension extends Nette\DI\CompilerExtension
{
private bool $debugMode;
private bool $cliMode;
public function __construct(bool $debugMode = false, bool $cliMode = false)
{
$this->debugMode = $debugMode;
$this->cliMode = $cliMode;
}
public function getConfigSchema(): Nette\Schema\Schema
{
return Expect::structure([
'debugger' => Expect::bool(false),
'autoStart' => Expect::anyOf('smart', true, false)->firstIsDefault(),
'expiration' => Expect::string()->dynamic(),
'handler' => Expect::string()->dynamic(),
'readAndClose' => Expect::bool(),
'cookieSamesite' => Expect::anyOf(IResponse::SAME_SITE_LAX, IResponse::SAME_SITE_STRICT, IResponse::SAME_SITE_NONE)
->firstIsDefault(),
])->otherItems('mixed');
}
public function loadConfiguration()
{
$builder = $this->getContainerBuilder();
$config = $this->config;
$session = $builder->addDefinition($this->prefix('session'))
->setFactory(Nette\Http\Session::class);
if ($config->expiration) {
$session->addSetup('setExpiration', [$config->expiration]);
}
if ($config->handler) {
$session->addSetup('setHandler', [$config->handler]);
}
if (($config->cookieDomain ?? null) === 'domain') {
$config->cookieDomain = $builder::literal('$this->getByType(Nette\Http\IRequest::class)->getUrl()->getDomain(2)');
}
$this->compiler->addExportedType(Nette\Http\IRequest::class);
if ($this->debugMode && $config->debugger) {
$session->addSetup('@Tracy\Bar::addPanel', [
new Nette\DI\Definitions\Statement(Nette\Bridges\HttpTracy\SessionPanel::class),
]);
}
$options = (array) $config;
unset($options['expiration'], $options['handler'], $options['autoStart'], $options['debugger']);
if ($options['readAndClose'] === null) {
unset($options['readAndClose']);
}
if (!empty($options)) {
$session->addSetup('setOptions', [$options]);
}
if ($this->name === 'session') {
$builder->addAlias('session', $this->prefix('session'));
}
if (!$this->cliMode) {
$name = $this->prefix('session');
if ($config->autoStart === 'smart') {
$this->initialization->addBody('$this->getService(?)->exists() && $this->getService(?)->start();', [$name, $name]);
} elseif ($config->autoStart) {
$this->initialization->addBody('$this->getService(?)->start();', [$name]);
}
}
}
}