diff --git a/system/Config/Factories.php b/system/Config/Factories.php index b4c6877a12ea..a82ec4613281 100644 --- a/system/Config/Factories.php +++ b/system/Config/Factories.php @@ -102,19 +102,23 @@ public static function __callStatic(string $component, array $arguments) if (isset(self::$basenames[$options['component']][$basename])) { $class = self::$basenames[$options['component']][$basename]; - } - else - { - // Try to locate the class - if (! $class = self::locateClass($options, $name)) + + // Need to verify if the shared instance matches the request + if (self::verifyInstanceOf($options, $class)) { - return null; + return self::$instances[$options['component']][$class]; } + } - self::$instances[$options['component']][$class] = new $class(...$arguments); - self::$basenames[$options['component']][$basename] = $class; + // Try to locate the class + if (! $class = self::locateClass($options, $name)) + { + return null; } + self::$instances[$options['component']][$class] = new $class(...$arguments); + self::$basenames[$options['component']][$basename] = $class; + return self::$instances[$options['component']][$class]; } diff --git a/tests/system/Config/FactoriesTest.php b/tests/system/Config/FactoriesTest.php index aefa7badd42d..4b0b28a1d7f0 100644 --- a/tests/system/Config/FactoriesTest.php +++ b/tests/system/Config/FactoriesTest.php @@ -217,6 +217,14 @@ public function testRespectsInstanceOf() $this->assertNull($result); } + public function testSharedRespectsInstanceOf() + { + Factories::injectMock('widgets', 'SomeWidget', new OtherWidget()); + + $result = Factories::widgets('SomeWidget', ['instanceOf' => stdClass::class]); + $this->assertInstanceOf(SomeWidget::class, $result); + } + public function testPrioritizesParameterOptions() { Factories::setOptions('widgets', ['instanceOf' => stdClass::class]);