123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: yunwuxin <448901948@qq.com>
- // +----------------------------------------------------------------------
- declare (strict_types = 1);
-
- namespace think;
-
- use InvalidArgumentException;
- use think\helper\Str;
-
- abstract class Manager
- {
- /** @var App */
- protected $app;
-
- /**
- * 驱动
- * @var array
- */
- protected $drivers = [];
-
- /**
- * 驱动的命名空间
- * @var string
- */
- protected $namespace = null;
-
- public function __construct(App $app)
- {
- $this->app = $app;
- }
-
- /**
- * 获取驱动实例
- * @param null|string $name
- * @return mixed
- */
- protected function driver(string $name = null)
- {
- $name = $name ?: $this->getDefaultDriver();
-
- if (is_null($name)) {
- throw new InvalidArgumentException(sprintf(
- 'Unable to resolve NULL driver for [%s].',
- static::class
- ));
- }
-
- return $this->drivers[$name] = $this->getDriver($name);
- }
-
- /**
- * 获取驱动实例
- * @param string $name
- * @return mixed
- */
- protected function getDriver(string $name)
- {
- return $this->drivers[$name] ?? $this->createDriver($name);
- }
-
- /**
- * 获取驱动类型
- * @param string $name
- * @return mixed
- */
- protected function resolveType(string $name)
- {
- return $name;
- }
-
- /**
- * 获取驱动配置
- * @param string $name
- * @return mixed
- */
- protected function resolveConfig(string $name)
- {
- return $name;
- }
-
- /**
- * 获取驱动类
- * @param string $type
- * @return string
- */
- 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.");
- }
-
- /**
- * 获取驱动参数
- * @param $name
- * @return array
- */
- protected function resolveParams($name): array
- {
- $config = $this->resolveConfig($name);
- return [$config];
- }
-
- /**
- * 创建驱动
- *
- * @param string $name
- * @return mixed
- *
- */
- protected function createDriver(string $name)
- {
- $type = $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);
-
- return $this->app->invokeClass($class, $params);
- }
-
- /**
- * 移除一个驱动实例
- *
- * @param array|string|null $name
- * @return $this
- */
- public function forgetDriver($name = null)
- {
- $name = $name ?? $this->getDefaultDriver();
-
- foreach ((array) $name as $cacheName) {
- if (isset($this->drivers[$cacheName])) {
- unset($this->drivers[$cacheName]);
- }
- }
-
- return $this;
- }
-
- /**
- * 默认驱动
- * @return string|null
- */
- abstract public function getDefaultDriver();
-
- /**
- * 动态调用
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- return $this->driver()->$method(...$parameters);
- }
- }
|