Gets a service.
If a service is defined both through a set() method and with a get{$id}Service() method, the former has always precedence.
@api
string $id The service identifier:
integer $invalidBehavior The behavior when the service does not exist:
object The associated service
InvalidArgumentException if the service is not defined
Overrides ContainerInterface::get
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {
$id = strtolower($id);
if (isset($this->services[$id])) {
return $this->services[$id];
}
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
}
if (method_exists($this, $method = 'get' . strtr($id, array(
'_' => '',
'.' => '_',
)) . 'Service')) {
$this->loading[$id] = true;
try {
$service = $this
->{$method}();
} catch (\Exception $e) {
unset($this->loading[$id]);
throw $e;
}
unset($this->loading[$id]);
return $service;
}
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
throw new ServiceNotFoundException($id);
}
}