ContainerBuilder
class ContainerBuilder extends Container implements TaggedContainerInterface
ContainerBuilder is a DI container that provides an API to easily describe services.
Properties
protected | $parameterBag | from Container | |
protected | $services | from Container | |
protected | $privates | from Container | |
protected | $fileMap | from Container | |
protected | $methodMap | from Container | |
protected | $factories | from Container | |
protected | $aliases | from Container | |
protected | $loading | from Container | |
protected | $resolving | from Container | |
protected | $syntheticIds | from Container |
Methods
Compiles the container.
Sets a service.
Returns true if the given service is defined.
Gets a service.
Returns true if the given service has actually been initialized.
Gets all service ids.
Gets removed service or alias ids.
Fetches a variable from the environment.
Sets the track resources flag.
Checks if resources are tracked.
Sets the instantiator to be used when fetching proxies.
Returns an extension by alias or namespace.
Returns all registered extensions.
Checks if we have an extension.
Returns an array of resources loaded to build this configuration.
Sets the resources for this configuration.
Adds the object class hierarchy as resources.
Retrieves the requested reflection class and registers it for resource tracking.
Checks whether the requested file or directory exists and registers the result for resource tracking.
Loads the configuration for an extension.
Adds a compiler pass.
Returns the compiler pass config which can then be modified.
Returns the compiler.
Removes a service definition.
Merges a ContainerBuilder with the current ContainerBuilder configuration.
Returns the configuration array for the given extension.
Prepends a config array to the configs of the given extension.
Adds the service aliases.
Sets the service aliases.
Removes an alias.
Returns true if an alias exists under the given identifier.
Gets all defined aliases.
Registers a service definition.
Registers an autowired service definition.
Adds the service definitions.
Sets the service definitions.
Gets all service definitions.
Returns true if a service definition exists under the given identifier.
Gets a service definition.
Gets a service definition by id or alias.
Replaces service references by the real service instance and evaluates expressions.
Returns service ids for a given tag.
Returns all tags the defined services use.
Returns all tags not queried by findTaggedServiceIds.
No description
Returns a ChildDefinition that will be used for autoconfiguring the interface/class.
Registers an autowiring alias that only binds to a specific argument name.
Returns an array of ChildDefinition[] keyed by interface.
Resolves env parameter placeholders in a string or an array.
Get statistics about env usage.
Returns the Service Conditionals.
Returns the initialized conditionals.
Gets removed binding ids.
Computes a reasonably unique hash of a value.
Details
compile(bool $resolveEnvPlaceholders = false)
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.
object
get(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
Gets a service.
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.
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.
setProxyInstantiator(InstantiatorInterface $proxyInstantiator)
Sets the instantiator to be used when fetching proxies.
ResourceInterface[]
getResources()
Returns an array of resources loaded to build this configuration.
ReflectionClass|null
getReflectionClass(string|null $class, bool $throw = true)
Retrieves the requested reflection class and registers it for resource tracking.
bool
fileExists(string $path, bool|string $trackContents = true)
Checks whether the requested file or directory exists and registers the result for resource tracking.
$this
loadFromExtension(string $extension, array $values = null)
Loads the configuration for an extension.
$this
addCompilerPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION, int $priority = 0)
Adds a compiler pass.
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(new ParameterBag(array('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.
prependExtensionConfig(string $name, array $config)
Prepends a config array to the configs of the given extension.
Definition
register($id, $class = null)
Registers a service definition.
This methods allows for simple registration of service definition with a fluid interface.
Definition
autowire(string $id, string|null $class = null)
Registers an autowired service definition.
This method implements a shortcut for using setDefinition() with an autowired definition.
bool
hasDefinition(string $id)
Returns true if a service definition exists under the given identifier.
Definition
findDefinition(string $id)
Gets a service definition by id or alias.
The method "unaliases" recursively to return a Definition instance.
mixed
resolveServices(mixed $value)
Replaces service references by the real service instance and evaluates expressions.
array
findTaggedServiceIds(string $name, bool $throwOnAbstract = false)
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'];
}
}
ChildDefinition
registerForAutoconfiguration(string $interface)
Returns a ChildDefinition that will be used for autoconfiguring the interface/class.
Alias
registerAliasForArgument(string $id, string $type, string $name = null)
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.
ChildDefinition[]
getAutoconfiguredInstanceof()
Returns an array of ChildDefinition[] keyed by interface.