Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ServiceLocator.php

function ServiceLocator::createNotFoundException

Overrides ServiceLocatorTrait::createNotFoundException

File

vendor/symfony/dependency-injection/ServiceLocator.php, line 92

Class

ServiceLocator
@author Robin Chalas <robin.chalas@gmail.com> @author Nicolas Grekas <p@tchwork.com>

Namespace

Symfony\Component\DependencyInjection

Code

private function createNotFoundException(string $id) : NotFoundExceptionInterface {
    if ($this->loading) {
        $msg = \sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $this->formatAlternatives());
        return new ServiceNotFoundException($id, end($this->loading) ?: null, null, [], $msg);
    }
    $class = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS, 4);
    $class = isset($class[3]['object']) ? $class[3]['object']::class : null;
    $externalId = $this->externalId ?: $class;
    $msg = [];
    $msg[] = \sprintf('Service "%s" not found:', $id);
    if (!$this->container) {
        $class = null;
    }
    elseif ($this->container
        ->has($id) || isset($this->container
        ->getRemovedIds()[$id])) {
        $msg[] = 'even though it exists in the app\'s container,';
    }
    else {
        try {
            $this->container
                ->get($id);
            $class = null;
        } catch (ServiceNotFoundException $e) {
            if ($e->getAlternatives()) {
                $msg[] = \sprintf('did you mean %s? Anyway,', $this->formatAlternatives($e->getAlternatives(), 'or'));
            }
            else {
                $class = null;
            }
        }
    }
    if ($externalId) {
        $msg[] = \sprintf('the container inside "%s" is a smaller service locator that %s', $externalId, $this->formatAlternatives());
    }
    else {
        $msg[] = \sprintf('the current service locator %s', $this->formatAlternatives());
    }
    if (!$class) {
        // no-op
    }
    elseif (is_subclass_of($class, ServiceSubscriberInterface::class)) {
        $msg[] = \sprintf('Unless you need extra laziness, try using dependency injection instead. Otherwise, you need to declare it using "%s::getSubscribedServices()".', preg_replace('/([^\\\\]++\\\\)++/', '', $class));
    }
    else {
        $msg[] = 'Try using dependency injection instead.';
    }
    return new ServiceNotFoundException($id, end($this->loading) ?: null, null, [], implode(' ', $msg));
}
RSS feed
Powered by Drupal