-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpFoundation] Add a way to avoid the session be written at each request #9119
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
Merged
Merged
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
[HttpFoundation] Add a way to avoid the session be written at each re…
…quest
- Loading branch information
commit 38f02eacbf0fb33e9ca3a186a2c280fb6920ca10
There are no files selected for viewing
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
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
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
91 changes: 91 additions & 0 deletions
91
src/Symfony/Component/HttpFoundation/Session/Storage/Handler/WriteCheckSessionHandler.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,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 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
94 changes: 94 additions & 0 deletions
94
...y/Component/HttpFoundation/Tests/Session/Storage/Handler/WriteCheckSessionHandlerTest.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,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 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.
What happens on session regenerate?
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 guess that
write
wont be able to know if the session has been modified or not and will save it