mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUG Safely handle empty injector factory responses
Fixes issue with ImageBackendFactory returning null and breaking injector
This commit is contained in:
parent
1658fe7617
commit
c070e989c4
@ -584,6 +584,11 @@ class Injector implements ContainerInterface
|
||||
$factory = isset($spec['factory']) ? $this->get($spec['factory']) : $this->getObjectCreator();
|
||||
$object = $factory->create($class, $constructorParams);
|
||||
|
||||
// Handle empty factory responses
|
||||
if (!$object) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// figure out if we have a specific id set or not. In some cases, we might be instantiating objects
|
||||
// that we don't manage directly; we don't want to store these in the service cache below
|
||||
if (!$id) {
|
||||
|
@ -14,6 +14,7 @@ use SilverStripe\Core\Tests\Injector\InjectorTest\CircularOne;
|
||||
use SilverStripe\Core\Tests\Injector\InjectorTest\CircularTwo;
|
||||
use SilverStripe\Core\Tests\Injector\InjectorTest\ConstructableObject;
|
||||
use SilverStripe\Core\Tests\Injector\InjectorTest\DummyRequirements;
|
||||
use SilverStripe\Core\Tests\Injector\InjectorTest\EmptyFactory;
|
||||
use SilverStripe\Core\Tests\Injector\InjectorTest\MyChildClass;
|
||||
use SilverStripe\Core\Tests\Injector\InjectorTest\MyParentClass;
|
||||
use SilverStripe\Core\Tests\Injector\InjectorTest\NeedsBothCirculars;
|
||||
@ -102,6 +103,21 @@ class InjectorTest extends SapphireTest
|
||||
);
|
||||
}
|
||||
|
||||
public function testEmptyFactory()
|
||||
{
|
||||
$this->expectException(InjectorNotFoundException::class);
|
||||
$injector = new Injector();
|
||||
$services = array(
|
||||
'SomeClass' => array(
|
||||
'class' => AnotherService::class,
|
||||
'factory' => EmptyFactory::class,
|
||||
)
|
||||
);
|
||||
|
||||
$injector->load($services);
|
||||
$injector->create('SomeClass');
|
||||
}
|
||||
|
||||
public function testConfiguredInjector()
|
||||
{
|
||||
$injector = new Injector();
|
||||
|
13
tests/php/Core/Injector/InjectorTest/EmptyFactory.php
Normal file
13
tests/php/Core/Injector/InjectorTest/EmptyFactory.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\Core\Tests\Injector\InjectorTest;
|
||||
|
||||
use SilverStripe\Core\Injector\Factory;
|
||||
|
||||
class EmptyFactory implements Factory
|
||||
{
|
||||
public function create($service, array $params = array())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user