BaseManager.php 5.1 KB

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