[HttpFoundation] Added drivers for PHP native session save handlers, files, sqlite, memcache and memcached.
This commit is contained in:
parent
57ef984e95
commit
85b5c43c7a
@ -0,0 +1,82 @@
|
||||
<?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\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\AttributeBagInterface;
|
||||
use Symfony\Component\HttpFoundation\FlashBagInterface;
|
||||
|
||||
/**
|
||||
* NativeMemcacheSessionStorage.
|
||||
*
|
||||
* Session based on native PHP memcache database handler.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NativeMemcacheSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Path of memcache server.
|
||||
* @param array $options Session configuration options.
|
||||
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
|
||||
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for default FlashBag)
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
|
||||
{
|
||||
if (!session_module_name('memcache')) {
|
||||
throw new \RuntimeException('PHP does not have "memcache" session module registered');
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
parent::__construct($attributes, $flashes, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
ini_set('session.save_handlers', 'memcache');
|
||||
ini_set('session.save_path', $this->savePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Sets any values memcached ini values.
|
||||
*
|
||||
* @see http://www.php.net/manual/en/memcache.ini.php
|
||||
*/
|
||||
protected function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
if (in_array($key, array(
|
||||
'memcache.allow_failover', 'memcache.max_failover_attempts',
|
||||
'memcache.chunk_size', 'memcache.default_port', 'memcache.hash_strategy',
|
||||
'memcache.hash_function', 'memcache.protocol', 'memcache.redundancy',
|
||||
'memcache.session_redundancy', 'memcache.compress_threshold',
|
||||
'memcache.lock_timeout'))) {
|
||||
ini_set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
parent::setOptions($options);
|
||||
}
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
<?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\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\AttributeBagInterface;
|
||||
use Symfony\Component\HttpFoundation\FlashBagInterface;
|
||||
|
||||
/**
|
||||
* NativeMemcachedSessionStorage.
|
||||
*
|
||||
* Session based on native PHP memcached database handler.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NativeMemcachedSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $savePath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $savePath Comma separated list of servers: e.g. memcache1.example.com:11211,memcache2.example.com:11211
|
||||
* @param array $options Session configuration options.
|
||||
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
|
||||
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for defaul FlashBag)
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($savePath = '127.0.0.1:11211', array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
|
||||
{
|
||||
if (!session_module_name('memcached')) {
|
||||
throw new \RuntimeException('PHP does not have "memcached" session module registered');
|
||||
}
|
||||
|
||||
$this->savePath = $savePath;
|
||||
parent::__construct($attributes, $flashes, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
ini_set('session.save_handlers', 'memcached');
|
||||
ini_set('session.save_path', $this->savePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Sets any values memcached ini values.
|
||||
*
|
||||
* @see https://github.com/php-memcached-dev/php-memcached/blob/master/memcached.ini
|
||||
*/
|
||||
protected function setOptions(array $options)
|
||||
{
|
||||
foreach ($options as $key => $value) {
|
||||
if (in_array($key, array(
|
||||
'memcached.sess_locking', 'memcached.sess_lock_wait',
|
||||
'memcached.sess_prefix', 'memcached.compression_type',
|
||||
'memcached.compression_factor', 'memcached.compression_threshold',
|
||||
'memcached.serializer'))) {
|
||||
ini_set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
parent::setOptions($options);
|
||||
}
|
||||
}
|
@ -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\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\AttributeBagInterface;
|
||||
use Symfony\Component\HttpFoundation\FlashBagInterface;
|
||||
|
||||
/**
|
||||
* NativeSqliteSessionStorage.
|
||||
*
|
||||
* Session based on native PHP sqlite database handler.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*/
|
||||
class NativeSqliteSessionStorage extends AbstractSessionStorage
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $dbPath;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $dbPath Path to SQLite database file.
|
||||
* @param array $options Session configuration options.
|
||||
* @param AttributeBagInterface $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
|
||||
* @param FlashBagInterface $flashes A FlashBagInterface instance (defaults null for defaul FlashBag)
|
||||
*
|
||||
* @see AbstractSessionStorage::__construct()
|
||||
*/
|
||||
public function __construct($dbPath, array $options = array(), AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
|
||||
{
|
||||
if (!session_module_name('sqlite')) {
|
||||
throw new \RuntimeException('PHP does not have "sqlite" session module registered');
|
||||
}
|
||||
|
||||
$this->dbPath = $dbPath;
|
||||
parent::__construct($attributes, $flashes, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function registerSaveHandlers()
|
||||
{
|
||||
ini_set('session.save_handlers', 'sqlite');
|
||||
ini_set('session.save_path', $this->dbPath);
|
||||
}
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\NativeFileSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\FlashBag;
|
||||
|
||||
/**
|
||||
* Test class for NativeFileSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeFileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstructDefaults()
|
||||
{
|
||||
$storage = new NativeFileSessionStorage();
|
||||
$this->assertEquals('files', ini_get('session.save_handler'));
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
|
||||
}
|
||||
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
$attributeBag = new AttributeBag();
|
||||
$flashBag = new FlashBag();
|
||||
$storage = new NativeFileSessionStorage(sys_get_temp_dir(), array('name' => 'TESTING'), $attributeBag, $flashBag);
|
||||
$this->assertEquals('files', ini_get('session.save_handler'));
|
||||
$this->assertEquals(sys_get_temp_dir(), ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
$this->assertSame($attributeBag, $storage->getAttributes());
|
||||
$this->assertSame($flashBag, $storage->getFlashes());
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\NativeMemcacheSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\FlashBag;
|
||||
|
||||
/**
|
||||
* Test class for NativeMemcacheSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeMemcacheSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstructDefaults()
|
||||
{
|
||||
if (!extension_loaded('memcache')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
$storage = new NativeMemcacheSessionStorage('tcp://127.0.0.1:11211?persistent=0');
|
||||
$this->assertEquals('memcache', ini_get('session.save_handler'));
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
|
||||
}
|
||||
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
if (!extension_loaded('memcache')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
$attributeBag = new AttributeBag();
|
||||
$flashBag = new FlashBag();
|
||||
$storage = new NativeMemcacheSessionStorage('tcp://127.0.0.1:11211?persistent=0', array('name' => 'TESTING'), $attributeBag, $flashBag);
|
||||
$this->assertEquals('memcache', ini_get('session.save_handler'));
|
||||
$this->assertEquals('tcp://127.0.0.1:11211?persistent=0', ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
$this->assertSame($attributeBag, $storage->getAttributes());
|
||||
$this->assertSame($flashBag, $storage->getFlashes());
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\NativeMemcachedSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\FlashBag;
|
||||
|
||||
/**
|
||||
* Test class for NativeMemcachedSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeMemcachedSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstructDefaults()
|
||||
{
|
||||
if (!extension_loaded('memcached')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
// test takes too long if memcached server is not running
|
||||
ini_set('memcached.sess_locking', '0');
|
||||
|
||||
$storage = new NativeMemcachedSessionStorage('127.0.0.1:11211');
|
||||
$this->assertEquals('memcached', ini_get('session.save_handler'));
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
|
||||
}
|
||||
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
if (!extension_loaded('memcached')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
$attributeBag = new AttributeBag();
|
||||
$flashBag = new FlashBag();
|
||||
|
||||
// test takes too long if memcached server is not running
|
||||
ini_set('memcached.sess_locking', '0');
|
||||
|
||||
$storage = new NativeMemcachedSessionStorage('127.0.0.1:11211', array('name' => 'TESTING'), $attributeBag, $flashBag);
|
||||
|
||||
$this->assertEquals('memcached', ini_get('session.save_handler'));
|
||||
$this->assertEquals('127.0.0.1:11211', ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
$this->assertSame($attributeBag, $storage->getAttributes());
|
||||
$this->assertSame($flashBag, $storage->getFlashes());
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace Symfony\Tests\Component\HttpFoundation\SessionStorage;
|
||||
|
||||
use Symfony\Component\HttpFoundation\SessionStorage\NativeSqliteSessionStorage;
|
||||
use Symfony\Component\HttpFoundation\AttributeBag;
|
||||
use Symfony\Component\HttpFoundation\FlashBag;
|
||||
|
||||
/**
|
||||
* Test class for NativeSqliteSessionStorage.
|
||||
*
|
||||
* @author Drak <drak@zikula.org>
|
||||
*
|
||||
* @runTestsInSeparateProcesses
|
||||
*/
|
||||
class NativeSqliteSessionStorageTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testConstructDefaults()
|
||||
{
|
||||
if (!extension_loaded('sqlite')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
$storage = new NativeSqliteSessionStorage(sys_get_temp_dir().'/sqlite.db');
|
||||
$this->assertEquals('sqlite', ini_get('session.save_handler'));
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\AttributeBagInterface', $storage->getAttributes());
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\FlashBagInterface', $storage->getFlashes());
|
||||
}
|
||||
|
||||
public function testSaveHandlers()
|
||||
{
|
||||
if (!extension_loaded('sqlite')) {
|
||||
$this->markTestSkipped('Skipped tests SQLite extension is not present');
|
||||
}
|
||||
|
||||
$attributeBag = new AttributeBag();
|
||||
$flashBag = new FlashBag();
|
||||
$storage = new NativeSqliteSessionStorage(sys_get_temp_dir().'/sqlite.db', array('name' => 'TESTING'), $attributeBag, $flashBag);
|
||||
$this->assertEquals('sqlite', ini_get('session.save_handler'));
|
||||
$this->assertEquals(sys_get_temp_dir().'/sqlite.db', ini_get('session.save_path'));
|
||||
$this->assertEquals('TESTING', ini_get('session.name'));
|
||||
$this->assertSame($attributeBag, $storage->getAttributes());
|
||||
$this->assertSame($flashBag, $storage->getFlashes());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user