Merge pull request #3201 from tractorcow/pulls/3.1/di-fix

BUG fix dependency injection stumbling over ViewableData's __isset
This commit is contained in:
Mateusz U 2014-06-12 10:41:37 +12:00
commit 020b590f23
2 changed files with 36 additions and 2 deletions

View File

@ -654,10 +654,10 @@ class Injector {
// If the type defines some injections, set them here
if ($injections && count($injections)) {
foreach ($injections as $property => $value) {
// we're checking isset in case it already has a property at this name
// we're checking empty in case it already has a property at this name
// this doesn't catch privately set things, but they will only be set by a setter method,
// which should be responsible for preventing further setting if it doesn't want it.
if (!isset($object->$property)) {
if (empty($object->$property)) {
$value = $this->convertServiceProperty($value);
$this->setObjectProperty($object, $property, $value);
}

View File

@ -458,6 +458,18 @@ class InjectorTest extends SapphireTest {
$this->assertEquals('NewRequirementsBackend', get_class($si->backend));
}
public function testSetterInjections() {
$injector = new Injector();
$config = array(
'NewRequirementsBackend',
);
$injector->load($config);
$si = $injector->get('TestSetterInjections');
$this->assertEquals('NewRequirementsBackend', get_class($si->getBackend()));
}
public function testCustomObjectCreator() {
$injector = new Injector();
$injector->setObjectCreator(new SSObjectCreator($injector));
@ -752,6 +764,28 @@ class TestStaticInjections implements TestOnly {
}
/**
* Make sure DI works with ViewableData's implementation of __isset
*/
class TestSetterInjections extends ViewableData implements TestOnly {
protected $backend;
/** @config */
private static $dependencies = array(
'backend' => '%$NewRequirementsBackend'
);
public function getBackend() {
return $this->backend;
}
public function setBackend($backend) {
$this->backend = $backend;
}
}
/**
* An example object creator that uses the SilverStripe class(arguments) mechanism for
* creating new objects