Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[HttpFoundation] Added drivers for PHP native session save handlers, …
…files, sqlite, memcache and memcached.
- Loading branch information
Drak
committed
Feb 11, 2012
1 parent
57ef984
commit 85b5c43
Showing
7 changed files
with
404 additions
and
0 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
src/Symfony/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/Symfony/Component/HttpFoundation/SessionStorage/NativeSqliteSessionStorage.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
tests/Symfony/Tests/Component/HttpFoundation/SessionStorage/NativeFileSessionStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
...ymfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcacheSessionStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...mfony/Tests/Component/HttpFoundation/SessionStorage/NativeMemcachedSessionStorageTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} | ||
|
Oops, something went wrong.