From 377bdfbd92ab7d4b5621861b6824fe0c9ebb31a0 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 2 Mar 2024 10:49:36 +0900 Subject: [PATCH] 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..df5a8b5e5324 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) + { + 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); + } }