Documentation

ContainerBuilder extends Container
in package
implements TaggedContainerInterface

ContainerBuilder is a DI container that provides an API to easily describe services.

Tags
author

Fabien Potencier fabien@symfony.com

Interfaces, Classes and Traits

TaggedContainerInterface
TaggedContainerInterface is the interface implemented when a container knows how to deals with tags.

Table of Contents

INTERNAL_TYPES  = ['int' => true, 'float' => true, 'string' => true, 'bool' => true, 'resource' => true, 'object' => true, 'array' => true, 'null' => true, 'callable' => true, 'iterable' => true, 'mixed' => true]
$aliases  : mixed
$factories  : mixed
$fileMap  : mixed
$loading  : mixed
$methodMap  : mixed
$parameterBag  : mixed
$privates  : mixed
$resolving  : mixed
$services  : mixed
$syntheticIds  : mixed
$aliasDefinitions  : array<string, Alias>
$autoconfiguredAttributes  : array<string, callable>
$autoconfiguredInstanceof  : array<string, ChildDefinition>
$classReflectors  : array<string, ReflectionClass>
$compiled  : mixed
$compiler  : Compiler
$definitions  : array<string, Definition>
$envCache  : mixed
$envCounters  : array<string|int, int>
$envPlaceholders  : array<string|int, array<string|int, string>>
$expressionLanguage  : ExpressionLanguage|null
$expressionLanguageProviders  : array<string|int, ExpressionFunctionProviderInterface>
$extensionConfigs  : array<string, array<string|int, array<string, mixed>>>
$extensions  : array<string, ExtensionInterface>
$extensionsByNs  : array<string, ExtensionInterface>
$getEnv  : mixed
$proxyInstantiator  : InstantiatorInterface|null
$removedBindingIds  : array<int, bool>
$removedIds  : array<string, bool>
$resources  : array<string, ResourceInterface>
$trackResources  : bool
$usedTags  : array<string|int, string>
$vendors  : array<string|int, string>
__construct()  : mixed
addAliases()  : mixed
Adds the service aliases.
addCompilerPass()  : $this
Adds a compiler pass.
addDefinitions()  : mixed
Adds the service definitions.
addExpressionLanguageProvider()  : mixed
addObjectResource()  : $this
Adds the object class hierarchy as resources.
addResource()  : $this
autowire()  : Definition
Registers an autowired service definition.
camelize()  : string
Camelizes a string.
compile()  : mixed
Compiles the container.
fileExists()  : bool
Checks whether the requested file or directory exists and registers the result for resource tracking.
findDefinition()  : Definition
Gets a service definition by id or alias.
findTaggedServiceIds()  : array<string, array<string|int, mixed>>
Returns service ids for a given tag.
findTags()  : array<string|int, string>
Returns all tags the defined services use.
findUnusedTags()  : array<string|int, string>
Returns all tags not queried by findTaggedServiceIds.
get()  : object|null
Gets a service.
getAlias()  : Alias
getAliases()  : array<string, Alias>
getAutoconfiguredAttributes()  : array<string, callable>
getAutoconfiguredInstanceof()  : array<string, ChildDefinition>
Returns an array of ChildDefinition[] keyed by interface.
getCompiler()  : Compiler
Returns the compiler.
getCompilerPassConfig()  : PassConfig
Returns the compiler pass config which can then be modified.
getDefinition()  : Definition
Gets a service definition.
getDefinitions()  : array<string, Definition>
Gets all service definitions.
getEnvCounters()  : array<string|int, int>
Get statistics about env usage.
getExpressionLanguageProviders()  : array<string|int, ExpressionFunctionProviderInterface>
getExtension()  : ExtensionInterface
Returns an extension by alias or namespace.
getExtensionConfig()  : array<string|int, array<string, mixed>>
Returns the configuration array for the given extension.
getExtensions()  : array<string, ExtensionInterface>
Returns all registered extensions.
getParameter()  : array<string|int, mixed>|bool|string|int|float|UnitEnum|null
Gets a parameter.
getParameterBag()  : ParameterBagInterface
Gets the service container parameter bag.
getReflectionClass()  : ReflectionClass|null
Retrieves the requested reflection class and registers it for resource tracking.
getRemovedIds()  : array<string, bool>
Gets removed service or alias ids.
getResources()  : array<string|int, ResourceInterface>
Returns an array of resources loaded to build this configuration.
getServiceIds()  : array<string|int, string>
Gets all service ids.
has()  : bool
Returns true if the given service is defined.
hasAlias()  : bool
hasDefinition()  : bool
Returns true if a service definition exists under the given identifier.
hasExtension()  : bool
Checks if we have an extension.
hash()  : string
Computes a reasonably unique hash of a value.
hasParameter()  : bool
initialized()  : bool
Returns true if the given service has actually been initialized.
isCompiled()  : bool
Returns true if the container is compiled.
isTrackingResources()  : bool
Checks if resources are tracked.
loadFromExtension()  : $this
Loads the configuration for an extension.
log()  : mixed
merge()  : mixed
Merges a ContainerBuilder with the current ContainerBuilder configuration.
prependExtensionConfig()  : mixed
Prepends a config array to the configs of the given extension.
register()  : Definition
Registers a service definition.
registerAliasForArgument()  : Alias
Registers an autowiring alias that only binds to a specific argument name.
registerAttributeForAutoconfiguration()  : void
Registers an attribute that will be used for autoconfiguring annotated classes.
registerExtension()  : mixed
registerForAutoconfiguration()  : ChildDefinition
Returns a ChildDefinition that will be used for autoconfiguring the interface/class.
removeAlias()  : mixed
removeDefinition()  : mixed
Removes a service definition.
reset()  : mixed
resolveEnvPlaceholders()  : mixed
Resolves env parameter placeholders in a string or an array.
resolveServices()  : mixed
Replaces service references by the real service instance and evaluates expressions.
set()  : mixed
Sets a service.
setAlias()  : Alias
Sets an alias for an existing service.
setAliases()  : mixed
Sets the service aliases.
setDefinition()  : Definition
Sets a service definition.
setDefinitions()  : mixed
Sets the service definitions.
setParameter()  : mixed
Sets a parameter.
setProxyInstantiator()  : mixed
Sets the instantiator to be used when fetching proxies.
setResources()  : $this
Sets the resources for this configuration.
setResourceTracking()  : mixed
Sets the track resources flag.
underscore()  : string
A string to underscore.
willBeAvailable()  : bool
Checks whether a class is available and will remain available in the "no-dev" mode of Composer.
getEnv()  : mixed
Fetches a variable from the environment.
load()  : mixed
Creates a service by requiring its factory file.
__clone()  : mixed
callMethod()  : mixed
createService()  : mixed
Creates a service for a service definition.
doGet()  : mixed
doResolveServices()  : mixed
getExpressionLanguage()  : ExpressionLanguage
inVendors()  : bool
make()  : mixed
Creates a service.
shareService()  : mixed
Shares a given service in the container.

Constants

INTERNAL_TYPES

private mixed INTERNAL_TYPES = ['int' => true, 'float' => true, 'string' => true, 'bool' => true, 'resource' => true, 'object' => true, 'array' => true, 'null' => true, 'callable' => true, 'iterable' => true, 'mixed' => true]

Properties

$syntheticIds

protected mixed $syntheticIds = []

$autoconfiguredAttributes

private array<string, callable> $autoconfiguredAttributes = []

$classReflectors

private array<string, ReflectionClass> $classReflectors

$envCounters

private array<string|int, int> $envCounters = []

a map of env vars to their resolution counter

$envPlaceholders

private array<string|int, array<string|int, string>> $envPlaceholders = []

a map of env var names to their placeholders

$extensionConfigs

private array<string, array<string|int, array<string, mixed>>> $extensionConfigs = []

$usedTags

private array<string|int, string> $usedTags = []

with tag names used by findTaggedServiceIds

$vendors

private array<string|int, string> $vendors

the list of vendor directories

Methods

addAliases()

Adds the service aliases.

public addAliases(array<string, string|\Symfony\Component\DependencyInjection\Alias> $aliases) : mixed
Parameters
$aliases : array<string, string|\Symfony\Component\DependencyInjection\Alias>
Return values
mixed

addCompilerPass()

Adds a compiler pass.

public addCompilerPass(CompilerPassInterface $pass[, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION ], int $priority) : $this
Parameters
$pass : CompilerPassInterface
$type : string = PassConfig::TYPE_BEFORE_OPTIMIZATION

The type of compiler pass

$priority : int

Used to sort the passes

Return values
$this

addDefinitions()

Adds the service definitions.

public addDefinitions(array<string, Definition$definitions) : mixed
Parameters
$definitions : array<string, Definition>
Return values
mixed

addObjectResource()

Adds the object class hierarchy as resources.

public addObjectResource(object|string $object) : $this
Parameters
$object : object|string

An object instance or class name

Return values
$this

autowire()

Registers an autowired service definition.

public autowire(string $id[, string|null $class = null ]) : Definition

This method implements a shortcut for using setDefinition() with an autowired definition.

Parameters
$id : string
$class : string|null = null
Return values
Definition

camelize()

Camelizes a string.

public static camelize(string $id) : string
Parameters
$id : string
Return values
string

compile()

Compiles the container.

public compile([bool $resolveEnvPlaceholders = false ]) : mixed

This method passes the container to compiler passes whose job is to manipulate and optimize the container.

The main compiler passes roughly do four things:

  • The extension configurations are merged;
  • Parameter values are resolved;
  • The parameter bag is frozen;
  • Extension loading is disabled.
Parameters
$resolveEnvPlaceholders : bool = false

Whether %env()% parameters should be resolved using the current env vars or be replaced by uniquely identifiable placeholders. Set to "true" when you want to use the current ContainerBuilder directly, keep to "false" when the container is dumped instead.

Return values
mixed

fileExists()

Checks whether the requested file or directory exists and registers the result for resource tracking.

public fileExists(string $path[, bool|string $trackContents = true ]) : bool
Parameters
$path : string

The file or directory path for which to check the existence

$trackContents : bool|string = true

Whether to track contents of the given resource. If a string is passed, it will be used as pattern for tracking contents of the requested directory

Tags
final
Return values
bool

findDefinition()

Gets a service definition by id or alias.

public findDefinition(string $id) : Definition

The method "unaliases" recursively to return a Definition instance.

Parameters
$id : string
Tags
throws
ServiceNotFoundException

if the service definition does not exist

Return values
Definition

findTaggedServiceIds()

Returns service ids for a given tag.

public findTaggedServiceIds(string $name[, bool $throwOnAbstract = false ]) : array<string, array<string|int, mixed>>

Example:

$container->register('foo')->addTag('my.tag', ['hello' => 'world']);

$serviceIds = $container->findTaggedServiceIds('my.tag'); foreach ($serviceIds as $serviceId => $tags) { foreach ($tags as $tag) { echo $tag['hello']; } }

Parameters
$name : string

The tag name

$throwOnAbstract : bool = false
Return values
array<string, array<string|int, mixed>>

An array of tags with the tagged service as key, holding a list of attribute arrays

findTags()

Returns all tags the defined services use.

public findTags() : array<string|int, string>
Return values
array<string|int, string>

findUnusedTags()

Returns all tags not queried by findTaggedServiceIds.

public findUnusedTags() : array<string|int, string>
Return values
array<string|int, string>

get()

Gets a service.

public get(string $id[, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE ]) : object|null
Parameters
$id : string

The service identifier

$invalidBehavior : int = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE

The behavior when the service does not exist

Tags
throws
InvalidArgumentException

when no definitions are available

throws
ServiceCircularReferenceException

When a circular reference is detected

throws
ServiceNotFoundException

When the service is not defined

throws
Exception
see
Reference
Return values
object|null

getAutoconfiguredAttributes()

public getAutoconfiguredAttributes() : array<string, callable>
Return values
array<string, callable>

getEnvCounters()

Get statistics about env usage.

public getEnvCounters() : array<string|int, int>
Return values
array<string|int, int>

The number of time each env vars has been resolved

getExtensionConfig()

Returns the configuration array for the given extension.

public getExtensionConfig(string $name) : array<string|int, array<string, mixed>>
Parameters
$name : string
Return values
array<string|int, array<string, mixed>>

getParameter()

Gets a parameter.

public getParameter(string $name) : array<string|int, mixed>|bool|string|int|float|UnitEnum|null
Parameters
$name : string
Tags
throws
InvalidArgumentException

if the parameter is not defined

Return values
array<string|int, mixed>|bool|string|int|float|UnitEnum|null

getReflectionClass()

Retrieves the requested reflection class and registers it for resource tracking.

public getReflectionClass(string|null $class[, bool $throw = true ]) : ReflectionClass|null
Parameters
$class : string|null
$throw : bool = true
Tags
throws
ReflectionException

when a parent class/interface/trait is not found and $throw is true

final
Return values
ReflectionClass|null

getRemovedIds()

Gets removed service or alias ids.

public getRemovedIds() : array<string, bool>
Return values
array<string, bool>

getServiceIds()

Gets all service ids.

public getServiceIds() : array<string|int, string>
Return values
array<string|int, string>

has()

Returns true if the given service is defined.

public has(string $id) : bool
Parameters
$id : string

The service identifier

Return values
bool

hasAlias()

public hasAlias(string $id) : bool
Parameters
$id : string
Return values
bool

hasDefinition()

Returns true if a service definition exists under the given identifier.

public hasDefinition(string $id) : bool
Parameters
$id : string
Return values
bool

hasExtension()

Checks if we have an extension.

public hasExtension(string $name) : bool
Parameters
$name : string
Return values
bool

hash()

Computes a reasonably unique hash of a value.

public static hash(mixed $value) : string
Parameters
$value : mixed

A serializable value

Return values
string

hasParameter()

public hasParameter(string $name) : bool
Parameters
$name : string
Return values
bool

initialized()

Returns true if the given service has actually been initialized.

public initialized(string $id) : bool
Parameters
$id : string
Return values
bool

isCompiled()

Returns true if the container is compiled.

public isCompiled() : bool
Return values
bool

isTrackingResources()

Checks if resources are tracked.

public isTrackingResources() : bool
Return values
bool

loadFromExtension()

Loads the configuration for an extension.

public loadFromExtension(string $extension[, array<string, mixed>|null $values = null ]) : $this
Parameters
$extension : string

The extension alias or namespace

$values : array<string, mixed>|null = null

An array of values that customizes the extension

Tags
throws
BadMethodCallException

When this ContainerBuilder is compiled

throws
LogicException

if the extension is not registered

Return values
$this

merge()

Merges a ContainerBuilder with the current ContainerBuilder configuration.

public merge(self $container) : mixed

Service definitions overrides the current defined ones.

But for parameters, they are overridden by the current ones. It allows the parameters passed to the container constructor to have precedence over the loaded ones.

$container = new ContainerBuilder(new ParameterBag(['foo' => 'bar']));
$loader = new LoaderXXX($container);
$loader->load('resource_name');
$container->register('foo', 'stdClass');

In the above example, even if the loaded resource defines a foo parameter, the value will still be 'bar' as defined in the ContainerBuilder constructor.

Parameters
$container : self
Tags
throws
BadMethodCallException

When this ContainerBuilder is compiled

Return values
mixed

prependExtensionConfig()

Prepends a config array to the configs of the given extension.

public prependExtensionConfig(string $name, array<string, mixed> $config) : mixed
Parameters
$name : string
$config : array<string, mixed>
Return values
mixed

register()

Registers a service definition.

public register(string $id[, string|null $class = null ]) : Definition

This methods allows for simple registration of service definition with a fluid interface.

Parameters
$id : string
$class : string|null = null
Return values
Definition

registerAliasForArgument()

Registers an autowiring alias that only binds to a specific argument name.

public registerAliasForArgument(string $id, string $type[, string|null $name = null ]) : Alias

The argument name is derived from $name if provided (from $id otherwise) using camel case: "foo.bar" or "foo_bar" creates an alias bound to "$fooBar"-named arguments with $type as type-hint. Such arguments will receive the service $id when autowiring is used.

Parameters
$id : string
$type : string
$name : string|null = null
Return values
Alias

registerAttributeForAutoconfiguration()

Registers an attribute that will be used for autoconfiguring annotated classes.

public registerAttributeForAutoconfiguration(class-string<\Symfony\Component\DependencyInjection\T> $attributeClass, callable $configurator) : void

The third argument passed to the callable is the reflector of the class/method/property/parameter that the attribute targets. Using one or many of \ReflectionClass|\ReflectionMethod|\ReflectionProperty|\ReflectionParameter as a type-hint for this argument allows filtering which attributes should be passed to the callable.

Parameters
$attributeClass : class-string<\Symfony\Component\DependencyInjection\T>
$configurator : callable
Tags
template

T

Return values
void

registerForAutoconfiguration()

Returns a ChildDefinition that will be used for autoconfiguring the interface/class.

public registerForAutoconfiguration(string $interface) : ChildDefinition
Parameters
$interface : string
Return values
ChildDefinition

removeAlias()

public removeAlias(string $alias) : mixed
Parameters
$alias : string
Return values
mixed

removeDefinition()

Removes a service definition.

public removeDefinition(string $id) : mixed
Parameters
$id : string
Return values
mixed

reset()

public reset() : mixed
Return values
mixed

resolveEnvPlaceholders()

Resolves env parameter placeholders in a string or an array.

public resolveEnvPlaceholders(mixed $value[, string|true|null $format = null ][, array<string|int, mixed> &$usedEnvs = null ]) : mixed
Parameters
$value : mixed

The value to resolve

$format : string|true|null = null

A sprintf() format returning the replacement for each env var name or null to resolve back to the original "%env(VAR)%" format or true to resolve to the actual values of the referenced env vars

$usedEnvs : array<string|int, mixed> = null

Env vars found while resolving are added to this array

Return values
mixed

The value with env parameters resolved if a string or an array is passed

resolveServices()

Replaces service references by the real service instance and evaluates expressions.

public resolveServices(mixed $value) : mixed
Parameters
$value : mixed
Return values
mixed

The same value with all service references replaced by the real service instances and all expressions evaluated

set()

Sets a service.

public set(string $id, object|null $service) : mixed
Parameters
$id : string
$service : object|null
Tags
throws
BadMethodCallException

When this ContainerBuilder is compiled

Return values
mixed

setAliases()

Sets the service aliases.

public setAliases(array<string, string|\Symfony\Component\DependencyInjection\Alias> $aliases) : mixed
Parameters
$aliases : array<string, string|\Symfony\Component\DependencyInjection\Alias>
Return values
mixed

setDefinitions()

Sets the service definitions.

public setDefinitions(array<string, Definition$definitions) : mixed
Parameters
$definitions : array<string, Definition>
Return values
mixed

setParameter()

Sets a parameter.

public setParameter(string $name, array<string|int, mixed>|bool|string|int|float|UnitEnum|null $value) : mixed
Parameters
$name : string

The parameter name

$value : array<string|int, mixed>|bool|string|int|float|UnitEnum|null

The parameter value

Return values
mixed

setResourceTracking()

Sets the track resources flag.

public setResourceTracking(bool $track) : mixed

If you are not using the loaders and therefore don't want to depend on the Config component, set this flag to false.

Parameters
$track : bool
Return values
mixed

underscore()

A string to underscore.

public static underscore(string $id) : string
Parameters
$id : string
Return values
string

willBeAvailable()

Checks whether a class is available and will remain available in the "no-dev" mode of Composer.

public final static willBeAvailable(string $package, string $class, array<string|int, mixed> $parentPackages) : bool

When parent packages are provided and if any of them is in dev-only mode, the class will be considered available even if it is also in dev-only mode.

Parameters
$package : string
$class : string
$parentPackages : array<string|int, mixed>
Return values
bool

getEnv()

Fetches a variable from the environment.

protected getEnv(string $name) : mixed
Parameters
$name : string
Return values
mixed

load()

Creates a service by requiring its factory file.

protected load(string $file) : mixed
Parameters
$file : string
Return values
mixed

__clone()

private __clone() : mixed
Return values
mixed

callMethod()

private callMethod(object $service, array<string|int, mixed> $call, array<string|int, mixed> &$inlineServices) : mixed
Parameters
$service : object
$call : array<string|int, mixed>
$inlineServices : array<string|int, mixed>
Return values
mixed

createService()

Creates a service for a service definition.

private createService(Definition $definition, array<string|int, mixed> &$inlineServices[, bool $isConstructorArgument = false ][, string|null $id = null ][, bool $tryProxy = true ]) : mixed
Parameters
$definition : Definition
$inlineServices : array<string|int, mixed>
$isConstructorArgument : bool = false
$id : string|null = null
$tryProxy : bool = true
Tags
throws
RuntimeException

When the factory definition is incomplete

throws
RuntimeException

When the service is a synthetic service

throws
InvalidArgumentException

When configure callable is not callable

Return values
mixed

doGet()

private doGet(string $id[, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE ][, array<string|int, mixed>|null &$inlineServices = null ][, bool $isConstructorArgument = false ]) : mixed
Parameters
$id : string
$invalidBehavior : int = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE
$inlineServices : array<string|int, mixed>|null = null
$isConstructorArgument : bool = false
Return values
mixed

doResolveServices()

private doResolveServices(mixed $value[, array<string|int, mixed> &$inlineServices = [] ][, bool $isConstructorArgument = false ]) : mixed
Parameters
$value : mixed
$inlineServices : array<string|int, mixed> = []
$isConstructorArgument : bool = false
Return values
mixed

inVendors()

private inVendors(string $path) : bool
Parameters
$path : string
Return values
bool

make()

Creates a service.

private make(string $id, int $invalidBehavior) : mixed

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

Parameters
$id : string
$invalidBehavior : int
Return values
mixed

shareService()

Shares a given service in the container.

private shareService(Definition $definition, mixed $service, string|null $id, array<string|int, mixed> &$inlineServices) : mixed
Parameters
$definition : Definition
$service : mixed
$id : string|null
$inlineServices : array<string|int, mixed>
Return values
mixed

Search results