Jelajahi Sumber

[HttpFoundation] fixed FilesystemSessionStorage

Kris Wallsmith 14 tahun lalu
induk
melakukan
30511d2965

+ 14 - 1
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 @@ 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;
     }
 }

+ 59 - 0
tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/FilesystemSessionStorageTest.php

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