1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sebdesign\StackSession; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Session\ExistenceAwareInterface; |
7
|
|
|
use SessionHandlerInterface; |
8
|
|
|
|
9
|
|
|
class StackSessionHandler implements |
10
|
|
|
ExistenceAwareInterface, |
11
|
|
|
SessionHandlerInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The handler instances. |
15
|
|
|
* |
16
|
|
|
* @var iterable<string,\SessionHandlerInterface> |
17
|
|
|
*/ |
18
|
|
|
protected $handlers; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Create a new stack handler instance. |
22
|
|
|
* |
23
|
|
|
* @param iterable<string,\SessionHandlerInterface> $handlers |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function __construct(iterable $handlers) |
26
|
|
|
{ |
27
|
|
|
$this->handlers = $handlers; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
public function open($savePath, $sessionName) |
34
|
|
|
{ |
35
|
|
|
$this->apply(static function ( |
36
|
|
|
SessionHandlerInterface $handler |
37
|
|
|
) use ($savePath, $sessionName): void { |
38
|
|
|
$handler->open($savePath, $sessionName); |
39
|
|
|
}); |
40
|
|
|
|
41
|
|
|
return true; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function close() |
48
|
|
|
{ |
49
|
|
|
$this->apply(static function (SessionHandlerInterface $handler): void { |
50
|
|
|
$handler->close(); |
51
|
|
|
}); |
52
|
|
|
|
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
|
|
public function read($sessionId) |
60
|
|
|
{ |
61
|
|
|
foreach ($this->handlers as $handler) { |
62
|
|
|
$data = $handler->read($sessionId); |
63
|
|
|
|
64
|
|
|
if ($data !== '') { |
65
|
|
|
return $data; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return ''; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function write($sessionId, $data) |
76
|
|
|
{ |
77
|
|
|
$this->apply(static function ( |
78
|
|
|
SessionHandlerInterface $handler |
79
|
|
|
) use ($sessionId, $data): void { |
80
|
|
|
$handler->write($sessionId, $data); |
81
|
|
|
}); |
82
|
|
|
|
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function destroy($sessionId) |
90
|
|
|
{ |
91
|
|
|
$this->apply(static function ( |
92
|
|
|
SessionHandlerInterface $handler |
93
|
|
|
) use ($sessionId): void { |
94
|
|
|
$handler->destroy($sessionId); |
95
|
|
|
}); |
96
|
|
|
|
97
|
|
|
return true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritdoc} |
102
|
|
|
*/ |
103
|
|
|
public function gc($maxlifetime) |
104
|
|
|
{ |
105
|
|
|
$this->apply(static function ( |
106
|
|
|
SessionHandlerInterface $handler |
107
|
|
|
) use ($maxlifetime): void { |
108
|
|
|
$handler->gc($maxlifetime); |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function setExists($value) |
118
|
|
|
{ |
119
|
|
|
$this->apply(static function ( |
120
|
|
|
SessionHandlerInterface $handler |
121
|
|
|
) use ($value): void { |
122
|
|
|
if ($handler instanceof ExistenceAwareInterface) { |
|
|
|
|
123
|
|
|
$handler->setExists($value); |
124
|
|
|
} |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
protected function apply(Closure $callback): void |
131
|
|
|
{ |
132
|
|
|
foreach ($this->handlers as $handler) { |
133
|
|
|
$callback($handler); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.