Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
8 changes: 8 additions & 0 deletions tests/system/Config/FactoriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down