Skip to content

Commit

Permalink
[11.x] Make MultipleInstanceManager driver field customizable (#51905)
Browse files Browse the repository at this point in the history
* make MultipleInstanceManager driver field customizable

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
princejohnsantillan and taylorotwell authored Jun 27, 2024
1 parent 28b5d4e commit 96398e9
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/Illuminate/Support/MultipleInstanceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ abstract class MultipleInstanceManager
*/
protected $customCreators = [];

/**
* The key name of the "driver" equivalent configuration option.
*
* @var string
*/
protected $driverKey = 'driver';

/**
* Create a new manager instance.
*
Expand Down Expand Up @@ -104,19 +111,19 @@ protected function resolve($name)
throw new InvalidArgumentException("Instance [{$name}] is not defined.");
}

if (! array_key_exists('driver', $config)) {
throw new RuntimeException("Instance [{$name}] does not specify a driver.");
if (! array_key_exists($this->driverKey, $config)) {
throw new RuntimeException("Instance [{$name}] does not specify a {$this->driverKey}.");
}

if (isset($this->customCreators[$config['driver']])) {
if (isset($this->customCreators[$config[$this->driverKey]])) {
return $this->callCustomCreator($config);
} else {
$driverMethod = 'create'.ucfirst($config['driver']).'Driver';
$createMethod = 'create'.ucfirst($config[$this->driverKey]).ucfirst($this->driverKey);

if (method_exists($this, $driverMethod)) {
return $this->{$driverMethod}($config);
if (method_exists($this, $createMethod)) {
return $this->{$createMethod}($config);
} else {
throw new InvalidArgumentException("Instance driver [{$config['driver']}] is not supported.");
throw new InvalidArgumentException("Instance {$this->driverKey} [{$config[$this->driverKey]}] is not supported.");
}
}
}
Expand All @@ -129,7 +136,7 @@ protected function resolve($name)
*/
protected function callCustomCreator(array $config)
{
return $this->customCreators[$config['driver']]($this->app, $config);
return $this->customCreators[$config[$this->driverKey]]($this->app, $config);
}

/**
Expand Down

0 comments on commit 96398e9

Please sign in to comment.