初始化
This commit is contained in:
325
src/think/session/Store.php
Normal file
325
src/think/session/Store.php
Normal file
@@ -0,0 +1,325 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2025 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\session;
|
||||
|
||||
use think\contract\SessionHandlerInterface;
|
||||
use think\helper\Arr;
|
||||
|
||||
class Store
|
||||
{
|
||||
/**
|
||||
* Session数据
|
||||
* @var array
|
||||
*/
|
||||
protected $data = [];
|
||||
|
||||
/**
|
||||
* 是否初始化
|
||||
* @var bool
|
||||
*/
|
||||
protected $init = null;
|
||||
|
||||
/**
|
||||
* 记录Session Id
|
||||
* @var string
|
||||
*/
|
||||
protected $id;
|
||||
|
||||
/** @var array */
|
||||
protected $serialize = [];
|
||||
|
||||
public function __construct(protected string $name, protected SessionHandlerInterface $handler, ?array $serialize = null)
|
||||
{
|
||||
if (!empty($serialize)) {
|
||||
$this->serialize = $serialize;
|
||||
}
|
||||
|
||||
$this->setId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置数据
|
||||
* @access public
|
||||
* @param array $data
|
||||
* @return void
|
||||
*/
|
||||
public function setData(array $data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* session初始化
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function init(): void
|
||||
{
|
||||
// 读取缓存数据
|
||||
$data = $this->handler->read($this->getId());
|
||||
|
||||
if (!empty($data)) {
|
||||
$this->data = array_merge($this->data, $this->unserialize($data));
|
||||
}
|
||||
|
||||
$this->init = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置SessionName
|
||||
* @access public
|
||||
* @param string $name session_name
|
||||
* @return void
|
||||
*/
|
||||
public function setName(string $name): void
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取sessionName
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* session_id设置
|
||||
* @access public
|
||||
* @param string $id session_id
|
||||
* @return void
|
||||
*/
|
||||
public function setId(?string $id = null): void
|
||||
{
|
||||
$this->id = is_string($id) && strlen($id) === 32 && ctype_alnum($id) ? $id : md5(microtime(true) . session_create_id());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取session_id
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getId(): string
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有数据
|
||||
* @return array
|
||||
*/
|
||||
public function all(): array
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* session设置
|
||||
* @access public
|
||||
* @param string $name session名称
|
||||
* @param mixed $value session值
|
||||
* @return void
|
||||
*/
|
||||
public function set(string $name, $value): void
|
||||
{
|
||||
Arr::set($this->data, $name, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* session获取
|
||||
* @access public
|
||||
* @param string $name session名称
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function get(string $name, $default = null)
|
||||
{
|
||||
return Arr::get($this->data, $name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* session获取并删除
|
||||
* @access public
|
||||
* @param string $name session名称
|
||||
* @param mixed $default 默认值
|
||||
* @return mixed
|
||||
*/
|
||||
public function pull(string $name, $default = null)
|
||||
{
|
||||
return Arr::pull($this->data, $name, $default);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加数据到一个session数组
|
||||
* @access public
|
||||
* @param string $key
|
||||
* @param mixed $value
|
||||
* @return void
|
||||
*/
|
||||
public function push(string $key, $value): void
|
||||
{
|
||||
$array = $this->get($key, []);
|
||||
|
||||
$array[] = $value;
|
||||
|
||||
$this->set($key, $array);
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断session数据
|
||||
* @access public
|
||||
* @param string $name session名称
|
||||
* @return bool
|
||||
*/
|
||||
public function has(string $name): bool
|
||||
{
|
||||
return Arr::has($this->data, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除session数据
|
||||
* @access public
|
||||
* @param string $name session名称
|
||||
* @return void
|
||||
*/
|
||||
public function delete(string $name): void
|
||||
{
|
||||
Arr::forget($this->data, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空session数据
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function clear(): void
|
||||
{
|
||||
$this->data = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* 销毁session
|
||||
*/
|
||||
public function destroy(): void
|
||||
{
|
||||
$this->clear();
|
||||
|
||||
$this->regenerate(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新生成session id
|
||||
* @param bool $destroy
|
||||
*/
|
||||
public function regenerate(bool $destroy = false): void
|
||||
{
|
||||
if ($destroy) {
|
||||
$this->handler->delete($this->getId());
|
||||
}
|
||||
|
||||
$this->setId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存session数据
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
$this->clearFlashData();
|
||||
|
||||
$sessionId = $this->getId();
|
||||
|
||||
if (!empty($this->data)) {
|
||||
$data = $this->serialize($this->data);
|
||||
|
||||
$this->handler->write($sessionId, $data);
|
||||
} else {
|
||||
$this->handler->delete($sessionId);
|
||||
}
|
||||
|
||||
$this->init = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* session设置 下一次请求有效
|
||||
* @access public
|
||||
* @param string $name session名称
|
||||
* @param mixed $value session值
|
||||
* @return void
|
||||
*/
|
||||
public function flash(string $name, $value): void
|
||||
{
|
||||
$this->set($name, $value);
|
||||
$this->push('__flash__.__next__', $name);
|
||||
$this->set('__flash__.__current__', Arr::except($this->get('__flash__.__current__', []), $name));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将本次闪存数据推迟到下次请求
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function reflash(): void
|
||||
{
|
||||
$keys = $this->get('__flash__.__current__', []);
|
||||
$values = array_unique(array_merge($this->get('__flash__.__next__', []), $keys));
|
||||
$this->set('__flash__.__next__', $values);
|
||||
$this->set('__flash__.__current__', []);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空当前请求的session数据
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function clearFlashData(): void
|
||||
{
|
||||
Arr::forget($this->data, $this->get('__flash__.__current__', []));
|
||||
if (!empty($next = $this->get('__flash__.__next__', []))) {
|
||||
$this->set('__flash__.__current__', $next);
|
||||
} else {
|
||||
$this->delete('__flash__.__current__');
|
||||
}
|
||||
$this->delete('__flash__.__next__');
|
||||
}
|
||||
|
||||
/**
|
||||
* 序列化数据
|
||||
* @access protected
|
||||
* @param mixed $data
|
||||
* @return string
|
||||
*/
|
||||
protected function serialize($data): string
|
||||
{
|
||||
$serialize = $this->serialize[0] ?? 'serialize';
|
||||
|
||||
return $serialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 反序列化数据
|
||||
* @access protected
|
||||
* @param string $data
|
||||
* @return array
|
||||
*/
|
||||
protected function unserialize(string $data): array
|
||||
{
|
||||
$unserialize = $this->serialize[1] ?? 'unserialize';
|
||||
|
||||
return (array) $unserialize($data);
|
||||
}
|
||||
}
|
||||
50
src/think/session/driver/Cache.php
Normal file
50
src/think/session/driver/Cache.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2025 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\session\driver;
|
||||
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
use think\contract\SessionHandlerInterface;
|
||||
use think\helper\Arr;
|
||||
|
||||
class Cache implements SessionHandlerInterface
|
||||
{
|
||||
|
||||
/** @var CacheInterface */
|
||||
protected $handler;
|
||||
|
||||
/** @var integer */
|
||||
protected $expire;
|
||||
|
||||
/** @var string */
|
||||
protected $prefix;
|
||||
|
||||
public function __construct(\think\Cache $cache, array $config = [])
|
||||
{
|
||||
$this->handler = $cache->store(Arr::get($config, 'store'));
|
||||
$this->expire = Arr::get($config, 'expire', 1440);
|
||||
$this->prefix = Arr::get($config, 'prefix', '');
|
||||
}
|
||||
|
||||
public function read(string $sessionId): string
|
||||
{
|
||||
return (string) $this->handler->get($this->prefix . $sessionId);
|
||||
}
|
||||
|
||||
public function delete(string $sessionId): bool
|
||||
{
|
||||
return $this->handler->delete($this->prefix . $sessionId);
|
||||
}
|
||||
|
||||
public function write(string $sessionId, string $data): bool
|
||||
{
|
||||
return $this->handler->set($this->prefix . $sessionId, $data, $this->expire);
|
||||
}
|
||||
}
|
||||
248
src/think/session/driver/File.php
Normal file
248
src/think/session/driver/File.php
Normal file
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006~2025 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace think\session\driver;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use FilesystemIterator;
|
||||
use Generator;
|
||||
use SplFileInfo;
|
||||
use think\App;
|
||||
use think\contract\SessionHandlerInterface;
|
||||
|
||||
/**
|
||||
* Session 文件驱动
|
||||
*/
|
||||
class File implements SessionHandlerInterface
|
||||
{
|
||||
protected $config = [
|
||||
'path' => '',
|
||||
'expire' => 1440,
|
||||
'prefix' => '',
|
||||
'data_compress' => false,
|
||||
'gc_probability' => 1,
|
||||
'gc_divisor' => 100,
|
||||
];
|
||||
|
||||
public function __construct(App $app, array $config = [])
|
||||
{
|
||||
$this->config = array_merge($this->config, $config);
|
||||
|
||||
if (empty($this->config['path'])) {
|
||||
$this->config['path'] = $app->getRuntimePath() . 'session' . DIRECTORY_SEPARATOR;
|
||||
} elseif (substr($this->config['path'], -1) != DIRECTORY_SEPARATOR) {
|
||||
$this->config['path'] .= DIRECTORY_SEPARATOR;
|
||||
}
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开Session
|
||||
* @access protected
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function init(): void
|
||||
{
|
||||
try {
|
||||
!is_dir($this->config['path']) && mkdir($this->config['path'], 0755, true);
|
||||
} catch (Exception $e) {
|
||||
// 写入失败
|
||||
}
|
||||
|
||||
// 垃圾回收
|
||||
if (random_int(1, $this->config['gc_divisor']) <= $this->config['gc_probability']) {
|
||||
$this->gc();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Session 垃圾回收
|
||||
* @access public
|
||||
* @return void
|
||||
*/
|
||||
public function gc(): void
|
||||
{
|
||||
$lifetime = $this->config['expire'];
|
||||
$now = time();
|
||||
|
||||
$files = $this->findFiles($this->config['path'], function (SplFileInfo $item) use ($lifetime, $now) {
|
||||
return $now - $lifetime > $item->getMTime();
|
||||
});
|
||||
|
||||
foreach ($files as $file) {
|
||||
$this->unlink($file->getPathname());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找文件
|
||||
* @param string $root
|
||||
* @param Closure $filter
|
||||
* @return Generator
|
||||
*/
|
||||
protected function findFiles(string $root, Closure $filter)
|
||||
{
|
||||
$items = new FilesystemIterator($root);
|
||||
|
||||
/** @var SplFileInfo $item */
|
||||
foreach ($items as $item) {
|
||||
if ($item->isDir() && !$item->isLink()) {
|
||||
yield from $this->findFiles($item->getPathname(), $filter);
|
||||
} else {
|
||||
if ($filter($item)) {
|
||||
yield $item;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得变量的存储文件名
|
||||
* @access protected
|
||||
* @param string $name 缓存变量名
|
||||
* @param bool $auto 是否自动创建目录
|
||||
* @return string
|
||||
*/
|
||||
protected function getFileName(string $name, bool $auto = false): string
|
||||
{
|
||||
if ($this->config['prefix']) {
|
||||
// 使用子目录
|
||||
$name = $this->config['prefix'] . DIRECTORY_SEPARATOR . 'sess_' . $name;
|
||||
} else {
|
||||
$name = 'sess_' . $name;
|
||||
}
|
||||
|
||||
$filename = $this->config['path'] . $name;
|
||||
$dir = dirname($filename);
|
||||
|
||||
if ($auto && !is_dir($dir)) {
|
||||
try {
|
||||
mkdir($dir, 0755, true);
|
||||
} catch (Exception $e) {
|
||||
// 创建失败
|
||||
}
|
||||
}
|
||||
|
||||
return $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @return string
|
||||
*/
|
||||
public function read(string $sessID): string
|
||||
{
|
||||
$filename = $this->getFileName($sessID);
|
||||
|
||||
if (is_file($filename) && filemtime($filename) >= time() - $this->config['expire']) {
|
||||
$content = $this->readFile($filename);
|
||||
|
||||
if ($this->config['data_compress'] && function_exists('gzcompress')) {
|
||||
//启用数据压缩
|
||||
$content = (string) gzuncompress($content);
|
||||
}
|
||||
|
||||
return $content;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* 写文件(加锁)
|
||||
* @param $path
|
||||
* @param $content
|
||||
* @return bool
|
||||
*/
|
||||
protected function writeFile($path, $content): bool
|
||||
{
|
||||
return (bool) file_put_contents($path, $content, LOCK_EX);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取文件内容(加锁)
|
||||
* @param $path
|
||||
* @return string
|
||||
*/
|
||||
protected function readFile($path): string
|
||||
{
|
||||
$contents = '';
|
||||
|
||||
$handle = fopen($path, 'rb');
|
||||
|
||||
if ($handle) {
|
||||
try {
|
||||
if (flock($handle, LOCK_SH)) {
|
||||
clearstatcache(true, $path);
|
||||
|
||||
$contents = fread($handle, filesize($path) ?: 1);
|
||||
|
||||
flock($handle, LOCK_UN);
|
||||
}
|
||||
} finally {
|
||||
fclose($handle);
|
||||
}
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param string $sessData
|
||||
* @return bool
|
||||
*/
|
||||
public function write(string $sessID, string $sessData): bool
|
||||
{
|
||||
$filename = $this->getFileName($sessID, true);
|
||||
$data = $sessData;
|
||||
|
||||
if ($this->config['data_compress'] && function_exists('gzcompress')) {
|
||||
//数据压缩
|
||||
$data = gzcompress($data, 3);
|
||||
}
|
||||
|
||||
return $this->writeFile($filename, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @return bool
|
||||
*/
|
||||
public function delete(string $sessID): bool
|
||||
{
|
||||
try {
|
||||
return $this->unlink($this->getFileName($sessID));
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断文件是否存在后,删除
|
||||
* @access private
|
||||
* @param string $file
|
||||
* @return bool
|
||||
*/
|
||||
private function unlink(string $file): bool
|
||||
{
|
||||
return is_file($file) && unlink($file);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user