Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

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