123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- namespace app\common\library;
- use think\helper\Arr;
- use think\helper\Str;
- use think\facade\Config;
- use InvalidArgumentException;
- class Token
- {
-
- public $instance = [];
-
- public $handler;
-
- protected $namespace = '\\app\\common\\library\\token\\driver\\';
-
- public function getDriver(string $name = null)
- {
- if (!is_null($this->handler)) {
- return $this->handler;
- }
- $name = $name ?: $this->getDefaultDriver();
- if (is_null($name)) {
- throw new InvalidArgumentException(sprintf(
- 'Unable to resolve NULL driver for [%s].',
- static::class
- ));
- }
- return $this->createDriver($name);
- }
-
- protected function createDriver(string $name)
- {
- $type = (string)$this->resolveType($name);
- $method = 'create' . Str::studly($type) . 'Driver';
- $params = $this->resolveParams($name);
- if (method_exists($this, $method)) {
- return $this->$method(...$params);
- }
- $class = $this->resolveClass($type);
- if (isset($this->instance[$type])) {
- return $this->instance[$type];
- }
- return new $class(...$params);
- }
-
- protected function getDefaultDriver(): string
- {
- return $this->getConfig('default');
- }
-
- protected function getConfig(string $name = null, $default = null)
- {
- if (!is_null($name)) {
- return Config::get('buildadmin.token.' . $name, $default);
- }
- return Config::get('buildadmin.token');
- }
-
- protected function resolveParams($name): array
- {
- $config = $this->getStoreConfig($name);
- return [$config];
- }
-
- protected function resolveClass(string $type): string
- {
- if ($this->namespace || false !== strpos($type, '\\')) {
- $class = false !== strpos($type, '\\') ? $type : $this->namespace . Str::studly($type);
- if (class_exists($class)) {
- return $class;
- }
- }
- throw new InvalidArgumentException("Driver [$type] not supported.");
- }
-
- protected function getStoreConfig(string $store, string $name = null, $default = null)
- {
- if ($config = $this->getConfig("stores.{$store}")) {
- return Arr::get($config, $name, $default);
- }
- throw new InvalidArgumentException("Store [$store] not found.");
- }
-
- protected function resolveType(string $name)
- {
- return $this->getStoreConfig($name, 'type', 'Mysql');
- }
-
- public function set(string $token, string $type, int $user_id, int $expire = null): bool
- {
- return $this->getDriver()->set($token, $type, $user_id, $expire);
- }
-
- public function get(string $token, bool $expirationException = true): array
- {
- return $this->getDriver()->get($token, $expirationException);
- }
-
- public function check(string $token, string $type, int $user_id, bool $expirationException = true): bool
- {
- return $this->getDriver()->check($token, $type, $user_id, $expirationException);
- }
-
- public function delete(string $token): bool
- {
- return $this->getDriver()->delete($token);
- }
-
- public function clear(string $type, int $user_id): bool
- {
- return $this->getDriver()->clear($type, $user_id);
- }
- }
|