Manager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. declare(strict_types=1);
  12. namespace think;
  13. use InvalidArgumentException;
  14. use think\helper\Str;
  15. abstract class Manager
  16. {
  17. /**
  18. * 驱动
  19. * @var array
  20. */
  21. protected $drivers = [];
  22. /**
  23. * 驱动的命名空间
  24. * @var string
  25. */
  26. protected $namespace = null;
  27. public function __construct(protected App $app)
  28. {
  29. }
  30. /**
  31. * 获取驱动实例
  32. * @param null|string $name
  33. * @return mixed
  34. */
  35. protected function driver(string $name = null)
  36. {
  37. $name = $name ?: $this->getDefaultDriver();
  38. if (is_null($name)) {
  39. throw new InvalidArgumentException(sprintf(
  40. 'Unable to resolve NULL driver for [%s].',
  41. static::class
  42. ));
  43. }
  44. return $this->drivers[$name] = $this->getDriver($name);
  45. }
  46. /**
  47. * 获取驱动实例
  48. * @param string $name
  49. * @return mixed
  50. */
  51. protected function getDriver(string $name)
  52. {
  53. return $this->drivers[$name] ?? $this->createDriver($name);
  54. }
  55. /**
  56. * 获取驱动类型
  57. * @param string $name
  58. * @return mixed
  59. */
  60. protected function resolveType(string $name)
  61. {
  62. return $name;
  63. }
  64. /**
  65. * 获取驱动配置
  66. * @param string $name
  67. * @return mixed
  68. */
  69. protected function resolveConfig(string $name)
  70. {
  71. return $name;
  72. }
  73. /**
  74. * 获取驱动类
  75. * @param string $type
  76. * @return string
  77. */
  78. protected function resolveClass(string $type): string
  79. {
  80. if ($this->namespace || str_contains($type, '\\')) {
  81. $class = str_contains($type, '\\') ? $type : $this->namespace . Str::studly($type);
  82. if (class_exists($class)) {
  83. return $class;
  84. }
  85. }
  86. throw new InvalidArgumentException("Driver [$type] not supported.");
  87. }
  88. /**
  89. * 获取驱动参数
  90. * @param $name
  91. * @return array
  92. */
  93. protected function resolveParams($name): array
  94. {
  95. $config = $this->resolveConfig($name);
  96. return [$config];
  97. }
  98. /**
  99. * 创建驱动
  100. *
  101. * @param string $name
  102. * @return mixed
  103. *
  104. */
  105. protected function createDriver(string $name)
  106. {
  107. $type = $this->resolveType($name);
  108. $method = 'create' . Str::studly($type) . 'Driver';
  109. $params = $this->resolveParams($name);
  110. if (method_exists($this, $method)) {
  111. return $this->$method(...$params);
  112. }
  113. $class = $this->resolveClass($type);
  114. return $this->app->invokeClass($class, $params);
  115. }
  116. /**
  117. * 移除一个驱动实例
  118. *
  119. * @param array|string|null $name
  120. * @return $this
  121. */
  122. public function forgetDriver($name = null)
  123. {
  124. $name = $name ?? $this->getDefaultDriver();
  125. foreach ((array) $name as $cacheName) {
  126. if (isset($this->drivers[$cacheName])) {
  127. unset($this->drivers[$cacheName]);
  128. }
  129. }
  130. return $this;
  131. }
  132. /**
  133. * 默认驱动
  134. * @return string|null
  135. */
  136. abstract public function getDefaultDriver();
  137. /**
  138. * 动态调用
  139. * @param string $method
  140. * @param array $parameters
  141. * @return mixed
  142. */
  143. public function __call($method, $parameters)
  144. {
  145. return $this->driver()->$method(...$parameters);
  146. }
  147. }