17
17
* Session handler that supports a PSR6 cache implementation.
18
18
*
19
19
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20
+ * @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
20
21
*/
21
- class Psr6SessionHandler implements \SessionHandlerInterface
22
+ class Psr6SessionHandler extends AbstractSessionHandler
22
23
{
23
24
/**
24
25
* @var CacheItemPoolInterface
@@ -31,7 +32,7 @@ class Psr6SessionHandler implements \SessionHandlerInterface
31
32
private $ ttl ;
32
33
33
34
/**
34
- * @var string Key prefix for shared environments.
35
+ * @var string Key prefix for shared environments
35
36
*/
36
37
private $ prefix ;
37
38
@@ -43,61 +44,54 @@ class Psr6SessionHandler implements \SessionHandlerInterface
43
44
* @param CacheItemPoolInterface $cache A Cache instance
44
45
* @param array $options An associative array of cache options
45
46
*/
46
- public function __construct (CacheItemPoolInterface $ cache , array $ options = array () )
47
+ public function __construct (CacheItemPoolInterface $ cache , array $ options = [] )
47
48
{
48
49
$ this ->cache = $ cache ;
49
50
50
- $ this -> ttl = isset ( $ options[ ' ttl ' ]) ? ( int ) $ options [ 'ttl ' ] : 86400 ;
51
- $ this -> prefix = isset ( $ options [ ' prefix ' ]) ? $ options[ ' prefix ' ] : ' sfPsr6sess_ ' ;
52
- }
51
+ if ( $ diff = array_diff ( array_keys ( $ options), [ ' prefix ' , 'ttl ' ])) {
52
+ throw new \ InvalidArgumentException ( sprintf ( ' The following options are not supported "%s" ' , implode ( ' , ' , $ diff ))) ;
53
+ }
53
54
54
- /**
55
- * {@inheritdoc}
56
- */
57
- public function open ($ savePath , $ sessionName )
58
- {
59
- return true ;
55
+ $ this ->ttl = $ options ['ttl ' ] ?? null ;
56
+ $ this ->prefix = $ options ['prefix ' ] ?? 'sf_s ' ;
60
57
}
61
58
62
59
/**
63
60
* {@inheritdoc}
64
61
*/
65
- public function close ( )
62
+ protected function doRead ( string $ sessionId )
66
63
{
67
- return true ;
64
+ $ item = $ this ->cache ->getItem ($ this ->prefix .$ sessionId );
65
+
66
+ return $ item ->isHit () ? $ item ->get () : '' ;
68
67
}
69
68
70
69
/**
71
70
* {@inheritdoc}
72
71
*/
73
- public function read ( $ sessionId )
72
+ protected function doWrite ( string $ sessionId, string $ data )
74
73
{
75
- $ item = $ this ->getCacheItem ($ sessionId );
76
- if ($ item ->isHit ()) {
77
- return $ item ->get ();
78
- }
74
+ $ item = $ this ->cache ->getItem ($ this ->prefix .$ sessionId );
75
+ $ item ->set ($ data )
76
+ ->expiresAfter ($ this ->ttl ?? ini_get ('session.gc_maxlifetime ' ));
79
77
80
- return '' ;
78
+ return $ this -> cache -> save ( $ item ) ;
81
79
}
82
80
83
81
/**
84
82
* {@inheritdoc}
85
83
*/
86
- public function write ( $ sessionId , $ data )
84
+ protected function doDestroy ( string $ sessionId )
87
85
{
88
- $ item = $ this ->getCacheItem ($ sessionId );
89
- $ item ->set ($ data )
90
- ->expiresAfter ($ this ->ttl );
91
-
92
- return $ this ->cache ->save ($ item );
86
+ return $ this ->cache ->deleteItem ($ this ->prefix .$ sessionId );
93
87
}
94
88
95
89
/**
96
90
* {@inheritdoc}
97
91
*/
98
- public function destroy ( $ sessionId )
92
+ public function close ( )
99
93
{
100
- return $ this -> cache -> deleteItem ( $ this -> prefix . $ sessionId ) ;
94
+ return true ;
101
95
}
102
96
103
97
/**
@@ -110,12 +104,13 @@ public function gc($lifetime)
110
104
}
111
105
112
106
/**
113
- * @param string $sessionId
114
- *
115
- * @return \Psr\Cache\CacheItemInterface
107
+ * {@inheritdoc}
116
108
*/
117
- private function getCacheItem ($ sessionId )
109
+ public function updateTimestamp ($ sessionId, $ data )
118
110
{
119
- return $ this ->cache ->getItem ($ this ->prefix .$ sessionId );
111
+ $ cacheItem = $ this ->cache ->getItem ($ this ->prefix .$ sessionId );
112
+ $ cacheItem ->expiresAfter ((int ) ($ this ->ttl ?? ini_get ('session.gc_maxlifetime ' )));
113
+
114
+ return $ this ->cache ->save ($ cacheItem );
120
115
}
121
116
}
0 commit comments