Manager.php 4.4 KB

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