123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\common\library;
- use app\common\library\tokendoctor\Driver;
- use think\App;
- use think\Config;
- use think\Log;
- class Tokendoctor
- {
-
- public static $instance = [];
-
- public static $handler;
-
- public static function connect(array $options = [], $name = false)
- {
- $type = !empty($options['type']) ? $options['type'] : 'File';
- if (false === $name) {
- $name = md5(serialize($options));
- }
- if (true === $name || !isset(self::$instance[$name])) {
- $class = false === strpos($type, '\\') ?
- '\\app\\common\\library\\tokendoctor\\driver\\' . ucwords($type) :
- $type;
-
- App::$debug && Log::record('[ TOKEN ] INIT ' . $type, 'info');
- if (true === $name) {
- return new $class($options);
- }
- self::$instance[$name] = new $class($options);
- }
- return self::$instance[$name];
- }
-
- public static function init(array $options = [])
- {
- if (is_null(self::$handler)) {
- if (empty($options) && 'complex' == Config::get('token.type')) {
- $default = Config::get('token.default');
-
- $options = Config::get('token.' . $default['type']) ?: $default;
- } elseif (empty($options)) {
- $options = Config::get('token');
- }
- self::$handler = self::connect($options);
- }
- return self::$handler;
- }
-
- public static function has($token, $user_id)
- {
- return self::check($token, $user_id);
- }
-
- public static function check($token, $user_id)
- {
- return self::init()->check($token, $user_id);
- }
-
- public static function get($token, $default = false)
- {
- return self::init()->get($token) ?: $default;
- }
-
- public static function set($token, $user_id, $expire = null)
- {
- return self::init()->set($token, $user_id, $expire);
- }
-
- public static function rm($token)
- {
- return self::delete($token);
- }
-
- public static function delete($token)
- {
- return self::init()->delete($token);
- }
-
- public static function clear($user_id = null)
- {
- return self::init()->clear($user_id);
- }
- }
|