mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX issue with Injector::create not passing args
If creating an object using Injector::create() and constructor arguments are passed through, in some cases where the object being created had a yml configuration set for it, the passed in constructor arguments weren't being passed through to the instantiation of the object.
This commit is contained in:
parent
3dab996c90
commit
428cbe4b03
@ -773,6 +773,9 @@ class Injector {
|
|||||||
if (isset($this->specs[$name])) {
|
if (isset($this->specs[$name])) {
|
||||||
$spec = $this->specs[$name];
|
$spec = $this->specs[$name];
|
||||||
$this->updateSpecConstructor($spec);
|
$this->updateSpecConstructor($spec);
|
||||||
|
if ($constructorArgs) {
|
||||||
|
$spec['constructor'] = $constructorArgs;
|
||||||
|
}
|
||||||
return $this->instantiate($spec, $name);
|
return $this->instantiate($spec, $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -523,6 +523,15 @@ class InjectorTest extends SapphireTest {
|
|||||||
|
|
||||||
$this->assertInstanceOf('OtherTestObject', $item->property->property);
|
$this->assertInstanceOf('OtherTestObject', $item->property->property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testCreateConfiggedObjectWithCustomConstructorArgs() {
|
||||||
|
// need to make sure that even if the config defines some constructor params,
|
||||||
|
// that we take our passed in constructor args instead
|
||||||
|
$injector = new Injector(array('locator' => 'InjectorTestConfigLocator'));
|
||||||
|
|
||||||
|
$item = $injector->create('ConfigConstructor', 'othervalue');
|
||||||
|
$this->assertEquals($item->property, 'othervalue');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator implements TestOnly {
|
class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator implements TestOnly {
|
||||||
@ -531,6 +540,10 @@ class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator
|
|||||||
return array('class' => 'ConstructableObject', 'constructor' => array('%$OtherTestObject'));
|
return array('class' => 'ConstructableObject', 'constructor' => array('%$OtherTestObject'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($name == 'ConfigConstructor') {
|
||||||
|
return array('class' => 'ConstructableObject', 'constructor' => array('value'));
|
||||||
|
}
|
||||||
|
|
||||||
return parent::locateConfigFor($name);
|
return parent::locateConfigFor($name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user