ContainerBuilder (Component\DependencyInjection)
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) 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. | ||
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. | |
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. | |
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. | |
addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider) | ||
ExpressionFunctionProviderInterface[] | getExpressionLanguageProviders() | |
static array | getServiceConditionals(mixed $value) Returns the Service Conditionals. |
Details
public __construct(ParameterBagInterface $parameterBag = null)
Constructor.
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:
- The extension configurations are merged;
- Parameter values are resolved;
- The parameter bag is frozen;
- Extension loading is disabled.
public bool isFrozen()
Returns true if the container parameter bag are frozen.
public ParameterBagInterface getParameterBag()
Gets the service container parameter bag.
public mixed getParameter(string $name)
Gets a parameter.
public bool hasParameter(string $name)
Checks if a parameter exists.
public setParameter(string $name, mixed $value)
Sets a parameter.
public set(string $id, object $service)
Sets a service.
public bool has(string $id)
Returns true if the given service is defined.
public object get(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
Gets a service.
public bool initialized(string $id)
Returns true if the given service has actually been initialized.
public 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.
public array getServiceIds()
Gets all service ids.
static public string camelize(string $id)
Camelizes a string.
static public string underscore(string $id)
A string to underscore.
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.
public bool isTrackingResources()
Checks if resources are tracked.
public setProxyInstantiator(InstantiatorInterface $proxyInstantiator)
Sets the instantiator to be used when fetching proxies.
public registerExtension(ExtensionInterface $extension)
Registers an extension.
public ExtensionInterface getExtension(string $name)
Returns an extension by alias or namespace.
public ExtensionInterface[] getExtensions()
Returns all registered extensions.
public bool hasExtension(string $name)
Checks if we have an extension.
public ResourceInterface[] getResources()
Returns an array of resources loaded to build this configuration.
public ContainerBuilder addResource(ResourceInterface $resource)
Adds a resource for this configuration.
public ContainerBuilder setResources(array $resources)
Sets the resources for this configuration.
public ContainerBuilder addObjectResource(object $object)
Adds the object class hierarchy as resources.
public ContainerBuilder addClassResource(ReflectionClass $class)
Adds the given class hierarchy as resources.
public ContainerBuilder loadFromExtension(string $extension, array $values = array())
Loads the configuration for an extension.
public ContainerBuilder addCompilerPass(CompilerPassInterface $pass, string $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
Adds a compiler pass.
public PassConfig getCompilerPassConfig()
Returns the compiler pass config which can then be modified.
public Compiler getCompiler()
Returns the compiler.
public removeDefinition(string $id)
Removes a service definition.
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.
public array getExtensionConfig(string $name)
Returns the configuration array for the given extension.
public prependExtensionConfig(string $name, array $config)
Prepends a config array to the configs of the given extension.
public addAliases(array $aliases)
Adds the service aliases.
public setAliases(array $aliases)
Sets the service aliases.
public setAlias(string $alias, string|Alias $id)
Sets an alias for an existing service.
public removeAlias(string $alias)
Removes an alias.
public bool hasAlias(string $id)
Returns true if an alias exists under the given identifier.
public Alias[] getAliases()
Gets all defined aliases.
public Alias getAlias(string $id)
Gets an alias.
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.
public addDefinitions(array $definitions)
Adds the service definitions.
public setDefinitions(array $definitions)
Sets the service definitions.
public Definition[] getDefinitions()
Gets all service definitions.
public Definition setDefinition(string $id, Definition $definition)
Sets a service definition.
public bool hasDefinition(string $id)
Returns true if a service definition exists under the given identifier.
public Definition getDefinition(string $id)
Gets a service definition.
public Definition findDefinition(string $id)
Gets a service definition by id or alias.
The method "unaliases" recursively to return a Definition instance.
public mixed resolveServices(mixed $value)
Replaces service references by the real service instance and evaluates expressions.
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']; } }
public array findTags()
Returns all tags the defined services use.
public string[] findUnusedTags()
Returns all tags not queried by findTaggedServiceIds.
public addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
public ExpressionFunctionProviderInterface[] getExpressionLanguageProviders()
static public array getServiceConditionals(mixed $value)
Returns the Service Conditionals.
© 2004–2016 Fabien Potencier
Licensed under the MIT License.
http://api.symfony.com/3.0/Symfony/Component/DependencyInjection/ContainerBuilder.html