From f33b77c23e7d410489fced4670dc37715c8f9986 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 24 May 2012 03:23:54 +0545 Subject: [PATCH 1/4] [HttpFoundation] Added a custom file save handler --- .../Storage/Handler/FileSessionHandler.php | 102 +++++++++++++++++ .../Handler/FilelSessionHandlerTest.php | 104 ++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php create mode 100644 src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php new file mode 100644 index 0000000000..a5e915d4e6 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php @@ -0,0 +1,102 @@ + + * + * 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; + +/** + * FileSessionHandler. + * + * @author Drak + */ +class FileSessionHandler implements \SessionHandlerInterface +{ + /** + * @var string + */ + private $savePath; + + /** + * Constructor. + * + * @param string $savePath Path of directory to save session files. + */ + public function __construct($savePath) + { + $this->savePath = $savePath; + if (false === is_dir($this->savePath)) { + mkdir($this->savePath, 0777, true); + } + } + + /** + * {@inheritdoc] + */ + function open($savePath, $sessionName) + { + return true; + } + + /** + * {@inheritdoc] + */ + function close() + { + return true; + } + + /** + * {@inheritdoc] + */ + function read($id) + { + $file = $this->savePath.'/sess_'.$id; + if (file_exists($file)) { + return file_get_contents($file); + } + + return ''; + } + + /** + * {@inheritdoc] + */ + function write($id, $data) + { + return false === file_put_contents($this->savePath.'/sess_'.$id, $data) ? false : true; + } + + /** + * {@inheritdoc] + */ + function destroy($id) + { + $file = $this->savePath.'/sess_'.$id; + if (file_exists($file)) { + unlink($file); + } + + return true; + } + + /** + * {@inheritdoc] + */ + function gc($maxlifetime) + { + foreach (glob($this->savePath.'/sess_*') as $file) { + if ((filemtime($file) + $maxlifetime) < time()) { + unlink($file); + } + } + + return true; + } +} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php new file mode 100644 index 0000000000..b363b09e60 --- /dev/null +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php @@ -0,0 +1,104 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; + +use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler; + +/** + * Test class for FileSessionHandler. + * + * @author Drak + */ +class FileSessionStorageTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var FileSessionHandler + */ + private $handler; + + private $path; + + public function setUp() + { + $this->path = sys_get_temp_dir().'/filesessionhandler/'; + $this->handler = new FileSessionHandler($this->path); + + parent::setUp(); + } + + public function tearDown() + { + foreach (glob($this->path.'*') as $file) { + unlink($file); + } + + rmdir($this->path); + + $this->handler = null; + } + + public function test__construct() + { + $this->assertTrue(is_dir($this->path)); + } + + public function testOpen() + { + $this->assertTrue($this->handler->open('a', 'b')); + } + + public function testClose() + { + $this->assertTrue($this->handler->close()); + } + + public function testReadWrite() + { + $this->assertEmpty($this->handler->read('123')); + $this->assertTrue($this->handler->write('123', 'data')); + $this->assertEquals('data', $this->handler->read('123')); + } + + public function testDestroy() + { + $this->handler->write('456', 'data'); + $this->handler->destroy('123'); + $this->assertEquals('data', $this->handler->read('456')); + $this->handler->destroy('456'); + $this->assertEmpty($this->handler->read('456')); + } + + public function testGc() + { + $prefix = $this->path.'sess_'; + $this->handler->write('1', 'data'); + touch($prefix.'1', time()-86400); + + $this->handler->write('2', 'data'); + touch($prefix.'2', time()-3600); + + $this->handler->write('3', 'data'); + touch($prefix.'3', time()-300); + + $this->handler->write('4', 'data'); + + $this->handler->gc(90000); + $this->assertEquals(4, count(glob($this->path.'*'))); + + $this->handler->gc(4000); + $this->assertEquals(3, count(glob($this->path.'*'))); + + $this->handler->gc(200); + $this->assertEquals(1, count(glob($this->path.'*'))); + } +} + From b2cc580be7614e80d9009126937c7c24aa0741c6 Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 26 May 2012 17:21:39 +0545 Subject: [PATCH 2/4] [HttpFoundation] Removed Native*Handler session save handler classes --- .../Handler/NativeFileSessionHandler.php | 41 ----------- .../Handler/NativeMemcacheSessionHandler.php | 68 ------------------- .../Handler/NativeMemcachedSessionHandler.php | 67 ------------------ .../Handler/NativeRedisSessionHandler.php | 43 ------------ .../Storage/Handler/NativeSessionHandler.php | 24 ------- .../Handler/NativeSqliteSessionHandler.php | 58 ---------------- .../NativeMemcacheSessionHandlerTest.php | 45 ------------ .../NativeMemcachedSessionHandlerTest.php | 49 ------------- .../Handler/NativeRedisSessionHandlerTest.php | 43 ------------ .../NativeSqliteSessionHandlerTest.php | 47 ------------- 10 files changed, 485 deletions(-) delete mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeFileSessionHandler.php delete mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcacheSessionHandler.php delete mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcachedSessionHandler.php delete mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandler.php delete mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php delete mode 100644 src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSqliteSessionHandler.php delete mode 100644 src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcacheSessionHandlerTest.php delete mode 100644 src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcachedSessionHandlerTest.php delete mode 100644 src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeRedisSessionHandlerTest.php delete mode 100644 src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSqliteSessionHandlerTest.php diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeFileSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeFileSessionHandler.php deleted file mode 100644 index 422e3a79a6..0000000000 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeFileSessionHandler.php +++ /dev/null @@ -1,41 +0,0 @@ - - * - * 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; - -/** - * NativeFileSessionHandler. - * - * Native session handler using PHP's built in file storage. - * - * @author Drak - */ -class NativeFileSessionHandler extends NativeSessionHandler -{ - /** - * Constructor. - * - * @param string $savePath Path of directory to save session files. Default null will leave setting as defined by PHP. - */ - public function __construct($savePath = null) - { - if (null === $savePath) { - $savePath = ini_get('session.save_path'); - } - - if ($savePath && !is_dir($savePath)) { - mkdir($savePath, 0777, true); - } - - ini_set('session.save_handler', 'files'); - ini_set('session.save_path', $savePath); - } -} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcacheSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcacheSessionHandler.php deleted file mode 100644 index 0b9c623558..0000000000 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcacheSessionHandler.php +++ /dev/null @@ -1,68 +0,0 @@ - - * - * 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; - -/** - * NativeMemcacheSessionHandler. - * - * Driver for the memcache session save handler provided by the memcache PHP extension. - * - * @see http://php.net/memcache - * - * @author Drak - */ -class NativeMemcacheSessionHandler extends NativeSessionHandler -{ - /** - * Constructor. - * - * @param string $savePath Path of memcache server. - * @param array $options Session configuration options. - */ - public function __construct($savePath = 'tcp://127.0.0.1:11211?persistent=0', array $options = array()) - { - if (!extension_loaded('memcache')) { - throw new \RuntimeException('PHP does not have "memcache" session module registered'); - } - - if (null === $savePath) { - $savePath = ini_get('session.save_path'); - } - - ini_set('session.save_handler', 'memcache'); - ini_set('session.save_path', $savePath); - - $this->setOptions($options); - } - - /** - * Set any memcached ini values. - * - * @see http://php.net/memcache.ini - */ - protected function setOptions(array $options) - { - $validOptions = array_flip(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', - )); - - foreach ($options as $key => $value) { - if (isset($validOptions[$key])) { - ini_set($key, $value); - } - } - } -} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcachedSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcachedSessionHandler.php deleted file mode 100644 index ee9d75475b..0000000000 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeMemcachedSessionHandler.php +++ /dev/null @@ -1,67 +0,0 @@ - - * - * 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; - -/** - * NativeMemcachedSessionHandler. - * - * Driver for the memcached session save handler provided by the memcached PHP extension. - * - * @see http://php.net/memcached.sessions - * - * @author Drak - */ -class NativeMemcachedSessionHandler extends NativeSessionHandler -{ - /** - * 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. - */ - public function __construct($savePath = '127.0.0.1:11211', array $options = array()) - { - if (!extension_loaded('memcached')) { - throw new \RuntimeException('PHP does not have "memcached" session module registered'); - } - - if (null === $savePath) { - $savePath = ini_get('session.save_path'); - } - - ini_set('session.save_handler', 'memcached'); - ini_set('session.save_path', $savePath); - - $this->setOptions($options); - } - - /** - * Set any memcached ini values. - * - * @see https://github.com/php-memcached-dev/php-memcached/blob/master/memcached.ini - */ - protected function setOptions(array $options) - { - $validOptions = array_flip(array( - 'memcached.sess_locking', 'memcached.sess_lock_wait', - 'memcached.sess_prefix', 'memcached.compression_type', - 'memcached.compression_factor', 'memcached.compression_threshold', - 'memcached.serializer', - )); - - foreach ($options as $key => $value) { - if (isset($validOptions[$key])) { - ini_set($key, $value); - } - } - } -} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandler.php deleted file mode 100644 index e60c7f6558..0000000000 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeRedisSessionHandler.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * 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 handler 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/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php deleted file mode 100644 index 1260ad0d29..0000000000 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSessionHandler.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * 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; - -/** - * Adds SessionHandler functionality if available. - * - * @see http://php.net/sessionhandler - */ - -if (version_compare(phpversion(), '5.4.0', '>=')) { - class NativeSessionHandler extends \SessionHandler {} -} else { - class NativeSessionHandler {} -} diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSqliteSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSqliteSessionHandler.php deleted file mode 100644 index ac034c2938..0000000000 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NativeSqliteSessionHandler.php +++ /dev/null @@ -1,58 +0,0 @@ - - * - * 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; - -/** - * NativeSqliteSessionHandler. - * - * Driver for the sqlite session save handler provided by the SQLite PHP extension. - * - * @author Drak - */ -class NativeSqliteSessionHandler extends NativeSessionHandler -{ - /** - * Constructor. - * - * @param string $savePath Path to SQLite database file itself. - * @param array $options Session configuration options. - */ - public function __construct($savePath, array $options = array()) - { - if (!extension_loaded('sqlite')) { - throw new \RuntimeException('PHP does not have "sqlite" session module registered'); - } - - if (null === $savePath) { - $savePath = ini_get('session.save_path'); - } - - ini_set('session.save_handler', 'sqlite'); - ini_set('session.save_path', $savePath); - - $this->setOptions($options); - } - - /** - * Set any sqlite ini values. - * - * @see http://php.net/sqlite.configuration - */ - protected function setOptions(array $options) - { - foreach ($options as $key => $value) { - if (in_array($key, array('sqlite.assoc_case'))) { - ini_set($key, $value); - } - } - } -} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcacheSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcacheSessionHandlerTest.php deleted file mode 100644 index b915e59ca0..0000000000 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcacheSessionHandlerTest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; - -use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeMemcacheSessionHandler; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; - -/** - * Test class for NativeMemcacheSessionHandler. - * - * @author Drak - * - * @runTestsInSeparateProcesses - */ -class NativeMemcacheSessionHandlerTest extends \PHPUnit_Framework_TestCase -{ - public function testSaveHandlers() - { - if (!extension_loaded('memcache')) { - $this->markTestSkipped('Skipped tests memcache extension is not present'); - } - - $storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeMemcacheSessionHandler('tcp://127.0.0.1:11211?persistent=0')); - - if (version_compare(phpversion(), '5.4.0', '<')) { - $this->assertEquals('memcache', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('memcache', ini_get('session.save_handler')); - } else { - $this->assertEquals('memcache', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('user', 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')); - } -} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcachedSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcachedSessionHandlerTest.php deleted file mode 100644 index 5f65e9a0fe..0000000000 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeMemcachedSessionHandlerTest.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; - -use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeMemcachedSessionHandler; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; - -/** - * Test class for NativeMemcachedSessionHandler. - * - * @author Drak - * - * @runTestsInSeparateProcesses - */ -class NativeMemcachedSessionHandlerTest extends \PHPUnit_Framework_TestCase -{ - public function testSaveHandlers() - { - if (!extension_loaded('memcached')) { - $this->markTestSkipped('Skipped tests memcached extension is not present'); - } - - // test takes too long if memcached server is not running - ini_set('memcached.sess_locking', '0'); - - $storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeMemcachedSessionHandler('127.0.0.1:11211')); - - if (version_compare(phpversion(), '5.4.0', '<')) { - $this->assertEquals('memcached', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('memcached', ini_get('session.save_handler')); - } else { - $this->assertEquals('memcached', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('user', ini_get('session.save_handler')); - } - - $this->assertEquals('127.0.0.1:11211', ini_get('session.save_path')); - $this->assertEquals('TESTING', ini_get('session.name')); - } -} - diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeRedisSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeRedisSessionHandlerTest.php deleted file mode 100644 index 8a5777659d..0000000000 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeRedisSessionHandlerTest.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\Tests\Session\Storage; - -use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeRedisSessionHandler; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; - -/** - * Test class for NativeRedisSessionHandlerTest. - * - * @runTestsInSeparateProcesses - */ -class NativeRedisSessionHandlerTest extends \PHPUnit_Framework_TestCase -{ - public function testSaveHandlers() - { - if (!extension_loaded('redis')) { - $this->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')); - } -} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSqliteSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSqliteSessionHandlerTest.php deleted file mode 100644 index 983148e279..0000000000 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeSqliteSessionHandlerTest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; - -use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeSqliteSessionHandler; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; - -/** - * Test class for NativeSqliteSessionHandler. - * - * @author Drak - * - * @runTestsInSeparateProcesses - */ -class NativeSqliteSessionHandlerTest extends \PHPUnit_Framework_TestCase -{ - public function testSaveHandlers() - { - if (!extension_loaded('sqlite')) { - $this->markTestSkipped('Skipped tests SQLite extension is not present'); - } - - $storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeSqliteSessionHandler(sys_get_temp_dir().'/sqlite.db')); - - if (version_compare(phpversion(), '5.4.0', '<')) { - $this->assertEquals('sqlite', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('sqlite', ini_get('session.save_handler')); - } else { - $this->assertEquals('sqlite', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('user', 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')); - } -} - From 13a2c82f01a8d9c7885bd4afe268b2bd75a58e55 Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 26 May 2012 17:39:45 +0545 Subject: [PATCH 3/4] [FrameworkBundle] Refactor session file handler service name and update changelogs --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 2 +- .../Bundle/FrameworkBundle/Resources/config/session.xml | 5 +++-- src/Symfony/Component/HttpFoundation/CHANGELOG.md | 5 +---- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 6df118bcc6..a8c5b97794 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -25,7 +25,7 @@ CHANGELOG * [BC BREAK] following session options: 'lifetime', 'path', 'domain', 'secure', 'httponly' are now prefixed with cookie_ when dumped to the container * Added `handler_id` configuration under `session` key to represent `session.handler` - service, defaults to `session.handler.native_file`. + service, defaults to `session.handler.file`. * Added `gc_maxlifetime`, `gc_probability`, and `gc_divisor` to session configuration. This means session garbage collection has a `gc_probability`/`gc_divisor` chance of being run. The `gc_maxlifetime` defines diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml index 871ea3ab9c..27631583d1 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/session.xml @@ -10,7 +10,7 @@ Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage - Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler + Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler Symfony\Bundle\FrameworkBundle\EventListener\SessionListener @@ -34,7 +34,7 @@ %kernel.cache_dir%/sessions - + %session.save_path% @@ -45,5 +45,6 @@ + diff --git a/src/Symfony/Component/HttpFoundation/CHANGELOG.md b/src/Symfony/Component/HttpFoundation/CHANGELOG.md index 0309a51114..d8664e60bc 100644 --- a/src/Symfony/Component/HttpFoundation/CHANGELOG.md +++ b/src/Symfony/Component/HttpFoundation/CHANGELOG.md @@ -21,12 +21,9 @@ CHANGELOG * [BC BREAK] Moved all session related classes and interfaces into own namespace, as `Symfony\Component\HttpFoundation\Session` and renamed classes accordingly. Session handlers are located in the subnamespace `Symfony\Component\HttpFoundation\Session\Handler`. - * SessionHandlers must implement `\SessionHandlerInterface` or extend from the - `Symfony\Component\HttpFoundation\Storage\Handler\NativeSessionHandler` base class. + * SessionHandlers must implement `\SessionHandlerInterface`. * Added internal storage driver proxy mechanism for forward compatibility with PHP 5.4 `\SessionHandler` class. - * Added session handlers for PHP native MongoDb, Memcache, Memcached, Redis and SQLite session - save handlers. * Added session handlers for custom Memcache, Memcached and Null session save handlers. * [BC BREAK] Removed `NativeSessionStorage` and replaced with `NativeFileSessionHandler`. * [BC BREAK] `SessionStorageInterface` methods removed: `write()`, `read()` and From 3c8cc0a1a091d6adeab64e271b1b7cdb234c153b Mon Sep 17 00:00:00 2001 From: Drak Date: Sat, 26 May 2012 17:45:04 +0545 Subject: [PATCH 4/4] [HttpFoundation][Sessions] Refactored tests --- .../Storage/Handler/FileSessionHandler.php | 45 ++++++++++------ .../Storage/Handler/NullSessionHandler.php | 2 +- .../Storage/MockFileSessionStorage.php | 52 ++++++++----------- ...lerTest.php => FileSessionHandlerTest.php} | 18 ++++--- .../Handler/NativeFileSessionHandlerTest.php | 49 ----------------- .../Storage/MockFileSessionStorageTest.php | 12 +++-- .../Storage/NativeSessionStorageTest.php | 3 +- 7 files changed, 70 insertions(+), 111 deletions(-) rename src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/{FilelSessionHandlerTest.php => FileSessionHandlerTest.php} (82%) delete mode 100644 src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeFileSessionHandlerTest.php diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php index a5e915d4e6..1417b5ad8c 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/FileSessionHandler.php @@ -23,23 +23,34 @@ class FileSessionHandler implements \SessionHandlerInterface */ private $savePath; + /** + * @var string + */ + private $prefix; + /** * Constructor. * * @param string $savePath Path of directory to save session files. */ - public function __construct($savePath) + public function __construct($savePath = null, $prefix = 'sess_') { + if (null === $savePath) { + $savePath = sys_get_temp_dir(); + } + $this->savePath = $savePath; if (false === is_dir($this->savePath)) { mkdir($this->savePath, 0777, true); } + + $this->prefix = $prefix; } /** * {@inheritdoc] */ - function open($savePath, $sessionName) + public function open($savePath, $sessionName) { return true; } @@ -47,7 +58,7 @@ class FileSessionHandler implements \SessionHandlerInterface /** * {@inheritdoc] */ - function close() + public function close() { return true; } @@ -55,31 +66,28 @@ class FileSessionHandler implements \SessionHandlerInterface /** * {@inheritdoc] */ - function read($id) + public function read($id) { - $file = $this->savePath.'/sess_'.$id; - if (file_exists($file)) { - return file_get_contents($file); - } + $file = $this->getPath().$id; - return ''; + return is_readable($file) ? file_get_contents($file) : ''; } /** * {@inheritdoc] */ - function write($id, $data) + public function write($id, $data) { - return false === file_put_contents($this->savePath.'/sess_'.$id, $data) ? false : true; + return false === file_put_contents($this->getPath().$id, $data) ? false : true; } /** * {@inheritdoc] */ - function destroy($id) + public function destroy($id) { - $file = $this->savePath.'/sess_'.$id; - if (file_exists($file)) { + $file = $this->getPath().$id; + if (is_file($file)) { unlink($file); } @@ -89,9 +97,9 @@ class FileSessionHandler implements \SessionHandlerInterface /** * {@inheritdoc] */ - function gc($maxlifetime) + public function gc($maxlifetime) { - foreach (glob($this->savePath.'/sess_*') as $file) { + foreach (glob($this->getPath().'*') as $file) { if ((filemtime($file) + $maxlifetime) < time()) { unlink($file); } @@ -99,4 +107,9 @@ class FileSessionHandler implements \SessionHandlerInterface return true; } + + private function getPath() + { + return $this->savePath.'/'.$this->prefix; + } } diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php index dd9f0c79ae..62068aff1b 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/Handler/NullSessionHandler.php @@ -14,7 +14,7 @@ namespace Symfony\Component\HttpFoundation\Session\Storage\Handler; /** * NullSessionHandler. * - * Can be used in unit testing or in a sitation where persisted sessions are not desired. + * Can be used in unit testing or in a situations where persisted sessions are not desired. * * @author Drak * diff --git a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php index 5bf2962ae6..964e24b5de 100644 --- a/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php +++ b/src/Symfony/Component/HttpFoundation/Session/Storage/MockFileSessionStorage.php @@ -11,6 +11,9 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; +use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler; +use Symfony\Component\HttpFoundation\Session\Storage\MetadataBag; + /** * MockFileSessionStorage is used to mock sessions for * functional testing when done in a single PHP process. @@ -25,31 +28,32 @@ namespace Symfony\Component\HttpFoundation\Session\Storage; class MockFileSessionStorage extends MockArraySessionStorage { /** - * @var string + * @var array */ - private $savePath; - private $sessionData; + /** + * @var FileSessionHandler + */ + private $handler; + /** * Constructor. * - * @param string $savePath Path of directory to save session files. - * @param string $name Session name. + * @param string $savePath Path of directory to save session files. + * @param string $name Session name. + * @param FileSessionHandler $handler Save handler + * @param MetadataBag $metaData Metadatabag */ - public function __construct($savePath = null, $name = 'MOCKSESSID') + public function __construct($savePath = null, $name = 'MOCKSESSID', FileSessionHandler $handler = null, MetadataBag $metaData = null) { - if (null === $savePath) { - $savePath = sys_get_temp_dir(); + if (null == $handler) { + $handler = new FileSessionHandler($savePath, 'mocksess_'); } - if (!is_dir($savePath)) { - mkdir($savePath, 0777, true); - } + $this->handler = $handler; - $this->savePath = $savePath; - - parent::__construct($name); + parent::__construct($name, $metaData); } /** @@ -93,7 +97,7 @@ class MockFileSessionStorage extends MockArraySessionStorage */ public function save() { - file_put_contents($this->getFilePath(), serialize($this->data)); + $this->handler->write($this->id, serialize($this->data)); } /** @@ -102,19 +106,7 @@ class MockFileSessionStorage extends MockArraySessionStorage */ private function destroy() { - if (is_file($this->getFilePath())) { - unlink($this->getFilePath()); - } - } - - /** - * Calculate path to file. - * - * @return string File path - */ - private function getFilePath() - { - return $this->savePath.'/'.$this->id.'.mocksess'; + $this->handler->destroy($this->id); } /** @@ -122,8 +114,8 @@ class MockFileSessionStorage extends MockArraySessionStorage */ private function read() { - $filePath = $this->getFilePath(); - $this->data = is_readable($filePath) && is_file($filePath) ? unserialize(file_get_contents($filePath)) : array(); + $data = $this->handler->read($this->id); + $this->data = $data ? unserialize($data) : array(); $this->loadSession(); } diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FileSessionHandlerTest.php similarity index 82% rename from src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php rename to src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FileSessionHandlerTest.php index b363b09e60..d935b34bd2 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FilelSessionHandlerTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/FileSessionHandlerTest.php @@ -25,19 +25,22 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase */ private $handler; + /** + * @var string + */ private $path; public function setUp() { - $this->path = sys_get_temp_dir().'/filesessionhandler/'; - $this->handler = new FileSessionHandler($this->path); + $this->path = sys_get_temp_dir().'/filesessionhandler'; + $this->handler = new FileSessionHandler($this->path, 'mocksess_'); parent::setUp(); } public function tearDown() { - foreach (glob($this->path.'*') as $file) { + foreach (glob($this->path.'/*') as $file) { unlink($file); } @@ -79,7 +82,7 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase public function testGc() { - $prefix = $this->path.'sess_'; + $prefix = $this->path.'/mocksess_'; $this->handler->write('1', 'data'); touch($prefix.'1', time()-86400); @@ -92,13 +95,12 @@ class FileSessionStorageTest extends \PHPUnit_Framework_TestCase $this->handler->write('4', 'data'); $this->handler->gc(90000); - $this->assertEquals(4, count(glob($this->path.'*'))); + $this->assertEquals(4, count(glob($this->path.'/*'))); $this->handler->gc(4000); - $this->assertEquals(3, count(glob($this->path.'*'))); + $this->assertEquals(3, count(glob($this->path.'/*'))); $this->handler->gc(200); - $this->assertEquals(1, count(glob($this->path.'*'))); + $this->assertEquals(1, count(glob($this->path.'/*'))); } } - diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeFileSessionHandlerTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeFileSessionHandlerTest.php deleted file mode 100644 index 7bdf3a1e3f..0000000000 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Handler/NativeFileSessionHandlerTest.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\HttpFoundation\Tests\Session\Storage\Handler; - -use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler; -use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; - -/** - * Test class for NativeFileSessionHandler. - * - * @author Drak - * - * @runTestsInSeparateProcesses - */ -class NativeFileSessionHandlerTest extends \PHPUnit_Framework_TestCase -{ - public function testConstruct() - { - $storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeFileSessionHandler(sys_get_temp_dir())); - - if (version_compare(phpversion(), '5.4.0', '<')) { - $this->assertEquals('files', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('files', ini_get('session.save_handler')); - } else { - $this->assertEquals('files', $storage->getSaveHandler()->getSaveHandlerName()); - $this->assertEquals('user', ini_get('session.save_handler')); - } - - $this->assertEquals(sys_get_temp_dir(), ini_get('session.save_path')); - $this->assertEquals('TESTING', ini_get('session.name')); - } - - public function testConstructDefault() - { - $path = ini_get('session.save_path'); - $storage = new NativeSessionStorage(array('name' => 'TESTING'), new NativeFileSessionHandler()); - - $this->assertEquals($path, ini_get('session.save_path')); - } -} diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php index 9578ec98a8..2ff05f367d 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/MockFileSessionStorageTest.php @@ -28,7 +28,7 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase private $sessionDir; /** - * @var FileMockSessionStorage + * @var MockFileSessionStorage */ protected $storage; @@ -40,12 +40,14 @@ class MockFileSessionStorageTest extends \PHPUnit_Framework_TestCase protected function tearDown() { + foreach (glob($this->sessionDir.'/mocksess_*') as $file) { + unlink($file); + } + + rmdir($this->sessionDir); + $this->sessionDir = null; $this->storage = null; - array_map('unlink', glob($this->sessionDir.'/*.session')); - if (is_dir($this->sessionDir)) { - rmdir($this->sessionDir); - } } public function testStart() diff --git a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php index 4b88a237a0..528a1d7b02 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/Session/Storage/NativeSessionStorageTest.php @@ -12,7 +12,6 @@ namespace Symfony\Component\HttpFoundation\Tests\Session\Storage; use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; -use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler; use Symfony\Component\HttpFoundation\Session\Storage\Handler\NullSessionHandler; use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag; @@ -137,8 +136,8 @@ class NativeSessionStorageTest extends \PHPUnit_Framework_TestCase $this->markTestSkipped('Test skipped, for PHP 5.3 only.'); } + ini_set('session.save_handler', 'files'); $storage = $this->getStorage(); - $storage->setSaveHandler(new NativeFileSessionHandler()); $this->assertInstanceOf('Symfony\Component\HttpFoundation\Session\Storage\Proxy\NativeProxy', $storage->getSaveHandler()); }