From 665f59348b396cd5311a5e6a15c818e77d5c5b10 Mon Sep 17 00:00:00 2001 From: Andrej Hudec Date: Sun, 4 Mar 2012 17:11:05 +0100 Subject: [PATCH] NativeRedisSessionStorage added - fix and simple unit test added --- .../Storage/NativeRedisSessionStorage.php | 56 +++++++++++++++++++ .../Storage/NativeRedisSessionStorageTest.php | 25 +++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorage.php create mode 100644 tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorageTest.php diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorage.php new file mode 100644 index 0000000000..3b91a077f5 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorage.php @@ -0,0 +1,56 @@ + + * + * 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; + +/** + * NativeRedisSessionStorage. + * + * Driver for the redis session save hadlers provided by the redis PHP extension. + * + * @see https://github.com/nicolasff/phpredis + * + * @author Andrej Hudec + */ +class NativeRedisSessionStorage extends AbstractSessionStorage +{ + /** + * @var string + */ + private $savePath; + + /** + * Constructor. + * + * @param string $savePath Path of redis server. + * @param array $options Session configuration options. + * + * @see AbstractSessionStorage::__construct() + */ + public function __construct($savePath = 'tcp://127.0.0.1:6379?persistent=0', array $options = array()) + { + if (!extension_loaded('redis')) { + throw new \RuntimeException('PHP does not have "redis" session module registered'); + } + + $this->savePath = $savePath; + parent::__construct($options); + } + + /** + * {@inheritdoc} + */ + protected function registerSaveHandlers() + { + ini_set('session.save_handler', 'redis'); + ini_set('session.save_path', $this->savePath); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorageTest.php new file mode 100644 index 0000000000..2a8a37503b --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/NativeRedisSessionStorageTest.php @@ -0,0 +1,25 @@ +markTestSkipped('Skipped tests - Redis extension is not present'); + } + + $storage = new NativeRedisSessionStorage('tcp://127.0.0.1:6379?persistent=0', array('name' => 'TESTING')); + $this->assertEquals('redis', ini_get('session.save_handler')); + $this->assertEquals('tcp://127.0.0.1:6379?persistent=0', ini_get('session.save_path')); + $this->assertEquals('TESTING', ini_get('session.name')); + } +} \ No newline at end of file