Skip to content

Commit

Permalink
[HttpFoundation] fixed FilesystemSessionStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
kriswallsmith committed Apr 20, 2011
1 parent fd1636b commit 30511d2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
Expand Up @@ -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;
}

Expand All @@ -57,6 +59,16 @@ public function start()
$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)
Expand Down Expand Up @@ -85,6 +97,7 @@ public function regenerate($destroy = false)
if ($destroy) {
$this->data = array();
}

return true;
}
}
@@ -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();
}
}

0 comments on commit 30511d2

Please sign in to comment.