Manager.php 4.5 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. } else {
  89. $method = 'create'.Str::studly($driver).'Driver';
  90. if (method_exists($this, $method)) {
  91. return $this->$method();
  92. }
  93. }
  94. throw new InvalidArgumentException("Driver [$driver] not supported.");
  95. }
  96. /**
  97. * Call a custom driver creator.
  98. *
  99. * @param string $driver
  100. * @return mixed
  101. */
  102. protected function callCustomCreator($driver)
  103. {
  104. return $this->customCreators[$driver]($this->container);
  105. }
  106. /**
  107. * Register a custom driver creator Closure.
  108. *
  109. * @param string $driver
  110. * @param \Closure $callback
  111. * @return $this
  112. */
  113. public function extend($driver, Closure $callback)
  114. {
  115. $this->customCreators[$driver] = $callback;
  116. return $this;
  117. }
  118. /**
  119. * Get all of the created "drivers".
  120. *
  121. * @return array
  122. */
  123. public function getDrivers()
  124. {
  125. return $this->drivers;
  126. }
  127. /**
  128. * Get the container instance used by the manager.
  129. *
  130. * @return \Illuminate\Contracts\Container\Container
  131. */
  132. public function getContainer()
  133. {
  134. return $this->container;
  135. }
  136. /**
  137. * Set the container instance used by the manager.
  138. *
  139. * @param \Illuminate\Contracts\Container\Container $container
  140. * @return $this
  141. */
  142. public function setContainer(Container $container)
  143. {
  144. $this->container = $container;
  145. return $this;
  146. }
  147. /**
  148. * Forget all of the resolved driver instances.
  149. *
  150. * @return $this
  151. */
  152. public function forgetDrivers()
  153. {
  154. $this->drivers = [];
  155. return $this;
  156. }
  157. /**
  158. * Dynamically call the default driver instance.
  159. *
  160. * @param string $method
  161. * @param array $parameters
  162. * @return mixed
  163. */
  164. public function __call($method, $parameters)
  165. {
  166. return $this->driver()->$method(...$parameters);
  167. }
  168. }