diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 66961f16be919..e4e8f9603ebfe 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -15,6 +15,7 @@ CHANGELOG * added `CannotWriteFileException`, `ExtensionFileException`, `FormSizeFileException`, `IniSizeFileException`, `NoFileException`, `NoTmpDirFileException`, `PartialFileException` to handle failed `UploadedFile`. + * added `MigratingSessionHandler` for migrating between two session handlers without losing sessions 4.0.0 ----- diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php new file mode 100644 index 0000000000000..abe1f361898cb --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/MigratingSessionHandler.php @@ -0,0 +1,97 @@ + + * + * 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; + +/** + * Migrating session handler for migrating from one handler to another. It reads + * from the current handler and writes both the current and new ones. + * + * It ignores errors from the new handler. + * + * @author Ross Motley + * @author Oliver Radwell + */ +class MigratingSessionHandler implements \SessionHandlerInterface +{ + private $currentHandler; + private $writeOnlyHandler; + + public function __construct(\SessionHandlerInterface $currentHandler, \SessionHandlerInterface $writeOnlyHandler) + { + $this->currentHandler = $currentHandler; + $this->writeOnlyHandler = $writeOnlyHandler; + } + + /** + * {@inheritdoc} + */ + public function close() + { + $result = $this->currentHandler->close(); + $this->writeOnlyHandler->close(); + + return $result; + } + + /** + * {@inheritdoc} + */ + public function destroy($sessionId) + { + $result = $this->currentHandler->destroy($sessionId); + $this->writeOnlyHandler->destroy($sessionId); + + return $result; + } + + /** + * {@inheritdoc} + */ + public function gc($maxlifetime) + { + $result = $this->currentHandler->gc($maxlifetime); + $this->writeOnlyHandler->gc($maxlifetime); + + return $result; + } + + /** + * {@inheritdoc} + */ + public function open($savePath, $sessionId) + { + $result = $this->currentHandler->open($savePath, $sessionId); + $this->writeOnlyHandler->open($savePath, $sessionId); + + return $result; + } + + /** + * {@inheritdoc} + */ + public function read($sessionId) + { + // No reading from new handler until switch-over + return $this->currentHandler->read($sessionId); + } + + /** + * {@inheritdoc} + */ + public function write($sessionId, $sessionData) + { + $result = $this->currentHandler->write($sessionId, $sessionData); + $this->writeOnlyHandler->write($sessionId, $sessionData); + + return $result; + } +} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php new file mode 100644 index 0000000000000..05fd7b7ab9fd4 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/MigratingSessionHandlerTest.php @@ -0,0 +1,141 @@ + + * + * 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 Symfony\Component\HttpFoundation\Session\Storage\Handler\MigratingSessionHandler; + +class MigratingSessionHandlerTest extends TestCase +{ + private $dualHandler; + private $currentHandler; + private $writeOnlyHandler; + + protected function setUp() + { + $this->currentHandler = $this->createMock(\SessionHandlerInterface::class); + $this->writeOnlyHandler = $this->createMock(\SessionHandlerInterface::class); + + $this->dualHandler = new MigratingSessionHandler($this->currentHandler, $this->writeOnlyHandler); + } + + public function testCloses() + { + $this->currentHandler->expects($this->once()) + ->method('close') + ->will($this->returnValue(true)); + + $this->writeOnlyHandler->expects($this->once()) + ->method('close') + ->will($this->returnValue(false)); + + $result = $this->dualHandler->close(); + + $this->assertTrue($result); + } + + public function testDestroys() + { + $sessionId = 'xyz'; + + $this->currentHandler->expects($this->once()) + ->method('destroy') + ->with($sessionId) + ->will($this->returnValue(true)); + + $this->writeOnlyHandler->expects($this->once()) + ->method('destroy') + ->with($sessionId) + ->will($this->returnValue(false)); + + $result = $this->dualHandler->destroy($sessionId); + + $this->assertTrue($result); + } + + public function testGc() + { + $maxlifetime = 357; + + $this->currentHandler->expects($this->once()) + ->method('gc') + ->with($maxlifetime) + ->will($this->returnValue(true)); + + $this->writeOnlyHandler->expects($this->once()) + ->method('gc') + ->with($maxlifetime) + ->will($this->returnValue(false)); + + $result = $this->dualHandler->gc($maxlifetime); + $this->assertTrue($result); + } + + public function testOpens() + { + $savePath = '/path/to/save/location'; + $sessionId = 'xyz'; + + $this->currentHandler->expects($this->once()) + ->method('open') + ->with($savePath, $sessionId) + ->will($this->returnValue(true)); + + $this->writeOnlyHandler->expects($this->once()) + ->method('open') + ->with($savePath, $sessionId) + ->will($this->returnValue(false)); + + $result = $this->dualHandler->open($savePath, $sessionId); + + $this->assertTrue($result); + } + + public function testReads() + { + $sessionId = 'xyz'; + $readValue = 'something'; + + $this->currentHandler->expects($this->once()) + ->method('read') + ->with($sessionId) + ->will($this->returnValue($readValue)); + + $this->writeOnlyHandler->expects($this->never()) + ->method('read') + ->with($this->any()); + + $result = $this->dualHandler->read($sessionId); + + $this->assertEquals($readValue, $result); + } + + public function testWrites() + { + $sessionId = 'xyz'; + $data = 'my-serialized-data'; + + $this->currentHandler->expects($this->once()) + ->method('write') + ->with($sessionId, $data) + ->will($this->returnValue(true)); + + $this->writeOnlyHandler->expects($this->once()) + ->method('write') + ->with($sessionId, $data) + ->will($this->returnValue(false)); + + $result = $this->dualHandler->write($sessionId, $data); + + $this->assertTrue($result); + } +} 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