diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php index e174be7a9480..5cc70ff45226 100644 --- a/src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/FilesystemSessionStorage.php @@ -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 @@ 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) @@ -85,6 +97,7 @@ public function regenerate($destroy = false) if ($destroy) { $this->data = array(); } + return true; } } diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php new file mode 100644 index 000000000000..7f4503b40557 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php @@ -0,0 +1,59 @@ + + * + * 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(); + } +}