From 38558d957dbceb8d73cf78bd2d681be975af69ab Mon Sep 17 00:00:00 2001 From: Luke Kuzmish <42181698+cosmastech@users.noreply.github.com> Date: Fri, 5 Jul 2024 12:41:55 -0400 Subject: [PATCH] [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 --- .../Support/MultipleInstanceManager.php | 17 +++++++++++++---- .../Fixtures/MultipleInstanceManager.php | 14 ++++++++++++++ .../Support/MultipleInstanceManagerTest.php | 5 +++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Support/MultipleInstanceManager.php b/src/Illuminate/Support/MultipleInstanceManager.php index 3525cf3c0798..c15577265027 100644 --- a/src/Illuminate/Support/MultipleInstanceManager.php +++ b/src/Illuminate/Support/MultipleInstanceManager.php @@ -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."); } } diff --git a/tests/Integration/Support/Fixtures/MultipleInstanceManager.php b/tests/Integration/Support/Fixtures/MultipleInstanceManager.php index 9c1500dc3b04..7c10ce48486a 100644 --- a/tests/Integration/Support/Fixtures/MultipleInstanceManager.php +++ b/tests/Integration/Support/Fixtures/MultipleInstanceManager.php @@ -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. * @@ -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 []; } diff --git a/tests/Integration/Support/MultipleInstanceManagerTest.php b/tests/Integration/Support/MultipleInstanceManagerTest.php index ceca19e317b8..90936ab52ae1 100644 --- a/tests/Integration/Support/MultipleInstanceManagerTest.php +++ b/tests/Integration/Support/MultipleInstanceManagerTest.php @@ -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()