Container
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
Constructs a new Container instance.
{@inheritdoc}
Resets shared services from the container.
Creates a service from a service definition.
{@inheritdoc}
{@inheritdoc}
{@inheritdoc}
{@inheritdoc}
{@inheritdoc}
{@inheritdoc}
Resolves arguments that represent services or variables to the real values.
Provides alternatives for a given array and key.
Provides alternatives in case a service was not found.
Provides alternatives in case a parameter was not found.
Gets all defined service IDs.
Details
__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.
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.
set($id, $service)
{@inheritdoc}
has($id)
{@inheritdoc}
getParameter($name)
{@inheritdoc}
hasParameter($name)
{@inheritdoc}
setParameter($name, $value)
{@inheritdoc}
initialized($id)
{@inheritdoc}
protected array
resolveServicesAndParameters(array|object $arguments)
Resolves arguments that represent services or variables to the real values.
protected string[]
getAlternatives(string $search_key, array $keys)
Provides alternatives for a given array and key.
protected string[]
getServiceAlternatives(string $id)
Provides alternatives in case a service was not found.
protected string[]
getParameterAlternatives(string $name)
Provides alternatives in case a parameter was not found.
array
getServiceIds()
Gets all defined service IDs.