Class

Symfony\Component\DependencyInjection\ContainerBuilder

class ContainerBuilder extends Container implements TaggedContainerInterface

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

Methods

__construct(ParameterBagInterface $parameterBag = null)

Constructor.

from Container
compile()

Compiles the container.

Boolean isFrozen()

Returns true if the container parameter bag are frozen.

from Container
ParameterBagInterface getParameterBag()

Gets the service container parameter bag.

from Container
mixed getParameter(string $name)

Gets a parameter.

from Container
Boolean hasParameter(string $name)

Checks if a parameter exists.

from Container
setParameter(string $name, mixed $value)

Sets a parameter.

from Container
set(string $id, object $service, string $scope = self::SCOPE_CONTAINER)

Sets a service.

Boolean has(string $id)

Returns true if the given service is defined.

object get(string $id, integer $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)

Gets a service.

array getServiceIds()

Gets all service ids.

enterScope(string $name)

This is called when you enter a scope

from Container
leaveScope(string $name)

This is called to leave the current scope, and move back to the parent scope.

from Container
addScope(ScopeInterface $scope)

Adds a scope to the container.

from Container
Boolean hasScope(string $name)

Returns whether this container has a certain scope

from Container
Boolean isScopeActive(string $name)

Returns whether this scope is currently active

from Container
static string camelize(string $id)

Camelizes a string.

from Container
static string underscore(string $id)

A string to underscore.

from Container
registerExtension(ExtensionInterface $extension)

Registers an extension.

ExtensionInterface getExtension(string $name)

Returns an extension by alias or namespace.

array getExtensions()

Returns all registered extensions.

Boolean hasExtension(string $name)

Checks if we have an extension.

ResourceInterface[] getResources()

Returns an array of resources loaded to build this configuration.

ContainerBuilder addResource(ResourceInterface $resource)

Adds a resource for this configuration.

addObjectResource(object $object)

Adds the object class hierarchy as resources.

ContainerBuilder loadFromExtension(string $extension, array $values = array())

Loads the configuration for an extension.

addCompilerPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)

Adds a compiler pass.

PassConfig getCompilerPassConfig()

Returns the compiler pass config which can then be modified.

Compiler getCompiler()

Returns the compiler.

array getScopes()

Returns all Scopes.

array getScopeChildren()

Returns all Scope children.

removeDefinition(string $id)

Removes a service definition.

merge(ContainerBuilder $container)

Merges a ContainerBuilder with the current ContainerBuilder configuration.

array getExtensionConfig(string $name)

Returns the configuration array for the given extension.

addAliases(array $aliases)

Adds the service aliases.

setAliases(array $aliases)

Sets the service aliases.

setAlias(string $alias, string|Alias $id)

Sets an alias for an existing service.

removeAlias(string $alias)

Removes an alias.

Boolean hasAlias(string $id)

Returns true if an alias exists under the given identifier.

Alias[] getAliases()

Gets all defined aliases.

Alias getAlias(string $id)

Gets an alias.

Definition register(string $id, string $class = null)

Registers a service definition.

addDefinitions(array $definitions)

Adds the service definitions.

setDefinitions(array $definitions)

Sets the service definitions.

Definition[] getDefinitions()

Gets all service definitions.

Definition setDefinition(string $id, Definition $definition)

Sets a service definition.

Boolean hasDefinition(string $id)

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

Definition getDefinition(string $id)

Gets a service definition.

Definition findDefinition(string $id)

Gets a service definition by id or alias.

mixed resolveServices(mixed $value)

Replaces service references by the real service instance.

array findTaggedServiceIds(string $name)

Returns service ids for a given tag.

static array getServiceConditionals(mixed $value)

Returns the Service Conditionals.

Details

in Container at line 77
public __construct(ParameterBagInterface $parameterBag = null)

Constructor.

Parameters

ParameterBagInterface $parameterBag A ParameterBagInterface instance

at line 430
public compile()

Compiles the container.

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:

in Container at line 114
public Boolean isFrozen()

Returns true if the container parameter bag are frozen.

Return Value

Boolean true if the container parameter bag are frozen, false otherwise

in Container at line 126
public ParameterBagInterface getParameterBag()

Gets the service container parameter bag.

Return Value

ParameterBagInterface A ParameterBagInterface instance

in Container at line 142
public mixed getParameter(string $name)

Gets a parameter.

Parameters

string $name The parameter name

Return Value

mixed The parameter value

Exceptions

InvalidArgumentException if the parameter is not defined

in Container at line 156
public Boolean hasParameter(string $name)

Checks if a parameter exists.

Parameters

string $name The parameter name

Return Value

Boolean The presence of parameter in container

in Container at line 169
public setParameter(string $name, mixed $value)

Sets a parameter.

Parameters

string $name The parameter name
mixed $value The parameter value

at line 258
public set(string $id, object $service, string $scope = self::SCOPE_CONTAINER)

Sets a service.

Parameters

string $id The service identifier
object $service The service instance
string $scope The scope

Exceptions

BadMethodCallException if the container is frozen

at line 292
public Boolean has(string $id)

Returns true if the given service is defined.

Parameters

string $id The service identifier

Return Value

Boolean true if the service is defined, false otherwise

at line 314
public object get(string $id, integer $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)

Gets a service.

Parameters

string $id The service identifier
integer $invalidBehavior The behavior when the service does not exist

Return Value

object The associated service

Exceptions

InvalidArgumentException if the service is not defined
LogicException if the service has a circular reference to itself

See also

Reference

at line 452
public array getServiceIds()

Gets all service ids.

Return Value

array An array of all defined service ids

in Container at line 299
public enterScope(string $name)

This is called when you enter a scope

Parameters

string $name

Exceptions

RuntimeException When the parent scope is inactive
InvalidArgumentException When the scope does not exist

in Container at line 345
public leaveScope(string $name)

This is called to leave the current scope, and move back to the parent scope.

Parameters

string $name The name of the scope to leave

Exceptions

InvalidArgumentException if the scope is not active

in Container at line 384
public addScope(ScopeInterface $scope)

Adds a scope to the container.

Parameters

ScopeInterface $scope

Exceptions

InvalidArgumentException When the scope is invalid

in Container at line 418
public Boolean hasScope(string $name)

Returns whether this container has a certain scope

Parameters

string $name The name of the scope

Return Value

Boolean

in Container at line 434
public Boolean isScopeActive(string $name)

Returns whether this scope is currently active

This does not actually check if the passed scope actually exists.

Parameters

string $name

Return Value

Boolean

in Container at line 446
static public string camelize(string $id)

Camelizes a string.

Parameters

string $id A string to camelize

Return Value

string The camelized string

in Container at line 458
static public string underscore(string $id)

A string to underscore.

Parameters

string $id The string to underscore

Return Value

string The underscored string

at line 45
public registerExtension(ExtensionInterface $extension)

Registers an extension.

Parameters

ExtensionInterface $extension An extension instance

at line 65
public ExtensionInterface getExtension(string $name)

Returns an extension by alias or namespace.

Parameters

string $name An alias or a namespace

Return Value

ExtensionInterface An extension instance

Exceptions

LogicException if the extension is not registered

at line 85
public array getExtensions()

Returns all registered extensions.

Return Value

array An array of ExtensionInterface

at line 99
public Boolean hasExtension(string $name)

Checks if we have an extension.

Parameters

string $name The name of the extension

Return Value

Boolean If the extension exists

at line 111
public ResourceInterface[] getResources()

Returns an array of resources loaded to build this configuration.

Return Value

ResourceInterface[] An array of resources

at line 125
public ContainerBuilder addResource(ResourceInterface $resource)

Adds a resource for this configuration.

Parameters

ResourceInterface $resource A resource instance

Return Value

ContainerBuilder The current instance

at line 139
public addObjectResource(object $object)

Adds the object class hierarchy as resources.

Parameters

object $object An object instance

at line 159
public ContainerBuilder loadFromExtension(string $extension, array $values = array())

Loads the configuration for an extension.

Parameters

string $extension The extension alias or namespace
array $values An array of values that customizes the extension

Return Value

ContainerBuilder The current instance

Exceptions

LogicException if the container is frozen

at line 180
public addCompilerPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)

Adds a compiler pass.

Parameters

CompilerPassInterface $pass A compiler pass
string $type The type of compiler pass

at line 198
public PassConfig getCompilerPassConfig()

Returns the compiler pass config which can then be modified.

Return Value

PassConfig The compiler pass config

at line 214
public Compiler getCompiler()

Returns the compiler.

Return Value

Compiler The compiler

at line 230
public array getScopes()

Returns all Scopes.

Return Value

array An array of scopes

at line 242
public array getScopeChildren()

Returns all Scope children.

Return Value

array An array of scope children.

at line 278
public removeDefinition(string $id)

Removes a service definition.

Parameters

string $id The service identifier

at line 373
public merge(ContainerBuilder $container)

Merges a ContainerBuilder with the current ContainerBuilder configuration.

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(array('foo' => 'bar')); $loader = new LoaderXXX($container); $loader->load('resource_name'); $container->register('foo', new 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

ContainerBuilder $container The ContainerBuilder instance to merge.

Exceptions

LogicException when this ContainerBuilder is frozen

at line 405
public array getExtensionConfig(string $name)

Returns the configuration array for the given extension.

Parameters

string $name The name of the extension

Return Value

array An array of configuration

at line 464
public addAliases(array $aliases)

Adds the service aliases.

Parameters

array $aliases An array of aliases

at line 478
public setAliases(array $aliases)

Sets the service aliases.

Parameters

array $aliases An array of service definitions

at line 495
public setAlias(string $alias, string|Alias $id)

Sets an alias for an existing service.

Parameters

string $alias The alias to create
string|Alias $id The service to alias

Exceptions

InvalidArgumentException if the id is not a string or an Alias
InvalidArgumentException if the alias is for itself

at line 521
public removeAlias(string $alias)

Removes an alias.

Parameters

string $alias The alias to remove

at line 535
public Boolean hasAlias(string $id)

Returns true if an alias exists under the given identifier.

Parameters

string $id The service identifier

Return Value

Boolean true if the alias exists, false otherwise

at line 547
public Alias[] getAliases()

Gets all defined aliases.

Return Value

Alias[] An array of aliases

at line 563
public Alias getAlias(string $id)

Gets an alias.

Parameters

string $id The service identifier

Return Value

Alias An Alias instance

Exceptions

InvalidArgumentException if the alias does not exist

at line 587
public Definition register(string $id, string $class = null)

Registers a service definition.

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

Parameters

string $id The service identifier
string $class The service class

Return Value

Definition A Definition instance

at line 599
public addDefinitions(array $definitions)

Adds the service definitions.

Parameters

array $definitions An array of service definitions

at line 613
public setDefinitions(array $definitions)

Sets the service definitions.

Parameters

array $definitions An array of service definitions

at line 626
public Definition[] getDefinitions()

Gets all service definitions.

Return Value

Definition[] An array of Definition instances

at line 643
public Definition setDefinition(string $id, Definition $definition)

Sets a service definition.

Parameters

string $id The service identifier
Definition $definition A Definition instance

Return Value

Definition the service definition

Exceptions

BadMethodCallException if the container is frozen

at line 665
public Boolean hasDefinition(string $id)

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

Parameters

string $id The service identifier

Return Value

Boolean true if the service definition exists, false otherwise

at line 681
public Definition getDefinition(string $id)

Gets a service definition.

Parameters

string $id The service identifier

Return Value

Definition A Definition instance

Exceptions

InvalidArgumentException if the service definition does not exist

at line 705
public Definition findDefinition(string $id)

Gets a service definition by id or alias.

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

Parameters

string $id The service identifier or alias

Return Value

Definition A Definition instance

Exceptions

InvalidArgumentException if the service definition does not exist

at line 811
public mixed resolveServices(mixed $value)

Replaces service references by the real service instance.

Parameters

mixed $value A value

Return Value

mixed The same value with all service references replaced by the real service instances

at line 835
public array findTaggedServiceIds(string $name)

Returns service ids for a given tag.

Parameters

string $name The tag name

Return Value

array An array of tags

at line 854
static public array getServiceConditionals(mixed $value)

Returns the Service Conditionals.

Parameters

mixed $value An array of conditionals to return.

Return Value

array An array of Service conditionals