Symfony2 API
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.

bool 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
bool 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.

bool has(string $id)

Returns true if the given service is defined.

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

Gets a service.

bool initialized(string $id)

Check for whether or not a service has been initialized.

from Container
array getServiceIds()

Gets all service ids.

enterScope(string $name)

Enters the given scope

from Container
leaveScope(string $name)

Leaves the current scope, and re-enters the parent scope

from Container
addScope(ScopeInterface $scope)

Adds a scope to the container

from Container
bool hasScope(string $name)

Whether this container has the given scope

from Container
bool isScopeActive(string $name)

Determines whether the given 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
setResourceTracking(bool $track)

Sets the track resources flag.

bool isTrackingResources()

Checks if resources are tracked.

setProxyInstantiator(InstantiatorInterface $proxyInstantiator)

Sets the instantiator to be used when fetching proxies.

registerExtension(ExtensionInterface $extension)

Registers an extension.

ExtensionInterface getExtension(string $name)

Returns an extension by alias or namespace.

ExtensionInterface[] getExtensions()

Returns all registered extensions.

bool 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.

ContainerBuilder setResources(array $resources)

Sets the resources for this configuration.

ContainerBuilder addObjectResource(object $object)

Adds the object class hierarchy as resources.

ContainerBuilder addClassResource(ReflectionClass $class)

Adds the given class hierarchy as resources.

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

Loads the configuration for an extension.

ContainerBuilder 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.

prependExtensionConfig(string $name, array $config)

Prepends a config array to the configs of 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.

bool 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.

bool 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.

object createService(Definition $definition, string $id, bool $tryProxy = true)

Creates a service for a service definition.

mixed resolveServices(mixed $value)

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

array findTaggedServiceIds(string $name)

Returns service ids for a given tag.

array findTags()

Returns all tags the defined services use.

addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)

static array getServiceConditionals(mixed $value)

Returns the Service Conditionals.

Details

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

Constructor.

Parameters

ParameterBagInterface $parameterBag A ParameterBagInterface instance

at line 605
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 117
public bool isFrozen()

Returns true if the container parameter bag are frozen.

Return Value

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

in Container at line 129
public ParameterBagInterface getParameterBag()

Gets the service container parameter bag.

Return Value

ParameterBagInterface A ParameterBagInterface instance

in Container at line 145
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 159
public bool hasParameter(string $name)

Checks if a parameter exists.

Parameters

string $name The parameter name

Return Value

bool The presence of parameter in container

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

Sets a parameter.

Parameters

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

at line 390
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 When this ContainerBuilder is frozen

at line 441
public bool has(string $id)

Returns true if the given service is defined.

Parameters

string $id The service identifier

Return Value

bool true if the service is defined, false otherwise

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

Gets a service.

Parameters

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

Return Value

object The associated service

Exceptions

InvalidArgumentException when no definitions are available
InactiveScopeException when the current scope is not active
LogicException when a circular dependency is detected
Exception

See also

Reference

in Container at line 353
public bool initialized(string $id)

Check for whether or not a service has been initialized.

Parameters

string $id The service identifier

Return Value

bool true if service has already been initialized, false otherwise

at line 635
public array getServiceIds()

Gets all service ids.

Return Value

array An array of all defined service ids

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

Enters the given scope

Parameters

string $name

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

Leaves the current scope, and re-enters the parent scope

Parameters

string $name The name of the scope to leave

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

Adds a scope to the container

Parameters

ScopeInterface $scope

in Container at line 524
public bool hasScope(string $name)

Whether this container has the given scope

Parameters

string $name The name of the scope

Return Value

bool

in Container at line 540
public bool isScopeActive(string $name)

Determines whether the given scope is currently active.

It does however not check if the scope actually exists.

Parameters

string $name

Return Value

bool

in Container at line 552
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 564
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 101
public setResourceTracking(bool $track)

Sets the track resources flag.

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

Parameters

bool $track true if you want to track resources, false otherwise

at line 111
public bool isTrackingResources()

Checks if resources are tracked.

Return Value

bool true if resources are tracked, false otherwise

at line 121
public setProxyInstantiator(InstantiatorInterface $proxyInstantiator)

Sets the instantiator to be used when fetching proxies.

Parameters

InstantiatorInterface $proxyInstantiator

at line 133
public registerExtension(ExtensionInterface $extension)

Registers an extension.

Parameters

ExtensionInterface $extension An extension instance

at line 153
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 173
public ExtensionInterface[] getExtensions()

Returns all registered extensions.

Return Value

ExtensionInterface[] An array of ExtensionInterface

at line 187
public bool hasExtension(string $name)

Checks if we have an extension.

Parameters

string $name The name of the extension

Return Value

bool If the extension exists

at line 199
public ResourceInterface[] getResources()

Returns an array of resources loaded to build this configuration.

Return Value

ResourceInterface[] An array of resources

at line 213
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 233
public ContainerBuilder setResources(array $resources)

Sets the resources for this configuration.

Parameters

array $resources An array of resources

Return Value

ContainerBuilder The current instance

at line 253
public ContainerBuilder addObjectResource(object $object)

Adds the object class hierarchy as resources.

Parameters

object $object An object instance

Return Value

ContainerBuilder The current instance

at line 269
public ContainerBuilder addClassResource(ReflectionClass $class)

Adds the given class hierarchy as resources.

Parameters

ReflectionClass $class

Return Value

ContainerBuilder The current instance

at line 295
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

BadMethodCallException When this ContainerBuilder is frozen
LogicException if the container is frozen

at line 318
public ContainerBuilder 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

Return Value

ContainerBuilder The current instance

at line 334
public PassConfig getCompilerPassConfig()

Returns the compiler pass config which can then be modified.

Return Value

PassConfig The compiler pass config

at line 346
public Compiler getCompiler()

Returns the compiler.

Return Value

Compiler The compiler

at line 362
public array getScopes()

Returns all Scopes.

Return Value

array An array of scopes

at line 374
public array getScopeChildren()

Returns all Scope children.

Return Value

array An array of scope children.

at line 427
public removeDefinition(string $id)

Removes a service definition.

Parameters

string $id The service identifier

at line 531
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

BadMethodCallException When this ContainerBuilder is frozen

at line 565
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 580
public prependExtensionConfig(string $name, array $config)

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

Parameters

string $name The name of the extension
array $config The config to set

at line 647
public addAliases(array $aliases)

Adds the service aliases.

Parameters

array $aliases An array of aliases

at line 661
public setAliases(array $aliases)

Sets the service aliases.

Parameters

array $aliases An array of aliases

at line 678
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 704
public removeAlias(string $alias)

Removes an alias.

Parameters

string $alias The alias to remove

at line 718
public bool hasAlias(string $id)

Returns true if an alias exists under the given identifier.

Parameters

string $id The service identifier

Return Value

bool true if the alias exists, false otherwise

at line 730
public Alias[] getAliases()

Gets all defined aliases.

Return Value

Alias[] An array of aliases

at line 746
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 770
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 782
public addDefinitions(array $definitions)

Adds the service definitions.

Parameters

array $definitions An array of service definitions

at line 796
public setDefinitions(array $definitions)

Sets the service definitions.

Parameters

array $definitions An array of service definitions

at line 809
public Definition[] getDefinitions()

Gets all service definitions.

Return Value

Definition[] An array of Definition instances

at line 826
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 When this ContainerBuilder is frozen

at line 848
public bool hasDefinition(string $id)

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

Parameters

string $id The service identifier

Return Value

bool true if the service definition exists, false otherwise

at line 864
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 888
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 913
public object createService(Definition $definition, string $id, bool $tryProxy = true)

Creates a service for a service definition.

Parameters

Definition $definition A service definition instance
string $id The service identifier
bool $tryProxy Whether to try proxying the service with a lazy proxy

Return Value

object The service described by the service definition

Exceptions

RuntimeException When the scope is inactive
RuntimeException When the factory definition is incomplete
RuntimeException When the service is a synthetic service
InvalidArgumentException When configure callable is not callable

at line 997
public mixed resolveServices(mixed $value)

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

Parameters

mixed $value A value

Return Value

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

at line 1034
public array findTaggedServiceIds(string $name)

Returns service ids for a given tag.

Example:

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

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

Parameters

string $name The tag name

Return Value

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

at line 1051
public array findTags()

Returns all tags the defined services use.

Return Value

array An array of tags

at line 1061
public addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)

at line 1073
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