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

Breadcrumb

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

function Container::make

Creates a service.

As a separate method to allow "get()" to use the really fast `??` operator.

File

vendor/symfony/dependency-injection/Container.php, line 211

Class

Container
Container is a dependency injection container.

Namespace

Symfony\Component\DependencyInjection

Code

private static function make(self $container, string $id, int $invalidBehavior) : ?object {
    if (isset($container->loading[$id])) {
        throw new ServiceCircularReferenceException($id, array_merge(array_keys($container->loading), [
            $id,
        ]));
    }
    $container->loading[$id] = true;
    try {
        if (isset($container->fileMap[$id])) {
            return 4 === $invalidBehavior ? null : $container->load($container->fileMap[$id]);
        }
        elseif (isset($container->methodMap[$id])) {
            return 4 === $invalidBehavior ? null : $container->{$container->methodMap[$id]}($container);
        }
    } catch (\Exception $e) {
        unset($container->services[$id]);
        throw $e;
    } finally {
        unset($container->loading[$id]);
    }
    if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {
        if (!$id) {
            throw new ServiceNotFoundException($id);
        }
        if (isset($container->syntheticIds[$id])) {
            throw new ServiceNotFoundException($id, null, null, [], \sprintf('The "%s" service is synthetic, it needs to be set at boot time before it can be used.', $id));
        }
        if (isset($container->getRemovedIds()[$id])) {
            throw new ServiceNotFoundException($id, null, null, [], \sprintf('The "%s" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.', $id));
        }
        $alternatives = [];
        foreach ($container->getServiceIds() as $knownId) {
            if ('' === $knownId || '.' === $knownId[0]) {
                continue;
            }
            $lev = levenshtein($id, $knownId);
            if ($lev <= \strlen($id) / 3 || str_contains($knownId, $id)) {
                $alternatives[] = $knownId;
            }
        }
        throw new ServiceNotFoundException($id, null, null, $alternatives);
    }
    return null;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal