class Container implements ContainerInterface, ResetInterface (View source)

Provides a container optimized for Drupal's needs.

This container implementation is compatible with the default Symfony dependency injection container and similar to the Symfony ContainerBuilder class, but optimized for speed.

It is based on a PHP array container definition dumped as a performance-optimized machine-readable format.

The best way to initialize this container is to use a Container Builder, compile it and then retrieve the definition via \Drupal\Component\DependencyInjection\Dumper\OptimizedPhpArrayDumper::getArray().

The retrieved array can be cached safely and then passed to this container via the constructor.

As the container is unfrozen by default, a second parameter can be passed to the container to "freeze" the parameter bag.

This container is different in behavior from the default Symfony container in the following ways:

  • It only allows lowercase service and parameter names, though it does only enforce it via assertions for performance reasons.
  • The following functions, that are not part of the interface, are explicitly not supported: getParameterBag(), isFrozen(), compile(), getAServiceWithAnIdByCamelCase().
  • The function getServiceIds() was added as it has a use-case in core and contrib.

Properties

protected array $parameters

The parameters of the container.

protected array $aliases

The aliases of the container.

protected array $serviceDefinitions

The service definitions of the container.

protected array $services

The instantiated services.

protected array $privateServices

The instantiated private services.

protected array $loading

The currently loading services.

protected bool $frozen

Whether the container parameters can still be changed.

Methods

__construct(array $container_definition = [])

Constructs a new Container instance.

get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)

{@inheritdoc}

reset()

Resets shared services from the container.

object
createService(array $definition, string $id)

Creates a service from a service definition.

set($id, $service)

{@inheritdoc}

has($id)

{@inheritdoc}

getParameter($name)

{@inheritdoc}

hasParameter($name)

{@inheritdoc}

setParameter($name, $value)

{@inheritdoc}

initialized($id)

{@inheritdoc}

array
resolveServicesAndParameters(array|object $arguments)

Resolves arguments that represent services or variables to the real values.

string[]
getAlternatives(string $search_key, array $keys)

Provides alternatives for a given array and key.

string[]
getServiceAlternatives(string $id)

Provides alternatives in case a service was not found.

string[]
getParameterAlternatives(string $name)

Provides alternatives in case a parameter was not found.

array
getServiceIds()

Gets all defined service IDs.

Details

__construct(array $container_definition = [])

Constructs a new Container instance.

Parameters

array $container_definition

An array containing the following keys:

  • aliases: The aliases of the container.
  • parameters: The parameters of the container.
  • services: The service definitions of the container.
  • frozen: Whether the container definition came from a frozen container builder or not.
  • machine_format: Whether this container definition uses the optimized machine-readable container format.

get($id, $invalid_behavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)

{@inheritdoc}

Parameters

$id
$invalid_behavior

reset()

Resets shared services from the container.

The container is not intended to be used again after being reset in a normal workflow. This method is meant as a way to release references for ref-counting. A subsequent call to ContainerInterface::get() will recreate a new instance of the shared service.

protected object createService(array $definition, string $id)

Creates a service from a service definition.

Parameters

array $definition

The service definition to create a service from.

string $id

The service identifier, necessary so it can be shared if its public.

Return Value

object

The service described by the service definition.

Exceptions

RuntimeException
InvalidArgumentException
ReflectionException

set($id, $service)

{@inheritdoc}

Parameters

$id
$service

has($id)

{@inheritdoc}

Parameters

$id

getParameter($name)

{@inheritdoc}

Parameters

$name

hasParameter($name)

{@inheritdoc}

Parameters

$name

setParameter($name, $value)

{@inheritdoc}

Parameters

$name
$value

initialized($id)

{@inheritdoc}

Parameters

$id

protected array resolveServicesAndParameters(array|object $arguments)

Resolves arguments that represent services or variables to the real values.

Parameters

array|object $arguments

The arguments to resolve.

Return Value

array

The resolved arguments.

Exceptions

RuntimeException
InvalidArgumentException

protected string[] getAlternatives(string $search_key, array $keys)

Provides alternatives for a given array and key.

Parameters

string $search_key

The search key to get alternatives for.

array $keys

The search space to search for alternatives in.

Return Value

string[]

An array of strings with suitable alternatives.

protected string[] getServiceAlternatives(string $id)

Provides alternatives in case a service was not found.

Parameters

string $id

The service to get alternatives for.

Return Value

string[]

An array of strings with suitable alternatives.

protected string[] getParameterAlternatives(string $name)

Provides alternatives in case a parameter was not found.

Parameters

string $name

The parameter to get alternatives for.

Return Value

string[]

An array of strings with suitable alternatives.

array getServiceIds()

Gets all defined service IDs.

Return Value

array

An array of all defined service IDs.