-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpFoundation] add a handler to store sessions in a PSR-6 cache #19193
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
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/CachePoolSessionHandler.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?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; | ||
use Psr\Cache\InvalidArgumentException; | ||
|
||
/** | ||
* Stores PHP sessions in a {@see CacheItemPoolInterface PSR-6 cache item pool}. | ||
* | ||
* @author Christian Flothmann <christian.flothmann@xabbuh.de> | ||
*/ | ||
class CachePoolSessionHandler implements \SessionHandlerInterface | ||
{ | ||
private $cachePool; | ||
private $expiresAfter; | ||
|
||
/** | ||
* Creates a session handler that wraps a cache item pool using the given options. | ||
* | ||
* Available options are: | ||
* * expires_after: The maximum lifetime of a session in seconds | ||
* | ||
* @param CacheItemPoolInterface $cachePool | ||
* @param array $options | ||
*/ | ||
public function __construct(CacheItemPoolInterface $cachePool, array $options = array()) | ||
{ | ||
if (count($diff = array_diff(array_keys($options), array('expires_after'))) > 0) { | ||
throw new \InvalidArgumentException(sprintf('The following options are not supported "%s".', implode('", "', $diff))); | ||
} | ||
|
||
$this->cachePool = $cachePool; | ||
$this->expiresAfter = isset($options['expires_after']) ? (int) $options['expires_after'] : (int) ini_get('session.gc_maxlifetime'); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function open($savePath, $sessionId) | ||
{ | ||
try { | ||
$this->cachePool->getItem($sessionId); | ||
|
||
return true; | ||
} catch (InvalidArgumentException $e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function close() | ||
{ | ||
// there is nothing like a close operation for PSR-6 cache pools | ||
return true; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($sessionId) | ||
{ | ||
try { | ||
$item = $this->cachePool->getItem($sessionId); | ||
|
||
return $item->isHit() ? $item->get() : ''; | ||
} catch (InvalidArgumentException $e) { | ||
return ''; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($sessionId, $sessionData) | ||
{ | ||
try { | ||
$item = $this->cachePool->getItem($sessionId); | ||
$item->set($sessionData); | ||
$item->expiresAfter($this->expiresAfter); | ||
|
||
return $this->cachePool->save($item); | ||
} catch (InvalidArgumentException $e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function destroy($sessionId) | ||
{ | ||
try { | ||
return $this->cachePool->deleteItem($sessionId); | ||
} catch (InvalidArgumentException $e) { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function gc($maxLifetime) | ||
{ | ||
// cache pools must implement garbage collection on their own | ||
return true; | ||
} | ||
} |
240 changes: 240 additions & 0 deletions
240
...ny/Component/HttpFoundation/Tests/Session/Storage/Handler/CachePoolSessionHandlerTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
<?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 Psr\Cache\CacheItemInterface; | ||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Cache\InvalidArgumentException as BaseInvalidArgumentException; | ||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\CachePoolSessionHandler; | ||
|
||
class CachePoolSessionHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @var \PHPUnit_Framework_MockObject_MockObject | ||
*/ | ||
private $cachePool; | ||
|
||
/** | ||
* @var CachePoolSessionHandler | ||
*/ | ||
private $sessionHandler; | ||
|
||
protected function setUp() | ||
{ | ||
$this->cachePool = $this->getMock(CacheItemPoolInterface::class); | ||
$this->sessionHandler = new CachePoolSessionHandler($this->cachePool, array()); | ||
} | ||
|
||
public function testOpenSucceeds() | ||
{ | ||
$this->assertTrue($this->sessionHandler->open('save-path', 'id')); | ||
} | ||
|
||
public function testOpenReturnsFalseIfTheCacheItemCannotBeRead() | ||
{ | ||
$this->cachePoolThrowsException('getItem'); | ||
|
||
$this->assertFalse($this->sessionHandler->open('save-path', 'id')); | ||
} | ||
|
||
public function testClose() | ||
{ | ||
$this->assertTrue($this->sessionHandler->close()); | ||
} | ||
|
||
public function testReadReturnsCachedDataOnSuccess() | ||
{ | ||
$this->cachePoolReturnsItemWithHit(); | ||
|
||
$this->assertSame('data', $this->sessionHandler->read('id')); | ||
} | ||
|
||
public function testReadReturnsEmptyStringIfNoCacheItemWasFound() | ||
{ | ||
$this->cachePoolReturnsItemWithoutHit(); | ||
|
||
$this->assertSame('', $this->sessionHandler->read('id')); | ||
} | ||
|
||
public function testReadReturnsEmptyStringIfTheCacheItemCannotBeRead() | ||
{ | ||
$this->cachePoolThrowsException('getItem'); | ||
|
||
$this->assertSame('', $this->sessionHandler->read('id')); | ||
} | ||
|
||
public function testWriteReturnsTrueWhenTheCacheItemCouldBeSaved() | ||
{ | ||
$this->cachePoolReturnsItemWithHit(); | ||
$this | ||
->cachePool | ||
->expects($this->once()) | ||
->method('save') | ||
->willReturn(true) | ||
; | ||
|
||
$this->assertTrue($this->sessionHandler->write('id', 'data')); | ||
} | ||
|
||
public function testWriteReturnsFalseWhenTheCacheItemCouldNotBeSaved() | ||
{ | ||
$this->cachePoolReturnsItemWithHit(); | ||
$this | ||
->cachePool | ||
->expects($this->once()) | ||
->method('save') | ||
->willReturn(false) | ||
; | ||
|
||
$this->assertFalse($this->sessionHandler->write('id', 'data')); | ||
} | ||
|
||
public function testWriteReturnsFalseWhenTheCachePoolThrowsAnException() | ||
{ | ||
$this->cachePoolThrowsException('getItem'); | ||
|
||
$this->assertFalse($this->sessionHandler->write('id', 'data')); | ||
} | ||
|
||
public function testWriteRespectsCustomMaxLifetime() | ||
{ | ||
$this->sessionHandler = new CachePoolSessionHandler($this->cachePool, array('expires_after' => 86400)); | ||
$item = $this->cachePoolReturnsItemWithHit(); | ||
$item | ||
->expects($this->atLeastOnce()) | ||
->method('expiresAfter') | ||
->with(86400) | ||
; | ||
$this | ||
->cachePool | ||
->expects($this->once()) | ||
->method('save') | ||
->willReturn(true) | ||
; | ||
|
||
$this->assertTrue($this->sessionHandler->write('id', 'data')); | ||
} | ||
|
||
public function testDestroyReturnsTrueWhenTheCacheItemWasRemoved() | ||
{ | ||
$this | ||
->cachePool | ||
->expects($this->once()) | ||
->method('deleteItem') | ||
->willReturn(true) | ||
; | ||
|
||
$this->assertTrue($this->sessionHandler->destroy('id')); | ||
} | ||
|
||
public function testDestroyReturnsFalseWhenTheCacheItemWasNotRemoved() | ||
{ | ||
$this | ||
->cachePool | ||
->expects($this->once()) | ||
->method('deleteItem') | ||
->willReturn(false) | ||
; | ||
|
||
$this->assertFalse($this->sessionHandler->destroy('id')); | ||
} | ||
|
||
public function testDestroyReturnsFalseWhenTheCachePoolThrowsAnException() | ||
{ | ||
$this->cachePoolThrowsException('deleteItem'); | ||
|
||
$this->assertFalse($this->sessionHandler->destroy('id')); | ||
} | ||
|
||
public function testGcSucceeds() | ||
{ | ||
$this->assertTrue($this->sessionHandler->gc(1000)); | ||
} | ||
|
||
/** | ||
* @dataProvider getOptionFixtures | ||
*/ | ||
public function testSupportedOptions($options, $supported) | ||
{ | ||
try { | ||
new CachePoolSessionHandler($this->cachePool, $options); | ||
|
||
$this->assertTrue($supported); | ||
} catch (\InvalidArgumentException $e) { | ||
$this->assertFalse($supported); | ||
} | ||
} | ||
|
||
public function getOptionFixtures() | ||
{ | ||
return array( | ||
array(array('prefix' => 'session'), false), | ||
array(array('expires_after' => 100), true), | ||
array(array('expires_after' => 100, 'foo' => 'bar'), false), | ||
); | ||
} | ||
|
||
private function cachePoolReturnsItemWithHit() | ||
{ | ||
$item = $this->getMock(CacheItemInterface::class); | ||
$item | ||
->expects($this->any()) | ||
->method('isHit') | ||
->willReturn(true) | ||
; | ||
$item | ||
->expects($this->any()) | ||
->method('get') | ||
->willReturn('data') | ||
; | ||
$this | ||
->cachePool | ||
->expects($this->any()) | ||
->method('getItem') | ||
->with('id') | ||
->willReturn($item) | ||
; | ||
|
||
return $item; | ||
} | ||
|
||
private function cachePoolReturnsItemWithoutHit() | ||
{ | ||
$item = $this->getMock(CacheItemInterface::class); | ||
$item | ||
->expects($this->any()) | ||
->method('isHit') | ||
->willReturn(false) | ||
; | ||
$this | ||
->cachePool | ||
->expects($this->any()) | ||
->method('getItem') | ||
->with('id') | ||
->willReturn($item) | ||
; | ||
} | ||
|
||
private function cachePoolThrowsException($method) | ||
{ | ||
$this | ||
->cachePool | ||
->expects($this->any()) | ||
->method($method) | ||
->willThrowException($this->getMock(InvalidArgumentException::class)) | ||
; | ||
} | ||
} | ||
|
||
class InvalidArgumentException extends \Exception implements BaseInvalidArgumentException | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if this is necessary. this mean every session reads the cache twice
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's debatable. I am not sure either. The thing is that
read()
cannot indicate that it failed. Another possible solution is to keep the read cache item in memory here and thus be able to avoid the second call togetItem()
inread()
.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean it can only fail for invalid session id chars. And since there is no fallback solution like encoding chars, I don't see the point of indicating failure. So it would be the same as non-existing session.