fix session

This commit is contained in:
zhylninc
2016-01-14 22:04:13 +08:00
parent e8001913bc
commit f22fffda28
2 changed files with 252 additions and 394 deletions

View File

@@ -8,21 +8,19 @@
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think;
class Session
{
class Session {
protected static $prefix = '';
/**
* 设置或者获取session作用域前缀
*
* @param string $prefix
* @return string|void
*/
public static function prefix($prefix = '')
{
if (empty($prefix)) {
public static function prefix($prefix = '') {
if (empty ( $prefix ) && $prefix !== null) {
return self::$prefix;
} else {
self::$prefix = $prefix;
@@ -31,11 +29,21 @@ class Session
/**
* session初始化
*
* @param array $config
* @return void
*/
public static function init($config = [])
{
public static function init($config = []) {
if (isset ( $config ['use_trans_sid'] )) {
ini_set ( 'session.use_trans_sid', $config ['use_trans_sid'] ? 1 : 0 );
}
// 启动session
if (! empty ( $config ['auto_start'] ) && PHP_SESSION_ACTIVE != session_status ()) {
ini_set ( 'session.auto_start', 0 );
session_start ();
}
if (isset ( $config ['prefix'] )) {
self::$prefix = $config ['prefix'];
}
@@ -57,9 +65,7 @@ class Session
ini_set ( 'session.gc_maxlifetime', $config ['expire'] );
ini_set ( 'session.cookie_lifetime', $config ['expire'] );
}
if (isset($config['use_trans_sid'])) {
ini_set('session.use_trans_sid', $config['use_trans_sid'] ? 1 : 0);
}
if (isset ( $config ['use_cookies'] )) {
ini_set ( 'session.use_cookies', $config ['use_cookies'] ? 1 : 0 );
}
@@ -72,28 +78,26 @@ class Session
if (! empty ( $config ['type'] )) {
// 读取session驱动
$class = (! empty ( $config ['namespace'] ) ? $config ['namespace'] : '\\think\\session\\driver\\') . ucwords ( $config ['type'] );
// 检查驱动类
if (!session_set_save_handler(new $class())) {
throw new Exception('error session handler', 11700);
}
}
// 启动session
if (!empty($config['auto_start']) && PHP_SESSION_ACTIVE != session_status()) {
ini_set('session.auto_start', 0);
session_start();
}
// 检查驱动类
if (! class_exists ( $class ) || ! session_set_save_handler ( new $class () )) {
throw new \think\Exception ( 'error session handler', 11700 );
}
}
}
/**
* session设置
* @param string $name session名称
* @param mixed $value session值
* @param string $prefix 作用域(前缀)
*
* @param string $name
* session名称
* @param mixed $value
* session值
* @param string $prefix
* 作用域(前缀)
* @return void
*/
public static function set($name, $value = '', $prefix = '')
{
public static function set($name, $value = '', $prefix = '') {
$prefix = $prefix ? $prefix : self::$prefix;
if (strpos ( $name, '.' )) {
// 二维数组赋值
@@ -112,12 +116,14 @@ class Session
/**
* session获取
* @param string $name session名称
* @param string $prefix 作用域(前缀)
*
* @param string $name
* session名称
* @param string $prefix
* 作用域(前缀)
* @return mixed
*/
public static function get($name = '', $prefix = '')
{
public static function get($name = '', $prefix = '') {
$prefix = $prefix ? $prefix : self::$prefix;
if ('' == $name) {
// 获取全部的session
@@ -143,12 +149,14 @@ class Session
/**
* 删除session数据
* @param string $name session名称
* @param string $prefix 作用域(前缀)
*
* @param string $name
* session名称
* @param string $prefix
* 作用域(前缀)
* @return void
*/
public static function delete($name, $prefix = '')
{
public static function delete($name, $prefix = '') {
$prefix = $prefix ? $prefix : self::$prefix;
if (strpos ( $name, '.' )) {
list ( $name1, $name2 ) = explode ( '.', $name );
@@ -168,11 +176,12 @@ class Session
/**
* 清空session数据
* @param string $prefix 作用域(前缀)
*
* @param string $prefix
* 作用域(前缀)
* @return void
*/
public static function clear($prefix = '')
{
public static function clear($prefix = '') {
$prefix = $prefix ? $prefix : self::$prefix;
if ($prefix) {
unset ( $_SESSION [$prefix] );
@@ -184,14 +193,14 @@ class Session
/**
* 判断session数据
*
* @param string $name session名称
* @param string $name
* session名称
* @param string $prefix
*
* @return bool
* @internal param mixed $value session值
*/
public static function has($name, $prefix = '')
{
public static function has($name, $prefix = '') {
$prefix = $prefix ? $prefix : self::$prefix;
if (strpos ( $name, '.' )) {
// 支持数组
@@ -204,29 +213,29 @@ class Session
/**
* 暂停session
*
* @return void
*/
public static function pause()
{
public static function pause() {
// 暂停session
session_write_close ();
}
/**
* 启动session
*
* @return void
*/
public static function start()
{
public static function start() {
session_start ();
}
/**
* 销毁session
*
* @return void
*/
public static function destroy()
{
public static function destroy() {
$_SESSION = [ ];
session_unset ();
session_destroy ();
@@ -234,11 +243,10 @@ class Session
/**
* 重新生成session_id
*
* @return void
*/
private static function regenerate()
{
private static function regenerate() {
session_regenerate_id ();
}
}

View File

@@ -1,150 +0,0 @@
<?php
namespace think;
/**
* Generated by PHPUnit_SkeletonGenerator on 2016-01-14 at 02:40:08.
*/
class SessionTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Session
*/
protected $object;
/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new Session;
}
/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}
/**
* @covers think\Session::prefix
* @todo Implement testPrefix().
*/
public function testPrefix()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::init
* @todo Implement testInit().
*/
public function testInit()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::set
* @todo Implement testSet().
*/
public function testSet()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::get
* @todo Implement testGet().
*/
public function testGet()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::delete
* @todo Implement testDelete().
*/
public function testDelete()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::clear
* @todo Implement testClear().
*/
public function testClear()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::has
* @todo Implement testHas().
*/
public function testHas()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::pause
* @todo Implement testPause().
*/
public function testPause()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::start
* @todo Implement testStart().
*/
public function testStart()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
/**
* @covers think\Session::destroy
* @todo Implement testDestroy().
*/
public function testDestroy()
{
// Remove the following lines when you implement this test.
$this->markTestIncomplete(
'This test has not been implemented yet.'
);
}
}