public getService ( string $name = null ) : object | ||
$name | string | |
return | object |
public function getService($name = null)
{
$name = $name ?: $this->defaultService;
if (!$name) {
throw new \RuntimeException(sprintf('You must configure a default %s service, registered %s services: "%s"', $this->serviceType, $this->serviceType, implode('", "', array_keys($this->services))));
}
if (isset($this->services[$name])) {
return $this->services[$name];
}
$this->assertServiceExists($name);
$this->services[$name] = $this->container->get($this->serviceMap[$name]);
return $this->services[$name];
}
/** * It should throw an exception if no argument given to get() and no default is defined. * * @expectedException RuntimeException * @expectedExceptionMessage You must configure a default test service, registered test services: "foo" */ public function testDefaultGetNoDefault() { $registry = new Registry('test', $this->container->reveal()); $registry->setService('foo', 'bar'); $this->assertEquals($registry->getService(), 'bar'); }