public function Container::get in Service Container 7.2
Same name in this branch
- 7.2 lib/Drupal/Component/DependencyInjection/Container.php \Drupal\Component\DependencyInjection\Container::get()
- 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Container.php \Symfony\Component\DependencyInjection\Container::get()
Same name and namespace in other branches
- 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/Container.php \Symfony\Component\DependencyInjection\Container::get()
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
Parameters
string $id The service identifier:
int $invalidBehavior The behavior when the service does not exist:
Return value
object The associated service
Throws
ServiceCircularReferenceException When a circular reference is detected
ServiceNotFoundException When the service is not defined
\Exception if an exception has been thrown when the service has been resolved
See also
File
- modules/
providers/ service_container_symfony/ lib/ Symfony/ Component/ DependencyInjection/ Container.php, line 275
Class
- Container
- Container is a dependency injection container.
Namespace
Symfony\Component\DependencyInjectionCode
public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE) {
// Attempt to retrieve the service by checking first aliases then
// available services. Service IDs are case insensitive, however since
// this method can be called thousands of times during a request, avoid
// calling strtolower() unless necessary.
for ($i = 2;;) {
if ('service_container' === $id) {
return $this;
}
if (isset($this->aliases[$id])) {
$id = $this->aliases[$id];
}
// Re-use shared service instance if it exists.
if (isset($this->services[$id]) || array_key_exists($id, $this->services)) {
return $this->services[$id];
}
if (isset($this->loading[$id])) {
throw new ServiceCircularReferenceException($id, array_keys($this->loading));
}
if (isset($this->methodMap[$id])) {
$method = $this->methodMap[$id];
}
elseif (--$i && $id !== ($lcId = strtolower($id))) {
$id = $lcId;
continue;
}
elseif (method_exists($this, $method = 'get' . strtr($id, $this->underscoreMap) . 'Service')) {
// $method is set to the right value, proceed
}
else {
if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
if (!$id) {
throw new ServiceNotFoundException($id);
}
$alternatives = array();
foreach ($this->services as $key => $associatedService) {
$lev = levenshtein($id, $key);
if ($lev <= strlen($id) / 3 || false !== strpos($key, $id)) {
$alternatives[] = $key;
}
}
throw new ServiceNotFoundException($id, null, null, $alternatives);
}
return;
}
$this->loading[$id] = true;
try {
$service = $this
->{$method}();
} catch (\Exception $e) {
unset($this->loading[$id]);
if (array_key_exists($id, $this->services)) {
unset($this->services[$id]);
}
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
return;
}
throw $e;
}
unset($this->loading[$id]);
return $service;
}
}