BaseManager.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. * Class BaseManager
  17. * @package crmeb\basic
  18. */
  19. abstract class BaseManager
  20. {
  21. /**
  22. * 驱动的命名空间
  23. * @var null
  24. */
  25. protected $namespace = null;
  26. /**
  27. * 配置
  28. * @var null
  29. */
  30. protected $configFile = null;
  31. /**
  32. * 配置
  33. * @var array
  34. */
  35. protected $config = [];
  36. /**
  37. * 驱动
  38. * @var array
  39. */
  40. protected $drivers = [];
  41. /**
  42. * 驱动类型
  43. * @var null
  44. */
  45. protected $name = null;
  46. /**
  47. * BaseManager constructor.
  48. * @param string|array|int $name驱动名称
  49. * @param array $config 配置
  50. */
  51. public function __construct($name = null, array $config = [])
  52. {
  53. $type = null;
  54. if (is_array($name)) {
  55. $config = $name;
  56. $name = null;
  57. }
  58. if (is_int($name)) {
  59. $type = $name;
  60. $name = null;
  61. }
  62. if ($name)
  63. $this->name = $name;
  64. if ($type && is_null($this->name)) {
  65. $this->setHandleType((int)$type - 1);
  66. }
  67. $this->config = $config;
  68. }
  69. /**
  70. * 提取配置文件名
  71. * @return $this
  72. */
  73. protected function getConfigFile()
  74. {
  75. if (is_null($this->configFile)) {
  76. $this->configFile = strtolower((new \ReflectionClass($this))->getShortName());
  77. }
  78. return $this;
  79. }
  80. /**
  81. * 设置文件句柄
  82. * @param int $type
  83. */
  84. protected function setHandleType(int $type)
  85. {
  86. $this->getConfigFile();
  87. $stores = array_keys(Config::get($this->configFile . '.stores', []));
  88. $name = $stores[$type] ?? null;
  89. if (!$name) {
  90. throw new \RuntimeException($this->configFile . ' type is not used');
  91. }
  92. $this->name = $name;
  93. }
  94. /**
  95. * 设置默认句柄
  96. * @return mixed
  97. */
  98. abstract protected function getDefaultDriver();
  99. /**
  100. * 动态调用
  101. * @param $method
  102. * @param $arguments
  103. * @return mixed
  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. }