diff --git a/control/injector/Injector.php b/control/injector/Injector.php index cfe06843e..324f3be59 100644 --- a/control/injector/Injector.php +++ b/control/injector/Injector.php @@ -773,6 +773,9 @@ class Injector { if (isset($this->specs[$name])) { $spec = $this->specs[$name]; $this->updateSpecConstructor($spec); + if ($constructorArgs) { + $spec['constructor'] = $constructorArgs; + } return $this->instantiate($spec, $name); } } diff --git a/tests/injector/InjectorTest.php b/tests/injector/InjectorTest.php index 297ba3af2..904aaff88 100644 --- a/tests/injector/InjectorTest.php +++ b/tests/injector/InjectorTest.php @@ -523,6 +523,15 @@ class InjectorTest extends SapphireTest { $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 { @@ -531,6 +540,10 @@ class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator return array('class' => 'ConstructableObject', 'constructor' => array('%$OtherTestObject')); } + if ($name == 'ConfigConstructor') { + return array('class' => 'ConstructableObject', 'constructor' => array('value')); + } + return parent::locateConfigFor($name); } }