From 5c4aa4b33bb744d3e39e7d9565d85488b46fe81d Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 2 Mar 2024 10:40:55 +0900 Subject: [PATCH 1/5] refactor: use $options['component'] instead of $component We should use $options['component'] because it takes precedence. See the code below to create a shared instance. --- system/Config/Factories.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Config/Factories.php b/system/Config/Factories.php index c4f443cc3a2d..cfac66503b10 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -141,8 +141,8 @@ public static function __callStatic(string $component, array $arguments) $options = array_merge(self::getOptions($component), $options); if (! $options['getShared']) { - if (isset(self::$aliases[$component][$alias])) { - $class = self::$aliases[$component][$alias]; + if (isset(self::$aliases[$options['component']][$alias])) { + $class = self::$aliases[$options['component']][$alias]; return new $class(...$arguments); } From 06e0753b73bc075f332e4e01fe342a672e5743c3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 2 Mar 2024 10:49:36 +0900 Subject: [PATCH 2/5] feat: add Factories::get() --- system/Config/Factories.php | 14 ++++++++++++++ tests/system/Config/FactoriesTest.php | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/system/Config/Factories.php b/system/Config/Factories.php index cfac66503b10..08cfa1deaad6 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -173,6 +173,20 @@ public static function __callStatic(string $component, array $arguments) return self::$instances[$options['component']][$class]; } + /** + * Simple method to get the shared instance fast. + */ + public static function get(string $component, string $alias): ?object + { + if (isset(self::$aliases[$component][$alias])) { + $class = self::$aliases[$component][$alias]; + + return self::$instances[$component][$class]; + } + + return self::__callStatic($component, [$alias]); + } + /** * Gets the defined instance. If not exists, creates new one. * diff --git a/tests/system/Config/FactoriesTest.php b/tests/system/Config/FactoriesTest.php index 4b8c66c1e2e1..3f4e9a81d88d 100644 --- a/tests/system/Config/FactoriesTest.php +++ b/tests/system/Config/FactoriesTest.php @@ -465,4 +465,25 @@ public function testIsUpdated(array $data) $this->assertFalse(Factories::isUpdated('config')); } + + public function testGet() + { + $config = Factories::config('App'); + + $this->assertSame($config, Factories::get('config', 'App')); + } + + public function testGetNonexistentInstance() + { + $config = Factories::get('config', 'App'); + + $this->assertSame($config, Factories::config('App')); + } + + public function testGetNonexistentClass() + { + $config = Factories::get('config', 'NonexistentInstance'); + + $this->assertNull($config); + } } From 839b29d81bf4c2e7f65e0dd9f9413c081f6689b9 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 2 Mar 2024 16:55:19 +0900 Subject: [PATCH 3/5] refactor: config() uses Factories::get() --- system/Common.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/Common.php b/system/Common.php index 086c4db48b01..983a8f9335f0 100644 --- a/system/Common.php +++ b/system/Common.php @@ -214,6 +214,10 @@ function command(string $command) */ function config(string $name, bool $getShared = true) { + if ($getShared) { + return Factories::get('config', $name); + } + return Factories::config($name, ['getShared' => $getShared]); } } From 4462a8d3c9e88a69df0df2b0d78f71879afd70e8 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 5 Mar 2024 13:59:34 +0900 Subject: [PATCH 4/5] test: change classname --- tests/system/Config/FactoriesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system/Config/FactoriesTest.php b/tests/system/Config/FactoriesTest.php index 3f4e9a81d88d..74141ff69c56 100644 --- a/tests/system/Config/FactoriesTest.php +++ b/tests/system/Config/FactoriesTest.php @@ -482,7 +482,7 @@ public function testGetNonexistentInstance() public function testGetNonexistentClass() { - $config = Factories::get('config', 'NonexistentInstance'); + $config = Factories::get('config', 'NonexistentClass'); $this->assertNull($config); } From 48294a8db109f6ff94d6371f5d7e9316f8dc2709 Mon Sep 17 00:00:00 2001 From: kenjis Date: Tue, 5 Mar 2024 14:00:00 +0900 Subject: [PATCH 5/5] test: fix test method names --- tests/system/Config/FactoriesTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/Config/FactoriesTest.php b/tests/system/Config/FactoriesTest.php index 74141ff69c56..f40b0968fb74 100644 --- a/tests/system/Config/FactoriesTest.php +++ b/tests/system/Config/FactoriesTest.php @@ -466,14 +466,14 @@ public function testIsUpdated(array $data) $this->assertFalse(Factories::isUpdated('config')); } - public function testGet() + public function testGetReturnsFactoriesConfigInstance() { $config = Factories::config('App'); $this->assertSame($config, Factories::get('config', 'App')); } - public function testGetNonexistentInstance() + public function testGetCreatesConfigInstanceAndFactoriesConfigReturnsIt() { $config = Factories::get('config', 'App');