diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandler.php new file mode 100644 index 0000000000..d155052a97 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandler.php @@ -0,0 +1,43 @@ + + * + * 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\Handler; + +/** + * NativeRedisSessionStorage. + * + * Driver for the redis session save hadlers provided by the redis PHP extension. + * + * @see https://github.com/nicolasff/phpredis + * + * @author Andrej Hudec + */ +class NativeRedisSessionHandler extends NativeSessionHandler +{ + /** + * Constructor. + * + * @param string $savePath Path of redis server. + */ + public function __construct($savePath = 'tcp://127.0.0.1:6379?persistent=0') + { + if (!extension_loaded('redis')) { + throw new \RuntimeException('PHP does not have "redis" session module registered'); + } + + if (null === $savePath) { + $savePath = ini_get('session.save_path'); + } + + ini_set('session.save_handler', 'redis'); + ini_set('session.save_path', $savePath); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandlerTest.php b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandlerTest.php new file mode 100644 index 0000000000..77ac423c51 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandlerTest.php @@ -0,0 +1,34 @@ +markTestSkipped('Skipped tests Redis extension is not present'); + } + + $storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeRedisSessionHandler('tcp://127.0.0.1:6379?persistent=0')); + + if (version_compare(phpversion(), '5.4.0', '<')) { + $this->assertEquals('redis', $storage->getSaveHandler()->getSaveHandlerName()); + $this->assertEquals('redis', ini_get('session.save_handler')); + } else { + $this->assertEquals('redis', $storage->getSaveHandler()->getSaveHandlerName()); + $this->assertEquals('user', 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