MultipleInstanceManager.php 4.5 KB

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