diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php new file mode 100644 index 0000000000..2467fd6187 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php @@ -0,0 +1,82 @@ + + * + * 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 + */ +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); + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php new file mode 100644 index 0000000000..71af999cef --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php @@ -0,0 +1,81 @@ + + * + * 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 + */ +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); + } +} diff --git a/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php new file mode 100644 index 0000000000..244c1624b4 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.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\Component\HttpFoundation\SessionStorage; + +use Symfony\Component\HttpFoundation\AttributeBagInterface; +use Symfony\Component\HttpFoundation\FlashBagInterface; + +/** + * NativeSqliteSessionStorage. + * + * Session based on native PHP sqlite database handler. + * + * @author Drak + */ +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); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php new file mode 100644 index 0000000000..0bb4dd6cfc --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php @@ -0,0 +1,37 @@ + + * + * @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()); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php new file mode 100644 index 0000000000..8f498f2e44 --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php @@ -0,0 +1,45 @@ + + * + * @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()); + } +} diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php new file mode 100644 index 0000000000..1559a117ab --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php @@ -0,0 +1,54 @@ + + * + * @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()); + } +} + diff --git a/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php new file mode 100644 index 0000000000..51d1b64ebe --- /dev/null +++ b/tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorageTest.php @@ -0,0 +1,46 @@ + + * + * @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()); + } +} +