Skip to content

Commit

Permalink
[11.x] Allow MultipleInstanceManager to have studly creators (#52030)
Browse files Browse the repository at this point in the history
* allow for creating a studly driver creator

* take out of else and decrease indentation

* remove formatting change

* Update MultipleInstanceManager.php

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
cosmastech and taylorotwell authored Jul 5, 2024
1 parent afd85aa commit 38558d9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Illuminate/Support/MultipleInstanceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,25 @@ protected function resolve($name)
throw new RuntimeException("Instance [{$name}] does not specify a {$this->driverKey}.");
}

if (isset($this->customCreators[$config[$this->driverKey]])) {
$driverName = $config[$this->driverKey];

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

if (method_exists($this, $createMethod)) {
return $this->{$createMethod}($config);

}

$createMethod = 'create'.Str::studly($driverName).ucfirst($this->driverKey);

if (method_exists($this, $createMethod)) {
return $this->{$createMethod}($config);
} else {
throw new InvalidArgumentException("Instance {$this->driverKey} [{$config[$this->driverKey]}] is not supported.");
}

throw new InvalidArgumentException("Instance {$this->driverKey} [{$config[$this->driverKey]}] is not supported.");
}
}

Expand Down
14 changes: 14 additions & 0 deletions tests/Integration/Support/Fixtures/MultipleInstanceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ public function __construct($config)
};
}

protected function createMysqlDatabaseConnectionDriver(array $config) {
return new class($config)
{
public function __construct(public $config)
{
}
};
}

/**
* Get the default instance name.
*
Expand Down Expand Up @@ -74,6 +83,11 @@ public function getInstanceConfig($name)
'driver' => 'bar',
'bar-option' => 'option-value',
];
case 'mysql_database-connection':
return [
'driver' => 'mysql_database-connection',
'mysql_database-connection-option' => 'option-value'
];
default:
return [];
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Integration/Support/MultipleInstanceManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ public function test_configurable_instances_can_be_resolved()
$barInstance = $manager->instance('bar');
$this->assertSame('option-value', $barInstance->config['bar-option']);

$mysqlInstance = $manager->instance('mysql_database-connection');
$this->assertSame('option-value', $mysqlInstance->config['mysql_database-connection-option']);

$duplicateFooInstance = $manager->instance('foo');
$duplicateBarInstance = $manager->instance('bar');
$duplicateMysqlInstance = $manager->instance('mysql_database-connection');
$this->assertEquals(spl_object_hash($fooInstance), spl_object_hash($duplicateFooInstance));
$this->assertEquals(spl_object_hash($barInstance), spl_object_hash($duplicateBarInstance));
$this->assertEquals(spl_object_hash($mysqlInstance), spl_object_hash($duplicateMysqlInstance));
}

public function test_unresolvable_instances_throw_errors()
Expand Down

0 comments on commit 38558d9

Please sign in to comment.