| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2019 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 name
- * @var string
- */
- protected $name = 'PHPSESSID';
- /**
- * 记录Session Id
- * @var string
- */
- protected $id;
- /**
- * @var SessionHandlerInterface
- */
- protected $handler;
- /** @var array */
- protected $serialize = [];
- public function __construct($name, SessionHandlerInterface $handler, array $serialize = null)
- {
- $this->name = $name;
- $this->handler = $handler;
- 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($id = null): void
- {
- $this->id = is_string($id) && strlen($id) === 32 ? $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名称
- * @return mixed
- */
- public function pull(string $name)
- {
- return Arr::pull($this->data, $name);
- }
- /**
- * 添加数据到一个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);
- }
- }
|