Skip to content

Commit ec32114

Browse files
committed
[HttpFoundation] Add Psr6SessionHandler
1 parent 0ebc1f7 commit ec32114

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

src/Symfony/Component/HttpFoundation/Session/Storage/Handler/Psr6SessionHandler.php

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
* Session handler that supports a PSR6 cache implementation.
1818
*
1919
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+
* @author Ahmed TAILOULOUTE <ahmed.tailouloute@gmail.com>
2021
*/
21-
class Psr6SessionHandler implements \SessionHandlerInterface
22+
class Psr6SessionHandler extends AbstractSessionHandler
2223
{
2324
/**
2425
* @var CacheItemPoolInterface
@@ -31,7 +32,7 @@ class Psr6SessionHandler implements \SessionHandlerInterface
3132
private $ttl;
3233

3334
/**
34-
* @var string Key prefix for shared environments.
35+
* @var string Key prefix for shared environments
3536
*/
3637
private $prefix;
3738

@@ -43,61 +44,54 @@ class Psr6SessionHandler implements \SessionHandlerInterface
4344
* @param CacheItemPoolInterface $cache A Cache instance
4445
* @param array $options An associative array of cache options
4546
*/
46-
public function __construct(CacheItemPoolInterface $cache, array $options = array())
47+
public function __construct(CacheItemPoolInterface $cache, array $options = [])
4748
{
4849
$this->cache = $cache;
4950

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+
}
5354

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';
6057
}
6158

6259
/**
6360
* {@inheritdoc}
6461
*/
65-
public function close()
62+
protected function doRead(string $sessionId)
6663
{
67-
return true;
64+
$item = $this->cache->getItem($this->prefix.$sessionId);
65+
66+
return $item->isHit() ? $item->get() : '';
6867
}
6968

7069
/**
7170
* {@inheritdoc}
7271
*/
73-
public function read($sessionId)
72+
protected function doWrite(string $sessionId, string $data)
7473
{
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'));
7977

80-
return '';
78+
return $this->cache->save($item);
8179
}
8280

8381
/**
8482
* {@inheritdoc}
8583
*/
86-
public function write($sessionId, $data)
84+
protected function doDestroy(string $sessionId)
8785
{
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);
9387
}
9488

9589
/**
9690
* {@inheritdoc}
9791
*/
98-
public function destroy($sessionId)
92+
public function close()
9993
{
100-
return $this->cache->deleteItem($this->prefix.$sessionId);
94+
return true;
10195
}
10296

10397
/**
@@ -110,12 +104,13 @@ public function gc($lifetime)
110104
}
111105

112106
/**
113-
* @param string $sessionId
114-
*
115-
* @return \Psr\Cache\CacheItemInterface
107+
* {@inheritdoc}
116108
*/
117-
private function getCacheItem($sessionId)
109+
public function updateTimestamp($sessionId, $data)
118110
{
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);
120115
}
121116
}

src/Symfony/Component/HttpFoundation/composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@
2323
"require-dev": {
2424
"predis/predis": "~1.0",
2525
"symfony/mime": "^4.4|^5.0",
26-
"symfony/expression-language": "^4.4|^5.0"
26+
"symfony/expression-language": "^4.4|^5.0",
27+
"psr/cache": "~1.0"
2728
},
2829
"suggest" : {
29-
"symfony/mime": "To use the file extension guesser"
30+
"symfony/mime": "To use the file extension guesser",
31+
"psr/cache": "To use the Psr6SessionHandler"
3032
},
3133
"autoload": {
3234
"psr-4": { "Symfony\\Component\\HttpFoundation\\": "" },

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy