PSR规范调整

This commit is contained in:
thinkphp
2015-10-04 13:05:15 +08:00
parent 1cfb3704c6
commit 27e724bb3c
135 changed files with 9426 additions and 11556 deletions

View File

@@ -11,15 +11,16 @@
namespace think;
class Cookie {
class Cookie
{
static protected $config = [
'prefix' => '', // cookie 名称前缀
'expire' => 0, // cookie 保存时间
'path' => '/', // cookie 保存路径
'domain' => '', // cookie 有效域名
'secure' => false, // cookie 启用安全传输
'httponly' => '', // httponly设置
protected static $config = [
'prefix' => '', // cookie 名称前缀
'expire' => 0, // cookie 保存时间
'path' => '/', // cookie 保存路径
'domain' => '', // cookie 有效域名
'secure' => false, // cookie 启用安全传输
'httponly' => '', // httponly设置
];
/**
@@ -27,9 +28,10 @@ class Cookie {
* @param array $config
* @return void
*/
static public function init($config=[]){
self::$config = array_merge(self::$config, array_change_key_case($config));
if(!empty(self::$config['httponly'])){
public static function init($config = [])
{
self::$config = array_merge(self::$config, array_change_key_case($config));
if (!empty(self::$config['httponly'])) {
ini_set('session.cookie_httponly', 1);
}
}
@@ -39,11 +41,12 @@ class Cookie {
* @param string $prefix
* @return string|void
*/
static public function prefix($prefix=''){
if(empty($prefix)) {
public static function prefix($prefix = '')
{
if (empty($prefix)) {
return self::$config['prefix'];
}else{
self::$config['prefix'] = $prefix;
} else {
self::$config['prefix'] = $prefix;
}
}
@@ -54,24 +57,27 @@ class Cookie {
* @param mixed $options cookie参数
* @return mixed
*/
static public function set($name, $value='', $option=null) {
public static function set($name, $value = '', $option = null)
{
// 参数设置(会覆盖黙认设置)
if (!is_null($option)) {
if (is_numeric($option))
if (is_numeric($option)) {
$option = ['expire' => $option];
elseif (is_string($option))
} elseif (is_string($option)) {
parse_str($option, $option);
$config = array_merge(self::$config, array_change_key_case($option));
}else{
$config = self::$config;
}
$config = array_merge(self::$config, array_change_key_case($option));
} else {
$config = self::$config;
}
$name = $config['prefix'] . $name;
// 设置cookie
if(is_array($value)){
$value = 'think:'.json_encode(array_map('urlencode',$value));
if (is_array($value)) {
$value = 'think:' . json_encode(array_map('urlencode', $value));
}
$expire = !empty($config['expire']) ? time() + intval($config['expire']) : 0;
setcookie($name, $value, $expire, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
setcookie($name, $value, $expire, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
$_COOKIE[$name] = $value;
}
@@ -81,18 +87,19 @@ class Cookie {
* @param string $prefix cookie前缀
* @return mixed
*/
static public function get($name, $prefix='') {
$prefix = $prefix ? $prefix : self::$config['prefix'];
$name = $prefix . $name;
if(isset($_COOKIE[$name])){
$value = $_COOKIE[$name];
if(0 === strpos($value,'think:')){
$value = substr($value,6);
return array_map('urldecode',json_decode($value,true));
}else{
public static function get($name, $prefix = '')
{
$prefix = $prefix ? $prefix : self::$config['prefix'];
$name = $prefix . $name;
if (isset($_COOKIE[$name])) {
$value = $_COOKIE[$name];
if (0 === strpos($value, 'think:')) {
$value = substr($value, 6);
return array_map('urldecode', json_decode($value, true));
} else {
return $value;
}
}else{
} else {
return null;
}
}
@@ -103,11 +110,12 @@ class Cookie {
* @param string $prefix cookie前缀
* @return mixed
*/
static public function delete($name, $prefix='') {
$config = self::$config;
$prefix = $prefix ? $prefix : $config['prefix'];
$name = $prefix . $name;
setcookie($name, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
public static function delete($name, $prefix = '')
{
$config = self::$config;
$prefix = $prefix ? $prefix : $config['prefix'];
$name = $prefix . $name;
setcookie($name, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
unset($_COOKIE[$name]); // 删除指定cookie
}
@@ -116,21 +124,25 @@ class Cookie {
* @param string $prefix cookie前缀
* @return mixed
*/
static public function clear($prefix='') {
public static function clear($prefix = '')
{
// 清除指定前缀的所有cookie
if (empty($_COOKIE))
if (empty($_COOKIE)) {
return;
}
// 要删除的cookie前缀不指定则删除config设置的指定前缀
$config = self::$config;
$prefix = $prefix ? $prefix: $config['prefix'];
if ($prefix) {// 如果前缀为空字符串将不作处理直接返回
$config = self::$config;
$prefix = $prefix ? $prefix : $config['prefix'];
if ($prefix) {
// 如果前缀为空字符串将不作处理直接返回
foreach ($_COOKIE as $key => $val) {
if (0 === strpos($key, $prefix)) {
setcookie($key, '', time() - 3600, $config['path'], $config['domain'],$config['secure'],$config['httponly']);
setcookie($key, '', time() - 3600, $config['path'], $config['domain'], $config['secure'], $config['httponly']);
unset($_COOKIE[$key]);
}
}
}else{
} else {
unset($_COOKIE);
}
return;