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\Session; |
12
|
|
|
|
13
|
|
|
use Psr\Container\ContainerInterface; |
14
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Spiral\Core\Exception\ScopeException; |
17
|
|
|
use Spiral\Session\Middleware\SessionMiddleware; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Provides access to the currently active session scope. |
21
|
|
|
*/ |
22
|
|
|
final class SessionScope implements SessionInterface |
23
|
|
|
{ |
24
|
|
|
/** Locations for unnamed segments i.e. default segment. */ |
25
|
|
|
private const DEFAULT_SECTION = '_DEFAULT'; |
26
|
|
|
|
27
|
|
|
/** @var ContainerInterface */ |
28
|
|
|
private $container; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ContainerInterface $container |
32
|
|
|
*/ |
33
|
|
|
public function __construct(ContainerInterface $container) |
34
|
|
|
{ |
35
|
|
|
$this->container = $container; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @inheritDoc |
40
|
|
|
* |
41
|
|
|
* @throws ScopeException |
42
|
|
|
*/ |
43
|
|
|
public function isStarted(): bool |
44
|
|
|
{ |
45
|
|
|
return $this->getActiveSession()->isStarted(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @inheritDoc |
50
|
|
|
* |
51
|
|
|
* @throws ScopeException |
52
|
|
|
*/ |
53
|
|
|
public function resume() |
54
|
|
|
{ |
55
|
|
|
return $this->getActiveSession()->resume(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @inheritDoc |
60
|
|
|
* |
61
|
|
|
* @throws ScopeException |
62
|
|
|
*/ |
63
|
|
|
public function getID(): ?string |
64
|
|
|
{ |
65
|
|
|
return $this->getActiveSession()->getID(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @inheritDoc |
70
|
|
|
* |
71
|
|
|
* @throws ScopeException |
72
|
|
|
*/ |
73
|
|
|
public function regenerateID(): SessionInterface |
74
|
|
|
{ |
75
|
|
|
$this->getActiveSession()->regenerateID(); |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
* |
83
|
|
|
* @throws ScopeException |
84
|
|
|
*/ |
85
|
|
|
public function commit(): bool |
86
|
|
|
{ |
87
|
|
|
return $this->getActiveSession()->commit(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
* |
93
|
|
|
* @throws ScopeException |
94
|
|
|
*/ |
95
|
|
|
public function abort(): bool |
96
|
|
|
{ |
97
|
|
|
return $this->getActiveSession()->abort(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
* |
103
|
|
|
* @throws ScopeException |
104
|
|
|
*/ |
105
|
|
|
public function destroy(): bool |
106
|
|
|
{ |
107
|
|
|
return $this->getActiveSession()->destroy(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @inheritDoc |
112
|
|
|
* |
113
|
|
|
* @throws ScopeException |
114
|
|
|
*/ |
115
|
|
|
public function getSection(string $name = null): SessionSectionInterface |
116
|
|
|
{ |
117
|
|
|
return new SectionScope($this, $name ?? self::DEFAULT_SECTION); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return SessionInterface |
122
|
|
|
* |
123
|
|
|
* @throws ScopeException |
124
|
|
|
*/ |
125
|
|
|
public function getActiveSession(): SessionInterface |
126
|
|
|
{ |
127
|
|
|
try { |
128
|
|
|
$request = $this->container->get(ServerRequestInterface::class); |
129
|
|
|
$session = $request->getAttribute(SessionMiddleware::ATTRIBUTE); |
130
|
|
|
if ($session === null) { |
131
|
|
|
throw new ScopeException('Unable to receive active Session, invalid request scope'); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $session; |
135
|
|
|
} catch (NotFoundExceptionInterface $e) { |
136
|
|
|
throw new ScopeException('Unable to receive active session', $e->getCode(), $e); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|