PHP Class Symfony\Component\DependencyInjection\ContainerBuilder

Author: Fabien Potencier ([email protected])
Inheritance: extends Symfony\Component\DependencyInjection\Container, implements Symfony\Component\DependencyInjection\TaggedContainerInterface
Show file Open project: symfony/symfony Class Usage Examples

Public Methods

Method Description
addAliases ( array $aliases ) Adds the service aliases.
addClassResource ( ReflectionClass $class ) : ContainerBuilder Adds the given class hierarchy as resources.
addCompilerPass ( Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION ) : ContainerBuilder Adds a compiler pass.
addDefinitions ( array $definitions ) Adds the service definitions.
addExpressionLanguageProvider ( Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface $provider )
addObjectResource ( object $object ) : ContainerBuilder Adds the object class hierarchy as resources.
addResource ( Symfony\Component\Config\Resource\ResourceInterface $resource ) : ContainerBuilder Adds a resource for this configuration.
autowire ( string $id, null | string $class = null ) : Definition Registers an autowired service definition.
compile ( ) Compiles the container.
findDefinition ( string $id ) : Definition Gets a service definition by id or alias.
findTaggedServiceIds ( string $name ) : array Returns service ids for a given tag.
findTags ( ) : array Returns all tags the defined services use.
findUnusedTags ( ) : string[] Returns all tags not queried by findTaggedServiceIds.
get ( string $id, integer $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE ) : object Gets a service.
getAlias ( string $id ) : Alias Gets an alias.
getAliases ( ) : Alias[] Gets all defined aliases.
getCompiler ( ) : Compiler Returns the compiler.
getCompilerPassConfig ( ) : PassConfig Returns the compiler pass config which can then be modified.
getDefinition ( string $id ) : Definition Gets a service definition.
getDefinitions ( ) : Definition[] Gets all service definitions.
getEnvCounters ( ) : int[] Get statistics about env usage.
getExpressionLanguageProviders ( ) : Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface[]
getExtension ( string $name ) : Symfony\Component\DependencyInjection\Extension\ExtensionInterface Returns an extension by alias or namespace.
getExtensionConfig ( string $name ) : array Returns the configuration array for the given extension.
getExtensions ( ) : Symfony\Component\DependencyInjection\Extension\ExtensionInterface[] Returns all registered extensions.
getResources ( ) : Symfony\Component\Config\Resource\ResourceInterface[] Returns an array of resources loaded to build this configuration.
getServiceConditionals ( mixed $value ) : array Returns the Service Conditionals.
getServiceIds ( ) : array Gets all service ids.
has ( string $id ) : boolean Returns true if the given service is defined.
hasAlias ( string $id ) : boolean Returns true if an alias exists under the given identifier.
hasDefinition ( string $id ) : boolean Returns true if a service definition exists under the given identifier.
hasExtension ( string $name ) : boolean Checks if we have an extension.
isTrackingResources ( ) : boolean Checks if resources are tracked.
loadFromExtension ( string $extension, array $values = [] ) : ContainerBuilder Loads the configuration for an extension.
merge ( ContainerBuilder $container ) Merges a ContainerBuilder with the current ContainerBuilder configuration.
prependExtensionConfig ( string $name, array $config ) Prepends a config array to the configs of the given extension.
register ( string $id, string $class = null ) : Definition Registers a service definition.
registerExtension ( Symfony\Component\DependencyInjection\Extension\ExtensionInterface $extension ) Registers an extension.
removeAlias ( string $alias ) Removes an alias.
removeDefinition ( string $id ) Removes a service definition.
resolveEnvPlaceholders ( mixed $value, string | true | null $format = null, array &$usedEnvs = null ) : string Resolves env parameter placeholders in a string or an array.
resolveServices ( mixed $value ) : mixed Replaces service references by the real service instance and evaluates expressions.
set ( string $id, object $service ) Sets a service.
setAlias ( string $alias, string | Alias $id ) Sets an alias for an existing service.
setAliases ( array $aliases ) Sets the service aliases.
setDefinition ( string $id, Definition $definition ) : Definition Sets a service definition.
setDefinitions ( array $definitions ) Sets the service definitions.
setProxyInstantiator ( Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface $proxyInstantiator ) Sets the instantiator to be used when fetching proxies.
setResourceTracking ( boolean $track ) Sets the track resources flag.
setResources ( array $resources ) : ContainerBuilder Sets the resources for this configuration.

Private Methods

Method Description
callMethod ( $service, $call )
createService ( Definition $definition, string $id, boolean $tryProxy = true ) : object Creates a service for a service definition.
getExpressionLanguage ( )
getProxyInstantiator ( ) : Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface Retrieves the currently set proxy instantiator or instantiates one.
shareService ( Definition $definition, mixed $service, string $id ) Shares a given service in the container.

Method Details

addAliases() public method

Adds the service aliases.
public addAliases ( array $aliases )
$aliases array An array of aliases

addClassResource() public method

Adds the given class hierarchy as resources.
public addClassResource ( ReflectionClass $class ) : ContainerBuilder
$class ReflectionClass
return ContainerBuilder The current instance

addCompilerPass() public method

Adds a compiler pass.
public addCompilerPass ( Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION ) : ContainerBuilder
$pass Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface A compiler pass
$type string The type of compiler pass
return ContainerBuilder The current instance

addDefinitions() public method

Adds the service definitions.
public addDefinitions ( array $definitions )
$definitions array An array of service definitions

addExpressionLanguageProvider() public method

public addExpressionLanguageProvider ( Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface $provider )
$provider Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface

addObjectResource() public method

Adds the object class hierarchy as resources.
public addObjectResource ( object $object ) : ContainerBuilder
$object object An object instance
return ContainerBuilder The current instance

addResource() public method

Adds a resource for this configuration.
public addResource ( Symfony\Component\Config\Resource\ResourceInterface $resource ) : ContainerBuilder
$resource Symfony\Component\Config\Resource\ResourceInterface A resource instance
return ContainerBuilder The current instance

autowire() public method

This method implements a shortcut for using setDefinition() with an autowired definition.
public autowire ( string $id, null | string $class = null ) : Definition
$id string The service identifier
$class null | string The service class
return Definition The created definition

compile() public method

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.
public compile ( )

findDefinition() public method

The method "unaliases" recursively to return a Definition instance.
public findDefinition ( string $id ) : Definition
$id string The service identifier or alias
return Definition A Definition instance

findTaggedServiceIds() public method

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']; } }
public findTaggedServiceIds ( string $name ) : array
$name string The tag name
return array An array of tags with the tagged service as key, holding a list of attribute arrays

findTags() public method

Returns all tags the defined services use.
public findTags ( ) : array
return array An array of tags

findUnusedTags() public method

Returns all tags not queried by findTaggedServiceIds.
public findUnusedTags ( ) : string[]
return string[] An array of tags

get() public method

Gets a service.
See also: Reference
public get ( string $id, integer $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE ) : object
$id string The service identifier
$invalidBehavior integer The behavior when the service does not exist
return object The associated service

getAlias() public method

Gets an alias.
public getAlias ( string $id ) : Alias
$id string The service identifier
return Alias An Alias instance

getAliases() public method

Gets all defined aliases.
public getAliases ( ) : Alias[]
return Alias[] An array of aliases

getCompiler() public method

Returns the compiler.
public getCompiler ( ) : Compiler
return Symfony\Component\DependencyInjection\Compiler\Compiler The compiler

getCompilerPassConfig() public method

Returns the compiler pass config which can then be modified.
public getCompilerPassConfig ( ) : PassConfig
return Symfony\Component\DependencyInjection\Compiler\PassConfig The compiler pass config

getDefinition() public method

Gets a service definition.
public getDefinition ( string $id ) : Definition
$id string The service identifier
return Definition A Definition instance

getDefinitions() public method

Gets all service definitions.
public getDefinitions ( ) : Definition[]
return Definition[] An array of Definition instances

getEnvCounters() public method

Get statistics about env usage.
public getEnvCounters ( ) : int[]
return int[] The number of time each env vars has been resolved

getExpressionLanguageProviders() public method

public getExpressionLanguageProviders ( ) : Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface[]
return Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface[]

getExtension() public method

Returns an extension by alias or namespace.
public getExtension ( string $name ) : Symfony\Component\DependencyInjection\Extension\ExtensionInterface
$name string An alias or a namespace
return Symfony\Component\DependencyInjection\Extension\ExtensionInterface An extension instance

getExtensionConfig() public method

Returns the configuration array for the given extension.
public getExtensionConfig ( string $name ) : array
$name string The name of the extension
return array An array of configuration

getExtensions() public method

Returns all registered extensions.
public getExtensions ( ) : Symfony\Component\DependencyInjection\Extension\ExtensionInterface[]
return Symfony\Component\DependencyInjection\Extension\ExtensionInterface[] An array of ExtensionInterface

getResources() public method

Returns an array of resources loaded to build this configuration.
public getResources ( ) : Symfony\Component\Config\Resource\ResourceInterface[]
return Symfony\Component\Config\Resource\ResourceInterface[] An array of resources

getServiceConditionals() public static method

Returns the Service Conditionals.
public static getServiceConditionals ( mixed $value ) : array
$value mixed An array of conditionals to return
return array An array of Service conditionals

getServiceIds() public method

Gets all service ids.
public getServiceIds ( ) : array
return array An array of all defined service ids

has() public method

Returns true if the given service is defined.
public has ( string $id ) : boolean
$id string The service identifier
return boolean true if the service is defined, false otherwise

hasAlias() public method

Returns true if an alias exists under the given identifier.
public hasAlias ( string $id ) : boolean
$id string The service identifier
return boolean true if the alias exists, false otherwise

hasDefinition() public method

Returns true if a service definition exists under the given identifier.
public hasDefinition ( string $id ) : boolean
$id string The service identifier
return boolean true if the service definition exists, false otherwise

hasExtension() public method

Checks if we have an extension.
public hasExtension ( string $name ) : boolean
$name string The name of the extension
return boolean If the extension exists

isTrackingResources() public method

Checks if resources are tracked.
public isTrackingResources ( ) : boolean
return boolean true if resources are tracked, false otherwise

loadFromExtension() public method

Loads the configuration for an extension.
public loadFromExtension ( string $extension, array $values = [] ) : ContainerBuilder
$extension string The extension alias or namespace
$values array An array of values that customizes the extension
return ContainerBuilder The current instance

merge() public method

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.
public merge ( ContainerBuilder $container )
$container ContainerBuilder The ContainerBuilder instance to merge

prependExtensionConfig() public method

Prepends a config array to the configs of the given extension.
public prependExtensionConfig ( string $name, array $config )
$name string The name of the extension
$config array The config to set

register() public method

This methods allows for simple registration of service definition with a fluid interface.
public register ( string $id, string $class = null ) : Definition
$id string The service identifier
$class string The service class
return Definition A Definition instance

registerExtension() public method

Registers an extension.
public registerExtension ( Symfony\Component\DependencyInjection\Extension\ExtensionInterface $extension )
$extension Symfony\Component\DependencyInjection\Extension\ExtensionInterface An extension instance

removeAlias() public method

Removes an alias.
public removeAlias ( string $alias )
$alias string The alias to remove

removeDefinition() public method

Removes a service definition.
public removeDefinition ( string $id )
$id string The service identifier

resolveEnvPlaceholders() public method

Resolves env parameter placeholders in a string or an array.
public resolveEnvPlaceholders ( mixed $value, string | true | null $format = null, array &$usedEnvs = null ) : string
$value mixed The value to resolve
$format string | true | 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
return string The string with env parameters resolved

resolveServices() public method

Replaces service references by the real service instance and evaluates expressions.
public resolveServices ( mixed $value ) : mixed
$value mixed A value
return mixed The same value with all service references replaced by the real service instances and all expressions evaluated

set() public method

Sets a service.
public set ( string $id, object $service )
$id string The service identifier
$service object The service instance

setAlias() public method

Sets an alias for an existing service.
public setAlias ( string $alias, string | Alias $id )
$alias string The alias to create
$id string | Alias The service to alias

setAliases() public method

Sets the service aliases.
public setAliases ( array $aliases )
$aliases array An array of aliases

setDefinition() public method

Sets a service definition.
public setDefinition ( string $id, Definition $definition ) : Definition
$id string The service identifier
$definition Definition A Definition instance
return Definition the service definition

setDefinitions() public method

Sets the service definitions.
public setDefinitions ( array $definitions )
$definitions array An array of service definitions

setProxyInstantiator() public method

Sets the instantiator to be used when fetching proxies.
public setProxyInstantiator ( Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface $proxyInstantiator )
$proxyInstantiator Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface

setResourceTracking() public method

If you are not using the loaders and therefore don't want to depend on the Config component, set this flag to false.
public setResourceTracking ( boolean $track )
$track boolean true if you want to track resources, false otherwise

setResources() public method

Sets the resources for this configuration.
public setResources ( array $resources ) : ContainerBuilder
$resources array An array of resources
return ContainerBuilder The current instance