Gets a service.
@api
string $id The service identifier:
integer $invalidBehavior The behavior when the service does not exist:
object The associated service
InvalidArgumentException when no definitions are available
InactiveScopeException when the current scope is not active
LogicException when a circular dependency is detected
\Exception
Overrides Container::get
public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) {
$id = strtolower($id);
try {
return parent::get($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE);
} catch (InactiveScopeException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
} catch (InvalidArgumentException $e) {
if (isset($this->loading[$id])) {
throw new LogicException(sprintf('The service "%s" has a circular reference to itself.', $id), 0, $e);
}
if (!$this
->hasDefinition($id) && isset($this->aliasDefinitions[$id])) {
return $this
->get($this->aliasDefinitions[$id]);
}
try {
$definition = $this
->getDefinition($id);
} catch (InvalidArgumentException $e) {
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
}
$this->loading[$id] = true;
try {
$service = $this
->createService($definition, $id);
} catch (\Exception $e) {
unset($this->loading[$id]);
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return null;
}
throw $e;
}
unset($this->loading[$id]);
return $service;
}
}