Manager.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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\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\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. public function driver($driver = null)
  48. {
  49. $driver = $driver ?: $this->getDefaultDriver();
  50. if (is_null($driver)) {
  51. throw new InvalidArgumentException(sprintf(
  52. 'Unable to resolve NULL driver for [%s].', static::class
  53. ));
  54. }
  55. // If the given driver has not been created before, we will create the instances
  56. // here and cache it so we can return it next time very quickly. If there is
  57. // already a driver created by this name, we'll just return that instance.
  58. if (! isset($this->drivers[$driver])) {
  59. $this->drivers[$driver] = $this->createDriver($driver);
  60. }
  61. return $this->drivers[$driver];
  62. }
  63. /**
  64. * Create a new driver instance.
  65. *
  66. * @param string $driver
  67. * @return mixed
  68. *
  69. * @throws \InvalidArgumentException
  70. */
  71. protected function createDriver($driver)
  72. {
  73. // We'll check to see if a creator method exists for the given driver. If not we
  74. // will check for a custom driver creator, which allows developers to create
  75. // drivers using their own customized driver creator Closure to create it.
  76. if (isset($this->customCreators[$driver])) {
  77. return $this->callCustomCreator($driver);
  78. } else {
  79. $method = 'create'.Str::studly($driver).'Driver';
  80. if (method_exists($this, $method)) {
  81. return $this->$method();
  82. }
  83. }
  84. throw new InvalidArgumentException("Driver [$driver] not supported.");
  85. }
  86. /**
  87. * Call a custom driver creator.
  88. *
  89. * @param string $driver
  90. * @return mixed
  91. */
  92. protected function callCustomCreator($driver)
  93. {
  94. return $this->customCreators[$driver]($this->app);
  95. }
  96. /**
  97. * Register a custom driver creator Closure.
  98. *
  99. * @param string $driver
  100. * @param \Closure $callback
  101. * @return $this
  102. */
  103. public function extend($driver, Closure $callback)
  104. {
  105. $this->customCreators[$driver] = $callback;
  106. return $this;
  107. }
  108. /**
  109. * Get all of the created "drivers".
  110. *
  111. * @return array
  112. */
  113. public function getDrivers()
  114. {
  115. return $this->drivers;
  116. }
  117. /**
  118. * Dynamically call the default driver instance.
  119. *
  120. * @param string $method
  121. * @param array $parameters
  122. * @return mixed
  123. */
  124. public function __call($method, $parameters)
  125. {
  126. return $this->driver()->$method(...$parameters);
  127. }
  128. }