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