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\Session\Handler; |
13
|
|
|
|
14
|
|
|
use Spiral\Files\FilesInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Stores session data in file. |
18
|
|
|
*/ |
19
|
|
|
final class FileHandler implements \SessionHandlerInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var FilesInterface |
23
|
|
|
*/ |
24
|
|
|
protected $files; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $directory = ''; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param FilesInterface $files |
33
|
|
|
* @param string $directory |
34
|
|
|
* @param int $lifetime Default session lifetime. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(FilesInterface $files, string $directory, int $lifetime = 0) |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$this->files = $files; |
39
|
|
|
$this->directory = $directory; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
public function close(): bool |
46
|
|
|
{ |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
* @codeCoverageIgnore |
53
|
|
|
*/ |
54
|
|
|
public function destroy($session_id): bool |
55
|
|
|
{ |
56
|
|
|
return $this->files->delete($this->getFilename($session_id)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
* @codeCoverageIgnore |
62
|
|
|
*/ |
63
|
|
|
public function gc($maxlifetime): void |
64
|
|
|
{ |
65
|
|
|
foreach ($this->files->getFiles($this->directory) as $filename) { |
66
|
|
|
if ($this->files->time($filename) < time() - $maxlifetime) { |
67
|
|
|
$this->files->delete($filename); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritdoc |
74
|
|
|
*/ |
75
|
|
|
public function open($save_path, $session_id): bool |
76
|
|
|
{ |
77
|
|
|
return true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritdoc |
82
|
|
|
*/ |
83
|
|
|
public function read($session_id): string |
84
|
|
|
{ |
85
|
|
|
return $this->files->exists($this->getFilename($session_id)) |
86
|
|
|
? $this->files->read($this->getFilename($session_id)) |
87
|
|
|
: ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritdoc |
92
|
|
|
*/ |
93
|
|
|
public function write($session_id, $session_data): bool |
94
|
|
|
{ |
95
|
|
|
return $this->files->write( |
96
|
|
|
$this->getFilename($session_id), |
97
|
|
|
$session_data, |
98
|
|
|
FilesInterface::RUNTIME, |
99
|
|
|
true |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Session data filename. |
105
|
|
|
* |
106
|
|
|
* @param string $session_id |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function getFilename($session_id): string |
111
|
|
|
{ |
112
|
|
|
return "{$this->directory}/{$session_id}"; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.