[HttpFoundation] Remove FileSessionHandler
This driver is inferior to native handling
This commit is contained in:
parent
345678786e
commit
653821a11b
@ -1,116 +0,0 @@
|
|||||||
<?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\Session\Storage\Handler;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* FileSessionHandler.
|
|
||||||
*
|
|
||||||
* @author Drak <drak@zikula.org>
|
|
||||||
*/
|
|
||||||
class FileSessionHandler implements \SessionHandlerInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $savePath;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $prefix;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constructor.
|
|
||||||
*
|
|
||||||
* @param string $savePath Path of directory to save session files.
|
|
||||||
* @param string $prefix
|
|
||||||
*/
|
|
||||||
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}
|
|
||||||
*/
|
|
||||||
public function open($savePath, $sessionName)
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function close()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function read($id)
|
|
||||||
{
|
|
||||||
$file = $this->getPath().$id;
|
|
||||||
|
|
||||||
return is_readable($file) ? file_get_contents($file) : '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function write($id, $data)
|
|
||||||
{
|
|
||||||
return false === file_put_contents($this->getPath().$id, $data) ? false : true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function destroy($id)
|
|
||||||
{
|
|
||||||
$file = $this->getPath().$id;
|
|
||||||
if (is_file($file)) {
|
|
||||||
unlink($file);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function gc($maxlifetime)
|
|
||||||
{
|
|
||||||
foreach (glob($this->getPath().'*') as $file) {
|
|
||||||
if ((filemtime($file) + $maxlifetime) < time()) {
|
|
||||||
unlink($file);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getPath()
|
|
||||||
{
|
|
||||||
return $this->savePath.'/'.$this->prefix;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,106 +0,0 @@
|
|||||||
<?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\Tests\Session\Storage\Handler;
|
|
||||||
|
|
||||||
use Symfony\Component\HttpFoundation\Session\Storage\Handler\FileSessionHandler;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test class for FileSessionHandler.
|
|
||||||
*
|
|
||||||
* @author Drak <drak@zikula.org>
|
|
||||||
*/
|
|
||||||
class FileSessionStorageTest extends \PHPUnit_Framework_TestCase
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @var FileSessionHandler
|
|
||||||
*/
|
|
||||||
private $handler;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $path;
|
|
||||||
|
|
||||||
public function setUp()
|
|
||||||
{
|
|
||||||
$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) {
|
|
||||||
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.'/mocksess_';
|
|
||||||
$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.'/*')));
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user