123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
-
- namespace Illuminate\Support;
-
- use Closure;
- use InvalidArgumentException;
-
- abstract class Manager
- {
- /**
- * The application instance.
- *
- * @var \Illuminate\Foundation\Application
- */
- protected $app;
-
- /**
- * The registered custom driver creators.
- *
- * @var array
- */
- protected $customCreators = [];
-
- /**
- * The array of created "drivers".
- *
- * @var array
- */
- protected $drivers = [];
-
- /**
- * Create a new manager instance.
- *
- * @param \Illuminate\Foundation\Application $app
- * @return void
- */
- public function __construct($app)
- {
- $this->app = $app;
- }
-
- /**
- * Get the default driver name.
- *
- * @return string
- */
- abstract public function getDefaultDriver();
-
- /**
- * Get a driver instance.
- *
- * @param string $driver
- * @return mixed
- */
- public function driver($driver = null)
- {
- $driver = $driver ?: $this->getDefaultDriver();
-
- if (is_null($driver)) {
- throw new InvalidArgumentException(sprintf(
- 'Unable to resolve NULL driver for [%s].', static::class
- ));
- }
-
- // If the given driver has not been created before, we will create the instances
- // here and cache it so we can return it next time very quickly. If there is
- // already a driver created by this name, we'll just return that instance.
- if (! isset($this->drivers[$driver])) {
- $this->drivers[$driver] = $this->createDriver($driver);
- }
-
- return $this->drivers[$driver];
- }
-
- /**
- * Create a new driver instance.
- *
- * @param string $driver
- * @return mixed
- *
- * @throws \InvalidArgumentException
- */
- protected function createDriver($driver)
- {
- // We'll check to see if a creator method exists for the given driver. If not we
- // will check for a custom driver creator, which allows developers to create
- // drivers using their own customized driver creator Closure to create it.
- if (isset($this->customCreators[$driver])) {
- return $this->callCustomCreator($driver);
- } else {
- $method = 'create'.Str::studly($driver).'Driver';
-
- if (method_exists($this, $method)) {
- return $this->$method();
- }
- }
- throw new InvalidArgumentException("Driver [$driver] not supported.");
- }
-
- /**
- * Call a custom driver creator.
- *
- * @param string $driver
- * @return mixed
- */
- protected function callCustomCreator($driver)
- {
- return $this->customCreators[$driver]($this->app);
- }
-
- /**
- * Register a custom driver creator Closure.
- *
- * @param string $driver
- * @param \Closure $callback
- * @return $this
- */
- public function extend($driver, Closure $callback)
- {
- $this->customCreators[$driver] = $callback;
-
- return $this;
- }
-
- /**
- * Get all of the created "drivers".
- *
- * @return array
- */
- public function getDrivers()
- {
- return $this->drivers;
- }
-
- /**
- * Dynamically call the default driver instance.
- *
- * @param string $method
- * @param array $parameters
- * @return mixed
- */
- public function __call($method, $parameters)
- {
- return $this->driver()->$method(...$parameters);
- }
- }
|