8000 Adding PSR6 session handler by Nyholm · Pull Request #23321 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Adding PSR6 session handler #23321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.4.0
-----

* Added `Psr6SessionHandler`.

3.3.0
-----

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;

use Psr\Cache\CacheItemPoolInterface;

/**
* Session handler that supports a PSR6 cache implementation.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
class Psr6SessionHandler implements \SessionHandlerInterface
{
/**
* @var CacheItemPoolInterface
*/
private $cache;

/**
* @var int Time to live in seconds
*/
private $ttl;

/**
* @var string Key prefix for shared environments.
*/
private $prefix;

/**
* List of available options:
* * prefix: The prefix to use for the cache keys in order to avoid collision
* * ttl: The time to live in seconds.
*
* @param CacheItemPoolInterface $cache A Cache instance
* @param array $options An associative array of cache options
*/
public function __construct(CacheItemPoolInterface $cache, array $options = array())
{
$this->cache = $cache;

$this->ttl = isset($options['ttl']) ? (int) $options['ttl'] : 86400;
$this->prefix = isset($options['prefix']) ? $options['prefix'] : 'sfPsr6sess_';
}

/**
* {@inheritdoc}
*/
public function open($savePath, $sessionName)
{
return true;
}

/**
* {@inheritdoc}
*/
public function close()
{
return true;
}

/**
* {@inheritdoc}
*/
public function read($sessionId)
{
$item = $this->getCacheItem($sessionId);
if ($item->isHit()) {
return $item->get();
}

return '';
}

/**
* {@inheritdoc}
*/
public function write($sessionId, $data)
{
$item = $this->getCacheItem($sessionId);
$item->set($data)
->expiresAfter($this->ttl);

return $this->cache->save($item);
}

/**
* {@inheritdoc}
*/
public function destroy($sessionId)
{
return $this->cache->deleteItem($this->prefix.$sessionId);
}

/**
* {@inheritdoc}
*/
public function gc($lifetime)
{
// not required here because cache will auto expire the records anyhow.
return true;
}

/**
* @param string $sessionId
*
* @return \Psr\Cache\CacheItemInterface
*/
private function getCacheItem($sessionId)
{
return $this->cache->getItem($this->prefix.$sessionId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler;

use PHPUnit\Framework\TestCase;
use Psr\Cache\CacheItemInterface;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\Psr6SessionHandler;

/**
* @author Aaron Scherer <aequasi@gmail.com>
*/
class Psr6SessionHandlerTest extends TestCase
{
const TTL = 100;
const PREFIX = 'pre';

/**
* @var Psr6SessionHandler
*/
private $handler;

/**
* @var \PHPUnit_Framework_MockObject_MockObject|CacheItemPoolInterface
*/
private $psr6;

protected function setUp()
{
parent::setUp();

$this->psr6 = $this->getMockBuilder(Cache::class)
->setMethods(array('getItem', 'deleteItem', 'save'))
->getMock();
$this->handler = new Psr6SessionHandler($this->psr6, array('prefix' => self::PREFIX, 'ttl' => self::TTL));
}

public function testOpen()
{
$this->assertTrue($this->handler->open('foo', 'bar'));
}

public function testClose()
{
$this->assertTrue($this->handler->close());
}

public function testGc()
{
$this->assertTrue($this->handler->gc(4711));
}

public function testReadMiss()
{
$item = $this->getItemMock();
$item->expects($this->once())
->method('isHit')
->willReturn(false);
$this->psr6->expects($this->once())
->method('getItem')
->willReturn($item);

$this->assertEquals('', $this->handler->read('foo'));
}

public function testReadHit()
{
$item = $this->getItemMock();
$item->expects($this->once())
->method('isHit')
->willReturn(true);
$item->expects($this->once())
->method('get')
->willReturn('bar');
$this->psr6->expects($this->once())
->method('getItem')
->willReturn($item);

$this->assertEquals('bar', $this->handler->read('foo'));
}

public function testWrite()
{
$item = $this->getItemMock();

$item->expects($this->once())
->method('set')
->with('session value')
->willReturnSelf();
$item->expects($this->once())
->method('expiresAfter')
->with(self::TTL)
->willReturnSelf();

$this->psr6->expects($this->once())
->method('getItem')
->with(self::PREFIX.'foo')
->willReturn($item);

$this->psr6->expects($this->once())
->method('save')
->with($item)
->willReturn(true);

$this->assertTrue($this->handler->write('foo', 'session value'));
}

public function testDestroy()
{
$this->psr6->expects($this->once())
->method('deleteItem')
->with(self::PREFIX.'foo')
->willReturn(true);

$this->assertTrue($this->handler->destroy('foo'));
}

/**
* @return \PHPUnit_Framework_MockObject_MockObject
*/
private function getItemMock()
{
return $this->getMockBuilder(CacheItemInterface::class)
->setMethods(array('isHit', 'getKey', 'get', 'set', 'expiresAt', 'expiresAfter'))
->getMock();
}
}

class Cache implements CacheItemPoolInterface
{
public function getItem($key)
{
}

public function getItems(array $keys = array())
{
}

public function hasItem($key)
{
}

public function clear()
{
}

public function deleteItem($key)
{
}

public function deleteItems(array $keys)
{
}

public function save(CacheItemInterface $item)
{
}

public function saveDeferred(CacheItemInterface $item)
{
}

public function commit()
{
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"symfony/polyfill-mbstring": "~1.1"
},
"require-dev": {
"symfony/expression-language": "~2.8|~3.0|~4.0"
"symfony/expression-language": "~2.8|~3.0|~4.0",
"psr/cache": "~1.0"
},
"autoload": {
"psr-4": { "Symfony\\Component\\HttpFoundation\\": "" },
Expand Down
0