-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.php
More file actions
180 lines (138 loc) · 4.45 KB
/
Copy pathSession.php
File metadata and controls
180 lines (138 loc) · 4.45 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
/*
* This file is part of the QuidPHP package <https://quidphp.com>
* Author: Pierre-Philippe Emond <emondpph@gmail.com>
* License: https://github.com/quidphp/main/blob/master/LICENSE
*/
namespace Quid\Main\File;
use Quid\Base;
use Quid\Main;
// session
// class for a session storage file, which is serialized
class Session extends Serialize implements Main\Contract\Session, Main\Contract\FileStorage
{
// config
protected static array $config = [];
// storageDirname
// retounre le dirname du storage, le dossier ou les fichiers sessions sont gargés
final public static function storageDirname():string
{
return Base\Session::getSavePath(true);
}
// sessionSid
// retourne la clé de session
final public function sessionSid():string
{
return $this->filename();
}
// sessionData
// retourne les données de la session
final public function sessionData():string
{
return $this->readRaw();
}
// sessionWrite
// écrit de nouvelles données dans le fichier session
final public function sessionWrite(string $data):bool
{
$this->overwrite($data,['callback'=>null]);
return true;
}
// sessionUpdateTimestamp
// update le timestamp du fichier session, retourne true même si rien n'a changé
final public function sessionUpdateTimestamp():bool
{
$this->touch();
return true;
}
// sessionDestroy
// détruit le fichier de session
final public function sessionDestroy():bool
{
return $this->unlink();
}
// sessionDir
// retourne le directoire de session
final public static function sessionDir(string $path,string $name):string
{
$return = null;
if(!empty($path) && !empty($name))
$return = Base\Path::append($path,$name);
return $return;
}
// sessionPath
// retourne le chemin de session
final public static function sessionPath(string $path,string $name,string $sid):string
{
$return = null;
if(!empty($path) && !empty($name) && !empty($sid))
$return = Base\Path::append($path,$name,$sid);
return $return;
}
// sessionExists
// retourne vrai si le sid exists pour le nom donné
final public static function sessionExists(string $path,string $name,string $sid):bool
{
$path = static::sessionPath($path,$name,$sid);
return Base\File::is($path);
}
// sessionCreate
// crée une nouvelle session avec le nom et side donné
// retourne une classe qui implémente Contract\Session
final public static function sessionCreate(string $path,string $name,string $sid):?Main\Contract\Session
{
$return = null;
if(!static::sessionExists($path,$name,$sid))
{
$path = static::sessionPath($path,$name,$sid);
$return = static::new($path,['create'=>true]);
$return->resource();
}
return $return;
}
// sessionRead
// lit une session à partir d'un nom et d'un sid
// retourne une classe qui implémente Contract\Session
final public static function sessionRead(string $path,string $name,string $sid):?Main\Contract\Session
{
$return = null;
if(static::sessionExists($path,$name,$sid))
{
$path = static::sessionPath($path,$name,$sid);
$return = static::new($path);
}
return $return;
}
// sessionGarbageCollect
// lance le processus de garbageCollect pour le nom de session donné
final public static function sessionGarbageCollect(string $path,string $name,int $lifetime,$not=null):int
{
$return = 0;
$dir = static::sessionDir($path,$name);
$timestamp = Base\Datetime::now() - $lifetime;
$files = Base\Dir::getFormatSmaller($dir,$timestamp,'dateAccess');
if(!is_array($not))
$not = [$not];
foreach ($not as $k => $v)
{
if($v instanceof Main\Contract\Session)
$not[$k] = $v->path();
elseif(!is_string($v))
unset($not[$k]);
}
if(!empty($files))
{
$files = Base\Arr::keysStrip($not,$files);
if(!empty($files))
{
$paths = array_keys($files);
$return = Base\Finder::unlinks(...$paths);
}
}
return $return;
}
}
// init
Session::__init();
?>