Copied!

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

Instantiable
Constants
public Symfony\Component\DependencyInjection\ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE = 1
public Symfony\Component\DependencyInjection\ContainerInterface::IGNORE_ON_INVALID_REFERENCE = 3
public Symfony\Component\DependencyInjection\ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE = 4
public Symfony\Component\DependencyInjection\ContainerInterface::NULL_ON_INVALID_REFERENCE = 2
public Symfony\Component\DependencyInjection\ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE = 0
Methods
public __construct(?Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface $parameterBag = NULL)
public addAliases(array $aliases) : void
 

Adds the service aliases.

  • param array<string,string|\Alias> $aliases
public addCompilerPass(Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface $pass, string $type = 'beforeOptimization'Symfony\Component\DependencyInjection\Compiler\PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0) : static
 

Adds a compiler pass.

  • param string $type The type of compiler pass
  • param int $priority Used to sort the passes
  • return $this
public addDefinitions(array $definitions) : void
 

Adds the service definitions.

  • param array<string,\Definition> $definitions
public addExpressionLanguageProvider(Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface $provider) : void
public addObjectResource(object|string $object) : static
 

Adds the object class hierarchy as resources.

  • param object|string $object An object instance or class name
  • return $this
public addResource(Symfony\Component\Config\Resource\ResourceInterface $resource) : static
 
  • return $this
public autowire(string $id, ?string $class = NULL) : Symfony\Component\DependencyInjection\Definition
 

Registers an autowired service definition.

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

public static Symfony\Component\DependencyInjection\Container::camelize(string $id) : string
 

Camelizes a string.

public compile(bool $resolveEnvPlaceholders = false) : void
 

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.
  • param bool $resolveEnvPlaceholders 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.
public deprecateParameter(string $name, string $package, string $version, string $message = 'The parameter "%s" is deprecated.') : void
 

Deprecates a service container parameter.

  • throws \ParameterNotFoundException if the parameter is not defined
public fileExists(string $path, string|bool $trackContents = true) : bool
 

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

  • param string $path The file or directory path for which to check the existence
  • param bool|string $trackContents 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
  • final
public findDefinition(string $id) : Symfony\Component\DependencyInjection\Definition
 

Gets a service definition by id or alias.

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

  • throws \ServiceNotFoundException if the service definition does not exist
public findTaggedServiceIds(string $name, bool $throwOnAbstract = false) : array
 

Returns service ids for a given tag.

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']; } }

  • return array<string,array> An array of tags with the tagged service as key, holding a list of attribute arrays
public findTags() : array
 

Returns all tags the defined services use.

  • return string[]
public findUnusedTags() : array
 

Returns all tags not queried by findTaggedServiceIds.

  • return string[]
public get(string $id, int $invalidBehavior = 1Symfony\Component\DependencyInjection\ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE) : ?object
 
  • 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
public getAlias(string $id) : Symfony\Component\DependencyInjection\Alias
 
  • throws \InvalidArgumentException if the alias does not exist
public getAliases() : array
 
  • return array<string,\Alias>
public getAutoconfiguredAttributes() : array
 
  • return array<string,callable>
public getAutoconfiguredInstanceof() : array
 

Returns an array of ChildDefinition[] keyed by interface.

  • return array<string,\ChildDefinition>
public getCompiler() : Symfony\Component\DependencyInjection\Compiler\Compiler
 

Returns the compiler.

public getCompilerPassConfig() : Symfony\Component\DependencyInjection\Compiler\PassConfig
 

Returns the compiler pass config which can then be modified.

public getDefinition(string $id) : Symfony\Component\DependencyInjection\Definition
 

Gets a service definition.

  • throws \ServiceNotFoundException if the service definition does not exist
public getDefinitions() : array
 

Gets all service definitions.

  • return array<string,\Definition>
public getEnvCounters() : array
 

Get statistics about env usage.

  • return int[] The number of time each env vars has been resolved
public getExpressionLanguageProviders() : array
 
  • return \ExpressionFunctionProviderInterface[]
public getExtension(string $name) : Symfony\Component\DependencyInjection\Extension\ExtensionInterface
 

Returns an extension by alias or namespace.

  • throws \LogicException if the extension is not registered
public getExtensionConfig(string $name) : array
 

Returns the configuration array for the given extension.

  • return array<string,mixed>[]
public getExtensions() : array
 

Returns all registered extensions.

  • return array<string,\ExtensionInterface>
public static getInitializedConditionals(?mixed $value) : array
 
  • return string[]
  • internal
public Symfony\Component\DependencyInjection\Container::getParameter(string $name) : UnitEnum|array|string|int|float|bool|?null
 

Gets a parameter.

  • throws \ParameterNotFoundException if the parameter is not defined
public Symfony\Component\DependencyInjection\Container::getParameterBag() : Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface
 

Gets the service container parameter bag.

public getReflectionClass(?string $class, bool $throw = true) : ?ReflectionClass
 

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

  • throws \ReflectionException when a parent class/interface/trait is not found and $throw is true
  • final
public getRemovedBindingIds() : array
 

Gets removed binding ids.

  • return array<int,bool>
  • internal
public getRemovedIds() : array
 

Gets removed service or alias ids.

  • return array<string,bool>
public getResources() : array
 

Returns an array of resources loaded to build this configuration.

  • return \ResourceInterface[]
public static getServiceConditionals(?mixed $value) : array
 
  • return string[]
  • internal
public getServiceIds() : array
public has(string $id) : bool
public hasAlias(string $id) : bool
public hasDefinition(string $id) : bool
 

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

public hasExtension(string $name) : bool
 

Checks if we have an extension.

public static hash(?mixed $value) : string
 

Computes a reasonably unique hash of a serializable value.

public Symfony\Component\DependencyInjection\Container::hasParameter(string $name) : bool
public Symfony\Component\DependencyInjection\Container::initialized(string $id) : bool
 

Returns true if the given service has actually been initialized.

public Symfony\Component\DependencyInjection\Container::isCompiled() : bool
 

Returns true if the container is compiled.

public isTrackingResources() : bool
 

Checks if resources are tracked.

public loadFromExtension(string $extension, ?array $values = NULL) : static
 

Loads the configuration for an extension.

  • param string $extension The extension alias or namespace
  • param array<string,mixed>|null $values An array of values that customizes the extension
  • return $this
  • throws \BadMethodCallException When this ContainerBuilder is compiled
  • throws \LogicException if the extension is not registered
public log(Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface $pass, string $message) : void
 
  • final
public merge(self $container) : void
 

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

  • throws \BadMethodCallException When this ContainerBuilder is compiled
public parameterCannotBeEmpty(string $name, string $message) : void
public prependExtensionConfig(string $name, array $config) : void
 

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

  • param array<string,mixed> $config
public register(string $id, ?string $class = NULL) : Symfony\Component\DependencyInjection\Definition
 

Registers a service definition.

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

public registerAliasForArgument(string $id, string $type, ?string $name = NULL) : Symfony\Component\DependencyInjection\Alias
 

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

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.

public registerAttributeForAutoconfiguration(string $attributeClass, callable $configurator) : void
 

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

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.

  • template
  • param class-string<\T> $attributeClass
  • param callable $configurator
public registerChild(string $id, string $parent) : Symfony\Component\DependencyInjection\ChildDefinition
 

This method provides a fluid interface for easily registering a child service definition of the given parent service.

public registerExtension(Symfony\Component\DependencyInjection\Extension\ExtensionInterface $extension) : void
public registerForAutoconfiguration(string $interface) : Symfony\Component\DependencyInjection\ChildDefinition
 

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

public removeAlias(string $alias) : void
public removeBindings(string $id) : void
 

Removes bindings for a service.

  • internal
public removeDefinition(string $id) : void
 

Removes a service definition.

public Symfony\Component\DependencyInjection\Container::reset() : void
public resolveEnvPlaceholders(?mixed $value, string|bool|?null $format = NULL, ?array $usedEnvs = NULL) : ?mixed
 

Resolves env parameter placeholders in a string or an array.

  • param string|true|null $format 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
  • param array $usedEnvs Env vars found while resolving are added to this array
  • return mixed The value with env parameters resolved if a string or an array is passed
public resolveServices(?mixed $value) : ?mixed
 

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

  • return mixed The same value with all service references replaced by the real service instances and all expressions evaluated
public set(string $id, ?object $service) : void
 

Sets a service.

  • throws \BadMethodCallException When this ContainerBuilder is compiled
public setAlias(string $alias, Symfony\Component\DependencyInjection\Alias|string $id) : Symfony\Component\DependencyInjection\Alias
 

Sets an alias for an existing service.

  • throws \InvalidArgumentException if the id is not a string or an Alias
  • throws \InvalidArgumentException if the alias is for itself
public setAliases(array $aliases) : void
 

Sets the service aliases.

  • param array<string,string|\Alias> $aliases
public setDefinition(string $id, Symfony\Component\DependencyInjection\Definition $definition) : Symfony\Component\DependencyInjection\Definition
 

Sets a service definition.

  • throws \BadMethodCallException When this ContainerBuilder is compiled
public setDefinitions(array $definitions) : void
 

Sets the service definitions.

  • param array<string,\Definition> $definitions
public Symfony\Component\DependencyInjection\Container::setParameter(string $name, UnitEnum|array|string|int|float|bool|?null $value) : void
public setProxyInstantiator(Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface $proxyInstantiator) : void
 

Sets the instantiator to be used when fetching proxies.

public setResources(array $resources) : static
 

Sets the resources for this configuration.

  • param array<string,\ResourceInterface> $resources
  • return $this
public setResourceTracking(bool $track) : void
 

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.

public static Symfony\Component\DependencyInjection\Container::underscore(string $id) : string
 

A string to underscore.

public final static willBeAvailable(string $package, string $class, array $parentPackages) : bool
 

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

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.

  • throws \LogicException If dependencies have been installed with Composer 1
Properties
protected array Symfony\Component\DependencyInjection\Container::$aliases = []
protected array Symfony\Component\DependencyInjection\Container::$factories = []
protected array Symfony\Component\DependencyInjection\Container::$fileMap = []
protected array Symfony\Component\DependencyInjection\Container::$loading = []
protected array Symfony\Component\DependencyInjection\Container::$methodMap = []
protected Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface Symfony\Component\DependencyInjection\Container::$parameterBag
protected array Symfony\Component\DependencyInjection\Container::$privates = []
protected array Symfony\Component\DependencyInjection\Container::$resolving = []
protected array Symfony\Component\DependencyInjection\Container::$services = []
protected array Symfony\Component\DependencyInjection\Container::$syntheticIds = []
Methods
protected getEnv(string $name) : ?mixed
protected final Symfony\Component\DependencyInjection\Container::getService(string|false $registry, string $id, ?string $method, string|bool $load) : ?mixed
 
  • internal
protected Symfony\Component\DependencyInjection\Container::load(string $file) : ?mixed
 

Creates a service by requiring its factory file.

Constants
private Symfony\Component\DependencyInjection\ContainerBuilder::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
private array $aliasDefinitions = []
 
  • var array<string,\Alias>
private array $autoconfiguredAttributes = []
 
  • var array<string,callable>
private array $autoconfiguredInstanceof = []
 
  • var array<string,\ChildDefinition>
private array $classReflectors
 
  • var array<string,\ReflectionClass>
private Symfony\Component\DependencyInjection\Compiler\Compiler $compiler
private array $definitions = []
 
  • var array<string,\Definition>
private array $envCounters = []
 
  • var int[] a map of env vars to their resolution counter
private array $envPlaceholders = []
 
  • var string[][] a map of env var names to their placeholders
private Symfony\Component\DependencyInjection\ExpressionLanguage $expressionLanguage
private array $expressionLanguageProviders = []
 
  • var \ExpressionFunctionProviderInterface[]
private array $extensionConfigs = []
 
  • var array<string,array<string,mixed>[]>
private array $extensions = []
 
  • var array<string,\ExtensionInterface>
private array $extensionsByNs = []
 
  • var array<string,\ExtensionInterface>
private array $pathsInVendor = []
 
  • var array<string,bool> whether a path is in a vendor directory
private Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface $proxyInstantiator
private array $removedBindingIds = []
 
  • var array<int,bool>
private array $removedIds = []
 
  • var array<string,bool>
private array $resources = []
 
  • var array<string,\ResourceInterface>
private bool $trackResources
private array $usedTags = []
 
  • var string[] with tag names used by findTaggedServiceIds
private array $vendors
 
  • var string[] the list of vendor directories
Methods
private callMethod(object $service, array $call, array $inlineServices) : ?mixed
private createService(Symfony\Component\DependencyInjection\Definition $definition, array $inlineServices, bool $isConstructorArgument = false, ?string $id = NULL, object|bool $tryProxy = true) : ?mixed
 

Creates a service for a service definition.

  • 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
private doGet(string $id, int $invalidBehavior = 1Symfony\Component\DependencyInjection\ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, ?array $inlineServices = NULL, bool $isConstructorArgument = false) : ?mixed
private doResolveServices(?mixed $value, array $inlineServices = [], bool $isConstructorArgument = false) : ?mixed
private getExpressionLanguage() : Symfony\Component\DependencyInjection\ExpressionLanguage
private inVendors(string $path) : bool
private shareService(Symfony\Component\DependencyInjection\Definition $definition, ?mixed $service, ?string $id, array $inlineServices) : void
Methods
public static Symfony\Component\DependencyInjection\Container::camelize(string $id) : string
 

Camelizes a string.

public static getInitializedConditionals(?mixed $value) : array
 
  • return string[]
  • internal
public static getServiceConditionals(?mixed $value) : array
 
  • return string[]
  • internal
public static hash(?mixed $value) : string
 

Computes a reasonably unique hash of a serializable value.

public static Symfony\Component\DependencyInjection\Container::underscore(string $id) : string
 

A string to underscore.

public final static willBeAvailable(string $package, string $class, array $parentPackages) : bool
 

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

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.

  • throws \LogicException If dependencies have been installed with Composer 1
© 2025 Bruce Wells
Search Namespaces \ Classes
Configuration