Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpFoundation] Add a way to avoid the session be written at each re…
…quest
- Loading branch information
1 parent
5ebaad3
commit 191418d
Showing
7 changed files
with
259 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/WriteCheckSessionHandler.php
This file contains 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,91 @@ | ||
<?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; | ||
|
||
/** | ||
* Wraps another SessionHandlerInterface to only write the session when it has been modified. | ||
* | ||
* @author Adrien Brault <adrien.brault@gmail.com> | ||
*/ | ||
class WriteCheckSessionHandler implements \SessionHandlerInterface | ||
{ | ||
/** | ||
* @var \SessionHandlerInterface | ||
*/ | ||
private $wrappedSessionHandler; | ||
|
||
/** | ||
* @var array sessionId => session | ||
*/ | ||
private $readSessions; | ||
|
||
public function __construct(\SessionHandlerInterface $wrappedSessionHandler) | ||
{ | ||
$this->wrappedSessionHandler = $wrappedSessionHandler; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function close() | ||
{ | ||
return $this->wrappedSessionHandler->close(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function destroy($sessionId) | ||
{ | ||
return $this->wrappedSessionHandler->destroy($sessionId); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function gc($maxLifetime) | ||
{ | ||
return $this->wrappedSessionHandler->gc($maxLifetime); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function open($savePath, $sessionId) | ||
{ | ||
return $this->wrappedSessionHandler->open($savePath, $sessionId); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function read($sessionId) | ||
{ | ||
$session = $this->wrappedSessionHandler->read($sessionId); | ||
|
||
$this->readSessions[$sessionId] = $session; | ||
|
||
return $session; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function write($sessionId, $sessionData) | ||
{ | ||
if (isset($this->readSessions[$sessionId]) && $sessionData === $this->readSessions[$sessionId]) { | ||
return true; | ||
} | ||
|
||
return $this->wrappedSessionHandler->write($sessionId, $sessionData); | ||
} | ||
} |
This file contains 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
94 changes: 94 additions & 0 deletions
94
...y/Component/HttpFoundation/Tests/Session/Storage/Handler/WriteCheckSessionHandlerTest.php
This file contains 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,94 @@ | ||
<?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 Symfony\Component\HttpFoundation\Session\Storage\Handler\WriteCheckSessionHandler; | ||
|
||
/** | ||
* @author Adrien Brault <adrien.brault@gmail.com> | ||
*/ | ||
class WriteCheckSessionHandlerTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function test() | ||
{ | ||
$wrappedSessionHandlerMock = $this->getMock('SessionHandlerInterface'); | ||
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock); | ||
|
||
$wrappedSessionHandlerMock | ||
->expects($this->once()) | ||
->method('close') | ||
->with() | ||
->will($this->returnValue(true)) | ||
; | ||
|
||
$this->assertEquals(true, $writeCheckSessionHandler->close()); | ||
} | ||
|
||
public function testWrite() | ||
{ | ||
$wrappedSessionHandlerMock = $this->getMock('SessionHandlerInterface'); | ||
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock); | ||
|
||
$wrappedSessionHandlerMock | ||
->expects($this->once()) | ||
->method('write') | ||
->with('foo', 'bar') | ||
->will($this->returnValue(true)) | ||
; | ||
|
||
$this->assertEquals(true, $writeCheckSessionHandler->write('foo', 'bar')); | ||
} | ||
|
||
public function testSkippedWrite() | ||
{ | ||
$wrappedSessionHandlerMock = $this->getMock('SessionHandlerInterface'); | ||
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock); | ||
|
||
$wrappedSessionHandlerMock | ||
->expects($this->once()) | ||
->method('read') | ||
->with('foo') | ||
->will($this->returnValue('bar')) | ||
; | ||
|
||
$wrappedSessionHandlerMock | ||
->expects($this->never()) | ||
->method('write') | ||
; | ||
|
||
$this->assertEquals('bar', $writeCheckSessionHandler->read('foo')); | ||
$this->assertEquals(true, $writeCheckSessionHandler->write('foo', 'bar')); | ||
} | ||
|
||
public function testNonSkippedWrite() | ||
{ | ||
$wrappedSessionHandlerMock = $this->getMock('SessionHandlerInterface'); | ||
$writeCheckSessionHandler = new WriteCheckSessionHandler($wrappedSessionHandlerMock); | ||
|
||
$wrappedSessionHandlerMock | ||
->expects($this->once()) | ||
->method('read') | ||
->with('foo') | ||
->will($this->returnValue('bar')) | ||
; | ||
|
||
$wrappedSessionHandlerMock | ||
->expects($this->once()) | ||
->method('write') | ||
->with('foo', 'baZZZ') | ||
->will($this->returnValue(true)) | ||
; | ||
|
||
$this->assertEquals('bar', $writeCheckSessionHandler->read('foo')); | ||
$this->assertEquals(true, $writeCheckSessionHandler->write('foo', 'baZZZ')); | ||
} | ||
} |
This file contains 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