class ContainerBuilder extends Container implements TaggedContainerInterface

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

Methods

__construct(ParameterBagInterface $parameterBag = null)

No description

from Container
compile()

Compiles the container.

bool
isFrozen()

Returns true if the container parameter bag are frozen.

from Container
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)

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)

Returns true if the given service has actually been initialized.

from Container
reset()

Resets shared services from the container.

from Container
array
getServiceIds()

Gets all service ids.

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.

getExtension(string $name)

Returns an extension by alias or namespace.

getExtensions()

Returns all registered extensions.

bool
hasExtension(string $name)

Checks if we have an extension.

getResources()

Returns an array of resources loaded to build this configuration.

addResource(ResourceInterface $resource)

Adds a resource for this configuration.

setResources(array $resources)

Sets the resources for this configuration.

addObjectResource(object $object)

Adds the object class hierarchy as resources.

addClassResource(ReflectionClass $class)

Adds the given class hierarchy as resources.

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

Loads the configuration for an extension.

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

Adds a compiler pass.

getCompilerPassConfig()

Returns the compiler pass config which can then be modified.

getCompiler()

Returns the compiler.

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.

getAlias(string $id)

Gets an alias.

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

Registers a service definition.

addDefinitions(array $definitions)

Adds the service definitions.

setDefinitions(array $definitions)

Sets the service definitions.

getDefinitions()

Gets all service definitions.

setDefinition(string $id, Definition $definition)

Sets a service definition.

bool
hasDefinition(string $id)

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

getDefinition(string $id)

Gets a service definition.

findDefinition(string $id)

Gets a service definition by id or alias.

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.

string[]
findUnusedTags()

Returns all tags not queried by findTaggedServiceIds.

string
resolveEnvPlaceholders(string $string, string|null $format = null, array $usedEnvs = null)

Resolves env parameter placeholders in a string.

int[]
getEnvCounters()

Get statistics about env usage.

static array
getServiceConditionals(mixed $value)

Returns the Service Conditionals.

Details

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

Parameters

ParameterBagInterface $parameterBag A ParameterBagInterface instance

at line line 555
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:

  • The extension configurations are merged;
  • Parameter values are resolved;
  • The parameter bag is frozen;
  • Extension loading is disabled.

in Container at line line 104
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 line 114
ParameterBagInterface getParameterBag()

Gets the service container parameter bag.

Return Value

ParameterBagInterface A ParameterBagInterface instance

in Container at line line 128
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 line 140
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 line 151
setParameter(string $name, mixed $value)

Sets a parameter.

Parameters

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

at line line 365
set(string $id, object $service)

Sets a service.

Parameters

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

Exceptions

BadMethodCallException When this ContainerBuilder is frozen

at line line 396
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 line 418
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
ServiceCircularReferenceException When a circular reference is detected
ServiceNotFoundException When the service is not defined
Exception

See also

Reference

in Container at line line 330
bool initialized(string $id)

Returns true if the given service has actually been initialized.

Parameters

string $id

Return Value

bool true if the service has been initialized, false otherwise

in Container at line line 348
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.

at line line 589
array getServiceIds()

Gets all service ids.

Return Value

array An array of all defined service ids

in Container at line line 385
static string camelize(string $id)

Camelizes a string.

Parameters

string $id A string to camelize

Return Value

string The camelized string

in Container at line line 397
static string underscore(string $id)

A string to underscore.

Parameters

string $id The string to underscore

Return Value

string The underscored string

at line line 111
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 line 121
bool isTrackingResources()

Checks if resources are tracked.

Return Value

bool true if resources are tracked, false otherwise

at line line 131
setProxyInstantiator(InstantiatorInterface $proxyInstantiator)

Sets the instantiator to be used when fetching proxies.

Parameters

InstantiatorInterface $proxyInstantiator

at line line 141
registerExtension(ExtensionInterface $extension)

Registers an extension.

Parameters

ExtensionInterface $extension An extension instance

at line line 159
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 line 177
ExtensionInterface[] getExtensions()

Returns all registered extensions.

Return Value

ExtensionInterface[] An array of ExtensionInterface

at line line 189
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 line 199
ResourceInterface[] getResources()

Returns an array of resources loaded to build this configuration.

Return Value

ResourceInterface[] An array of resources

at line line 211
ContainerBuilder addResource(ResourceInterface $resource)

Adds a resource for this configuration.

Parameters

ResourceInterface $resource A resource instance

Return Value

ContainerBuilder The current instance

at line line 229
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 line 247
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 line 263
ContainerBuilder addClassResource(ReflectionClass $class)

Adds the given class hierarchy as resources.

Parameters

ReflectionClass $class

Return Value

ContainerBuilder The current instance

at line line 289
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 line 311
ContainerBuilder addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)

Adds a compiler pass.

Parameters

CompilerPassInterface $pass
$type

Return Value

ContainerBuilder The current instance

at line line 338
PassConfig getCompilerPassConfig()

Returns the compiler pass config which can then be modified.

Return Value

PassConfig The compiler pass config

at line line 348
Compiler getCompiler()

Returns the compiler.

Return Value

Compiler The compiler

at line line 384
removeDefinition(string $id)

Removes a service definition.

Parameters

string $id The service identifier

at line line 473
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 line 517
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 line 532
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 line 599
addAliases(array $aliases)

Adds the service aliases.

Parameters

array $aliases An array of aliases

at line line 611
setAliases(array $aliases)

Sets the service aliases.

Parameters

array $aliases An array of aliases

at line line 626
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 line 650
removeAlias(string $alias)

Removes an alias.

Parameters

string $alias The alias to remove

at line line 662
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 line 672
Alias[] getAliases()

Gets all defined aliases.

Return Value

Alias[] An array of aliases

at line line 686
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 line 708
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 line 718
addDefinitions(array $definitions)

Adds the service definitions.

Parameters

array $definitions An array of service definitions

at line line 730
setDefinitions(array $definitions)

Sets the service definitions.

Parameters

array $definitions An array of service definitions

at line line 741
Definition[] getDefinitions()

Gets all service definitions.

Return Value

Definition[] An array of Definition instances

at line line 756
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 line 776
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 line 790
Definition getDefinition(string $id)

Gets a service definition.

Parameters

string $id The service identifier

Return Value

Definition A Definition instance

Exceptions

ServiceNotFoundException if the service definition does not exist

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

ServiceNotFoundException if the service definition does not exist

at line line 942
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 line 977
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

at line line 995
array findTags()

Returns all tags the defined services use.

Return Value

array An array of tags

at line line 1010
string[] findUnusedTags()

Returns all tags not queried by findTaggedServiceIds.

Return Value

string[] An array of tags

at line line 1015
addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)

at line line 1023
ExpressionFunctionProviderInterface[] getExpressionLanguageProviders()

at line line 1037
string resolveEnvPlaceholders(string $string, string|null $format = null, array $usedEnvs = null)

Resolves env parameter placeholders in a string.

Parameters

string $string The string to resolve
string|null $format A sprintf() format to use as replacement for env placeholders or null to use the default parameter format
array $usedEnvs &$usedEnvs Env vars found while resolving are added to this array

Return Value

string The string with env parameters resolved

at line line 1064
int[] getEnvCounters()

Get statistics about env usage.

Return Value

int[] The number of time each env vars has been resolved

at line line 1085
static 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