Skip to content

Commit 38558d9

Browse files
[11.x] Allow MultipleInstanceManager to have studly creators (#52030)
* 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]>
1 parent afd85aa commit 38558d9

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/Illuminate/Support/MultipleInstanceManager.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,25 @@ protected function resolve($name)
115115
throw new RuntimeException("Instance [{$name}] does not specify a {$this->driverKey}.");
116116
}
117117

118-
if (isset($this->customCreators[$config[$this->driverKey]])) {
118+
$driverName = $config[$this->driverKey];
119+
120+
if (isset($this->customCreators[$driverName])) {
119121
return $this->callCustomCreator($config);
120122
} else {
121-
$createMethod = 'create'.ucfirst($config[$this->driverKey]).ucfirst($this->driverKey);
123+
$createMethod = 'create'.ucfirst($driverName).ucfirst($this->driverKey);
124+
125+
if (method_exists($this, $createMethod)) {
126+
return $this->{$createMethod}($config);
127+
128+
}
129+
130+
$createMethod = 'create'.Str::studly($driverName).ucfirst($this->driverKey);
122131

123132
if (method_exists($this, $createMethod)) {
124133
return $this->{$createMethod}($config);
125-
} else {
126-
throw new InvalidArgumentException("Instance {$this->driverKey} [{$config[$this->driverKey]}] is not supported.");
127134
}
135+
136+
throw new InvalidArgumentException("Instance {$this->driverKey} [{$config[$this->driverKey]}] is not supported.");
128137
}
129138
}
130139

tests/Integration/Support/Fixtures/MultipleInstanceManager.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ public function __construct($config)
3434
};
3535
}
3636

37+
protected function createMysqlDatabaseConnectionDriver(array $config) {
38+
return new class($config)
39+
{
40+
public function __construct(public $config)
41+
{
42+
}
43+
};
44+
}
45+
3746
/**
3847
* Get the default instance name.
3948
*
@@ -74,6 +83,11 @@ public function getInstanceConfig($name)
7483
'driver' => 'bar',
7584
'bar-option' => 'option-value',
7685
];
86+
case 'mysql_database-connection':
87+
return [
88+
'driver' => 'mysql_database-connection',
89+
'mysql_database-connection-option' => 'option-value'
90+
];
7791
default:
7892
return [];
7993
}

tests/Integration/Support/MultipleInstanceManagerTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ public function test_configurable_instances_can_be_resolved()
1818
$barInstance = $manager->instance('bar');
1919
$this->assertSame('option-value', $barInstance->config['bar-option']);
2020

21+
$mysqlInstance = $manager->instance('mysql_database-connection');
22+
$this->assertSame('option-value', $mysqlInstance->config['mysql_database-connection-option']);
23+
2124
$duplicateFooInstance = $manager->instance('foo');
2225
$duplicateBarInstance = $manager->instance('bar');
26+
$duplicateMysqlInstance = $manager->instance('mysql_database-connection');
2327
$this->assertEquals(spl_object_hash($fooInstance), spl_object_hash($duplicateFooInstance));
2428
$this->assertEquals(spl_object_hash($barInstance), spl_object_hash($duplicateBarInstance));
29+
$this->assertEquals(spl_object_hash($mysqlInstance), spl_object_hash($duplicateMysqlInstance));
2530
}
2631

2732
public function test_unresolvable_instances_throw_errors()

0 commit comments

Comments
 (0)