Manager.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace Illuminate\Support;
  3. use Closure;
  4. use Illuminate\Contracts\Container\Container;
  5. use InvalidArgumentException;
  6. abstract class Manager
  7. {
  8. /**
  9. * The container instance.
  10. *
  11. * @var \Illuminate\Contracts\Container\Container
  12. */
  13. protected $container;
  14. /**
  15. * The configuration repository instance.
  16. *
  17. * @var \Illuminate\Contracts\Config\Repository
  18. */
  19. protected $config;
  20. /**
  21. * The registered custom driver creators.
  22. *
  23. * @var array
  24. */
  25. protected $customCreators = [];
  26. /**
  27. * The array of created "drivers".
  28. *
  29. * @var array
  30. */
  31. protected $drivers = [];
  32. /**
  33. * Create a new manager instance.
  34. *
  35. * @param \Illuminate\Contracts\Container\Container $container
  36. */
  37. public function __construct(Container $container)
  38. {
  39. $this->container = $container;
  40. $this->config = $container->make('config');
  41. }
  42. /**
  43. * Get the default driver name.
  44. *
  45. * @return string|null
  46. */
  47. abstract public function getDefaultDriver();
  48. /**
  49. * Get a driver instance.
  50. *
  51. * @param string|null $driver
  52. * @return mixed
  53. *
  54. * @throws \InvalidArgumentException
  55. */
  56. public function driver($driver = null)
  57. {
  58. $driver = $driver ?: $this->getDefaultDriver();
  59. if (is_null($driver)) {
  60. throw new InvalidArgumentException(sprintf(
  61. 'Unable to resolve NULL driver for [%s].', static::class
  62. ));
  63. }
  64. // If the given driver has not been created before, we will create the instances
  65. // here and cache it so we can return it next time very quickly. If there is
  66. // already a driver created by this name, we'll just return that instance.
  67. return $this->drivers[$driver] ??= $this->createDriver($driver);
  68. }
  69. /**
  70. * Create a new driver instance.
  71. *
  72. * @param string $driver
  73. * @return mixed
  74. *
  75. * @throws \InvalidArgumentException
  76. */
  77. protected function createDriver($driver)
  78. {
  79. // First, we will determine if a custom driver creator exists for the given driver and
  80. // if it does not we will check for a creator method for the driver. Custom creator
  81. // callbacks allow developers to build their own "drivers" easily using Closures.
  82. if (isset($this->customCreators[$driver])) {
  83. return $this->callCustomCreator($driver);
  84. }
  85. $method = 'create'.Str::studly($driver).'Driver';
  86. if (method_exists($this, $method)) {
  87. return $this->$method();
  88. }
  89. throw new InvalidArgumentException("Driver [$driver] not supported.");
  90. }
  91. /**
  92. * Call a custom driver creator.
  93. *
  94. * @param string $driver
  95. * @return mixed
  96. */
  97. protected function callCustomCreator($driver)
  98. {
  99. return $this->customCreators[$driver]($this->container);
  100. }
  101. /**
  102. * Register a custom driver creator Closure.
  103. *
  104. * @param string $driver
  105. * @param \Closure $callback
  106. * @return $this
  107. */
  108. public function extend($driver, Closure $callback)
  109. {
  110. $this->customCreators[$driver] = $callback;
  111. return $this;
  112. }
  113. /**
  114. * Get all of the created "drivers".
  115. *
  116. * @return array
  117. */
  118. public function getDrivers()
  119. {
  120. return $this->drivers;
  121. }
  122. /**
  123. * Get the container instance used by the manager.
  124. *
  125. * @return \Illuminate\Contracts\Container\Container
  126. */
  127. public function getContainer()
  128. {
  129. return $this->container;
  130. }
  131. /**
  132. * Set the container instance used by the manager.
  133. *
  134. * @param \Illuminate\Contracts\Container\Container $container
  135. * @return $this
  136. */
  137. public function setContainer(Container $container)
  138. {
  139. $this->container = $container;
  140. return $this;
  141. }
  142. /**
  143. * Forget all of the resolved driver instances.
  144. *
  145. * @return $this
  146. */
  147. public function forgetDrivers()
  148. {
  149. $this->drivers = [];
  150. return $this;
  151. }
  152. /**
  153. * Dynamically call the default driver instance.
  154. *
  155. * @param string $method
  156. * @param array $parameters
  157. * @return mixed
  158. */
  159. public function __call($method, $parameters)
  160. {
  161. return $this->driver()->$method(...$parameters);
  162. }
  163. }