You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Manager.php 3.5KB

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