Skip to content
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

[HttpFoundation] Added a migrating session handler #26096

Merged
merged 1 commit into from
Apr 6, 2018
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?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;

/**
* 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 <ross.motley@amara.com>
* @author Oliver Radwell <oliver.radwell@amara.com>
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?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 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);
}
}