You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace crmeb\basic;
  3. use think\facade\Config;
  4. use think\helper\Str;
  5. use think\Container;
  6. /**
  7. * 驱动基类
  8. * Class BaseManager
  9. */
  10. abstract class BaseManager
  11. {
  12. /**
  13. * 驱动的命名空间
  14. * @var null
  15. */
  16. protected $namespace = null;
  17. /**
  18. * 配置
  19. * @var null
  20. */
  21. protected $configFile = null;
  22. /**
  23. * 配置
  24. * @var array
  25. */
  26. protected $config = [];
  27. /**
  28. * 驱动
  29. * @var array
  30. */
  31. protected $drivers = [];
  32. /**
  33. * 驱动类型
  34. * @var null
  35. */
  36. protected $name = null;
  37. /**
  38. * BaseManager constructor.
  39. * @param string|array|int $name驱动名称
  40. * @param array $config 配置
  41. */
  42. public function __construct($name = null, array $config = [])
  43. {
  44. $type = null;
  45. if (is_array($name)) {
  46. $config = $name;
  47. $name = null;
  48. }
  49. if (is_int($name)) {
  50. $type = $name;
  51. $name = null;
  52. }
  53. if ($name)
  54. $this->name = $name;
  55. if ($type && is_null($this->name)) {
  56. $this->setHandleType((int)$type - 1);
  57. }
  58. $this->config = $config;
  59. }
  60. /**
  61. * 提取配置文件名
  62. * @return $this
  63. */
  64. protected function getConfigFile()
  65. {
  66. if (is_null($this->configFile)) {
  67. $this->configFile = strtolower((new \ReflectionClass($this))->getShortName());
  68. }
  69. return $this;
  70. }
  71. /**
  72. * 设置文件句柄
  73. * @param int $type
  74. */
  75. protected function setHandleType(int $type)
  76. {
  77. $this->getConfigFile();
  78. $stores = array_keys(Config::get($this->configFile . '.stores', []));
  79. $name = $stores[$type] ?? null;
  80. if (!$name) {
  81. throw new \RuntimeException($this->configFile . ' type is not used');
  82. }
  83. $this->name = $name;
  84. }
  85. /**
  86. * 设置默认句柄
  87. * @return mixed
  88. */
  89. abstract protected function getDefaultDriver();
  90. /**
  91. * 动态调用
  92. * @param $method
  93. * @param $arguments
  94. */
  95. public function __call($method, $arguments)
  96. {
  97. return $this->driver()->{$method}(...$arguments);
  98. }
  99. /**
  100. * 获取驱动实例
  101. * @param null|string $name
  102. * @return mixed
  103. */
  104. protected function driver(string $name = null)
  105. {
  106. $name = $name ?: $this->name;
  107. $name = $name ?: $this->getDefaultDriver();
  108. if (is_null($name)) {
  109. throw new \InvalidArgumentException(sprintf(
  110. 'Unable to resolve NULL driver for [%s].', static::class
  111. ));
  112. }
  113. return $this->drivers[$name] = $this->getDriver($name);
  114. }
  115. /**
  116. * 获取驱动实例
  117. * @param string $name
  118. * @return mixed
  119. */
  120. protected function getDriver(string $name)
  121. {
  122. return $this->drivers[$name] ?? $this->createDriver($name);
  123. }
  124. /**
  125. * 获取驱动类型
  126. * @param string $name
  127. * @return mixed
  128. */
  129. protected function resolveType(string $name)
  130. {
  131. return $name;
  132. }
  133. /**
  134. * 创建驱动
  135. *
  136. * @param string $name
  137. * @return mixed
  138. *
  139. */
  140. protected function createDriver(string $name)
  141. {
  142. $type = $this->resolveType($name);
  143. $method = 'create' . Str::studly($type) . 'Driver';
  144. if (method_exists($this, $method)) {
  145. return $this->$method($name);
  146. }
  147. $class = $this->resolveClass($type);
  148. $this->name = $type;
  149. return $this->invokeClass($class);
  150. }
  151. /**
  152. * 获取驱动类
  153. * @param string $type
  154. * @return string
  155. */
  156. protected function resolveClass(string $type): string
  157. {
  158. if ($this->namespace || false !== strpos($type, '\\')) {
  159. $class = false !== strpos($type, '\\') ? $type : $this->namespace . Str::studly($type);
  160. if (class_exists($class)) {
  161. return $class;
  162. }
  163. }
  164. throw new \InvalidArgumentException("Driver [$type] not supported.");
  165. }
  166. /**
  167. * 实例化类
  168. * @param $class
  169. * @return mixed
  170. */
  171. protected function invokeClass($class)
  172. {
  173. if (!class_exists($class)) {
  174. throw new \RuntimeException('class not exists: ' . $class);
  175. }
  176. $this->getConfigFile();
  177. $handle = Container::getInstance()->invokeClass($class, [$this->name, $this->config, $this->configFile]);
  178. $this->config = [];
  179. return $handle;
  180. }
  181. }