Merge pull request #1184 from ajshort/named-services

BUG: Fixed the injection of named services.
This commit is contained in:
Ingo Schommer 2013-02-15 10:46:33 -08:00
commit 37e10d14f3
2 changed files with 10 additions and 0 deletions

View File

@ -692,6 +692,7 @@ class Injector {
* Register a service with an explicit name
*/
public function registerNamedService($name, $service) {
$this->specs[$name] = array('class' => get_class($service));
$this->serviceCache[$name] = $service;
$this->inject($service);
}

View File

@ -523,6 +523,15 @@ class InjectorTest extends SapphireTest {
$this->assertInstanceOf('OtherTestObject', $item->property->property);
}
public function testNamedServices() {
$injector = new Injector();
$service = new stdClass();
$injector->registerNamedService('NamedService', $service);
$this->assertEquals($service, $injector->get('NamedService'));
}
}
class InjectorTestConfigLocator extends SilverStripeServiceConfigurationLocator implements TestOnly {