From f33b77c23e7d410489fced4670dc37715c8f9986 Mon Sep 17 00:00:00 2001 From: Drak Date: Thu, 24 May 2012 03:23:54 +0545 Subject: [PATCH] [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.'*'))); + } +} +