MultipleInstanceManager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace Illuminate\Support;
  3. use Closure;
  4. use InvalidArgumentException;
  5. use RuntimeException;
  6. abstract class MultipleInstanceManager
  7. {
  8. /**
  9. * The application instance.
  10. *
  11. * @var \Illuminate\Contracts\Foundation\Application
  12. */
  13. protected $app;
  14. /**
  15. * The configuration repository instance.
  16. *
  17. * @var \Illuminate\Contracts\Config\Repository
  18. */
  19. protected $config;
  20. /**
  21. * The array of resolved instances.
  22. *
  23. * @var array
  24. */
  25. protected $instances = [];
  26. /**
  27. * The registered custom instance creators.
  28. *
  29. * @var array
  30. */
  31. protected $customCreators = [];
  32. /**
  33. * The key name of the "driver" equivalent configuration option.
  34. *
  35. * @var string
  36. */
  37. protected $driverKey = 'driver';
  38. /**
  39. * Create a new manager instance.
  40. *
  41. * @param \Illuminate\Contracts\Foundation\Application $app
  42. */
  43. public function __construct($app)
  44. {
  45. $this->app = $app;
  46. $this->config = $app->make('config');
  47. }
  48. /**
  49. * Get the default instance name.
  50. *
  51. * @return string
  52. */
  53. abstract public function getDefaultInstance();
  54. /**
  55. * Set the default instance name.
  56. *
  57. * @param string $name
  58. * @return void
  59. */
  60. abstract public function setDefaultInstance($name);
  61. /**
  62. * Get the instance specific configuration.
  63. *
  64. * @param string $name
  65. * @return array
  66. */
  67. abstract public function getInstanceConfig($name);
  68. /**
  69. * Get an instance by name.
  70. *
  71. * @param string|null $name
  72. * @return mixed
  73. */
  74. public function instance($name = null)
  75. {
  76. $name = $name ?: $this->getDefaultInstance();
  77. return $this->instances[$name] = $this->get($name);
  78. }
  79. /**
  80. * Attempt to get an instance from the local cache.
  81. *
  82. * @param string $name
  83. * @return mixed
  84. */
  85. protected function get($name)
  86. {
  87. return $this->instances[$name] ?? $this->resolve($name);
  88. }
  89. /**
  90. * Resolve the given instance.
  91. *
  92. * @param string $name
  93. * @return mixed
  94. *
  95. * @throws \InvalidArgumentException
  96. * @throws \RuntimeException
  97. */
  98. protected function resolve($name)
  99. {
  100. $config = $this->getInstanceConfig($name);
  101. if (is_null($config)) {
  102. throw new InvalidArgumentException("Instance [{$name}] is not defined.");
  103. }
  104. if (! array_key_exists($this->driverKey, $config)) {
  105. throw new RuntimeException("Instance [{$name}] does not specify a {$this->driverKey}.");
  106. }
  107. $driverName = $config[$this->driverKey];
  108. if (isset($this->customCreators[$driverName])) {
  109. return $this->callCustomCreator($config);
  110. } else {
  111. $createMethod = 'create'.ucfirst($driverName).ucfirst($this->driverKey);
  112. if (method_exists($this, $createMethod)) {
  113. return $this->{$createMethod}($config);
  114. }
  115. $createMethod = 'create'.Str::studly($driverName).ucfirst($this->driverKey);
  116. if (method_exists($this, $createMethod)) {
  117. return $this->{$createMethod}($config);
  118. }
  119. throw new InvalidArgumentException("Instance {$this->driverKey} [{$config[$this->driverKey]}] is not supported.");
  120. }
  121. }
  122. /**
  123. * Call a custom instance creator.
  124. *
  125. * @param array $config
  126. * @return mixed
  127. */
  128. protected function callCustomCreator(array $config)
  129. {
  130. return $this->customCreators[$config[$this->driverKey]]($this->app, $config);
  131. }
  132. /**
  133. * Unset the given instances.
  134. *
  135. * @param array|string|null $name
  136. * @return $this
  137. */
  138. public function forgetInstance($name = null)
  139. {
  140. $name ??= $this->getDefaultInstance();
  141. foreach ((array) $name as $instanceName) {
  142. if (isset($this->instances[$instanceName])) {
  143. unset($this->instances[$instanceName]);
  144. }
  145. }
  146. return $this;
  147. }
  148. /**
  149. * Disconnect the given instance and remove from local cache.
  150. *
  151. * @param string|null $name
  152. * @return void
  153. */
  154. public function purge($name = null)
  155. {
  156. $name ??= $this->getDefaultInstance();
  157. unset($this->instances[$name]);
  158. }
  159. /**
  160. * Register a custom instance creator Closure.
  161. *
  162. * @param string $name
  163. * @param \Closure $callback
  164. *
  165. * @param-closure-this $this $callback
  166. *
  167. * @return $this
  168. */
  169. public function extend($name, Closure $callback)
  170. {
  171. $this->customCreators[$name] = $callback->bindTo($this, $this);
  172. return $this;
  173. }
  174. /**
  175. * Set the application instance used by the manager.
  176. *
  177. * @param \Illuminate\Contracts\Foundation\Application $app
  178. * @return $this
  179. */
  180. public function setApplication($app)
  181. {
  182. $this->app = $app;
  183. return $this;
  184. }
  185. /**
  186. * Dynamically call the default instance.
  187. *
  188. * @param string $method
  189. * @param array $parameters
  190. * @return mixed
  191. */
  192. public function __call($method, $parameters)
  193. {
  194. return $this->instance()->$method(...$parameters);
  195. }
  196. }