Manager.php 4.0 KB

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