Manager.php 3.2 KB

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