Merge remote branch 'kriswallsmith/http/filesystem-session-fix'

* kriswallsmith/http/filesystem-session-fix:
  [HttpFoundation] fixed FilesystemSessionStorage
This commit is contained in:
Fabien Potencier 2011-04-20 23:09:04 +02:00
commit d721cbe1e2
2 changed files with 73 additions and 1 deletions

View File

@ -20,17 +20,19 @@ class FilesystemSessionStorage extends NativeSessionStorage
{
private $path;
private $data;
private $started;
public function __construct($path, array $options = array())
{
$this->path = $path;
$this->started = false;
parent::__construct($options);
}
public function start()
{
if (self::$sessionStarted) {
if ($this->started) {
return;
}
@ -57,6 +59,16 @@ class FilesystemSessionStorage extends NativeSessionStorage
$file = $this->path.'/'.session_id().'.session';
$this->data = file_exists($file) ? unserialize(file_get_contents($file)) : array();
$this->started = true;
}
public function getId()
{
if (!$this->started) {
throw new \RuntimeException('The session must be started before reading its ID');
}
return session_id();
}
public function read($key, $default = null)
@ -85,6 +97,7 @@ class FilesystemSessionStorage extends NativeSessionStorage
if ($destroy) {
$this->data = array();
}
return true;
}
}

View File

@ -0,0 +1,59 @@
<?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\Tests\Component\HttpFoundation\SessionStorage;
use Symfony\Component\HttpFoundation\SessionStorage\FilesystemSessionStorage;
class FilesystemSessionStorageTest extends \PHPUnit_Framework_TestCase
{
private $path;
protected function setUp()
{
$this->path = sys_get_temp_dir().'/sf2/session_test';
if (!file_exists($this->path)) {
mkdir($this->path, 0777, true);
}
}
protected function tearDown()
{
array_map('unlink', glob($this->path.'/*.session'));
rmdir($this->path);
}
public function testMultipleInstances()
{
$storage = new FilesystemSessionStorage($this->path);
$storage->start();
$storage->write('foo', 'bar');
$storage = new FilesystemSessionStorage($this->path);
$storage->start();
$this->assertEquals('bar', $storage->read('foo'), 'values persist between instances');
}
public function testGetIdThrowsErrorBeforeStart()
{
$this->setExpectedException('RuntimeException');
$storage = new FilesystemSessionStorage($this->path);
$storage->getId();
}
public function testGetIdWorksAfterStart()
{
$storage = new FilesystemSessionStorage($this->path);
$storage->start();
$storage->getId();
}
}