4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection;
14 use Symfony\Component\DependencyInjection\Compiler\Compiler;
15 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
16 use Symfony\Component\DependencyInjection\Compiler\PassConfig;
17 use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
18 use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
19 use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
20 use Symfony\Component\DependencyInjection\Exception\LogicException;
21 use Symfony\Component\DependencyInjection\Exception\RuntimeException;
22 use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
23 use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
24 use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
25 use Symfony\Component\Config\Resource\FileResource;
26 use Symfony\Component\Config\Resource\ResourceInterface;
27 use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\InstantiatorInterface;
28 use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
29 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
30 use Symfony\Component\ExpressionLanguage\Expression;
31 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
34 * ContainerBuilder is a DI container that provides an API to easily describe services.
36 * @author Fabien Potencier <fabien@symfony.com>
38 class ContainerBuilder extends Container implements TaggedContainerInterface
41 * @var ExtensionInterface[]
43 private $extensions = array();
46 * @var ExtensionInterface[]
48 private $extensionsByNs = array();
53 private $definitions = array();
58 private $obsoleteDefinitions = array();
63 private $aliasDefinitions = array();
66 * @var ResourceInterface[]
68 private $resources = array();
70 private $extensionConfigs = array();
77 private $trackResources;
80 * @var InstantiatorInterface|null
82 private $proxyInstantiator;
85 * @var ExpressionLanguage|null
87 private $expressionLanguage;
90 * @var ExpressionFunctionProviderInterface[]
92 private $expressionLanguageProviders = array();
94 public function __construct(ParameterBagInterface $parameterBag = null)
96 parent::__construct($parameterBag);
98 $this->trackResources = interface_exists('Symfony\Component\Config\Resource\ResourceInterface');
102 * @var string[] with tag names used by findTaggedServiceIds
104 private $usedTags = array();
107 * Sets the track resources flag.
109 * If you are not using the loaders and therefore don't want
110 * to depend on the Config component, set this flag to false.
112 * @param bool $track true if you want to track resources, false otherwise
114 public function setResourceTracking($track)
116 $this->trackResources = (bool) $track;
120 * Checks if resources are tracked.
122 * @return bool true if resources are tracked, false otherwise
124 public function isTrackingResources()
126 return $this->trackResources;
130 * Sets the instantiator to be used when fetching proxies.
132 * @param InstantiatorInterface $proxyInstantiator
134 public function setProxyInstantiator(InstantiatorInterface $proxyInstantiator)
136 $this->proxyInstantiator = $proxyInstantiator;
140 * Registers an extension.
142 * @param ExtensionInterface $extension An extension instance
144 public function registerExtension(ExtensionInterface $extension)
146 $this->extensions[$extension->getAlias()] = $extension;
148 if (false !== $extension->getNamespace()) {
149 $this->extensionsByNs[$extension->getNamespace()] = $extension;
154 * Returns an extension by alias or namespace.
156 * @param string $name An alias or a namespace
158 * @return ExtensionInterface An extension instance
160 * @throws LogicException if the extension is not registered
162 public function getExtension($name)
164 if (isset($this->extensions[$name])) {
165 return $this->extensions[$name];
168 if (isset($this->extensionsByNs[$name])) {
169 return $this->extensionsByNs[$name];
172 throw new LogicException(sprintf('Container extension "%s" is not registered', $name));
176 * Returns all registered extensions.
178 * @return ExtensionInterface[] An array of ExtensionInterface
180 public function getExtensions()
182 return $this->extensions;
186 * Checks if we have an extension.
188 * @param string $name The name of the extension
190 * @return bool If the extension exists
192 public function hasExtension($name)
194 return isset($this->extensions[$name]) || isset($this->extensionsByNs[$name]);
198 * Returns an array of resources loaded to build this configuration.
200 * @return ResourceInterface[] An array of resources
202 public function getResources()
204 return array_unique($this->resources);
208 * Adds a resource for this configuration.
210 * @param ResourceInterface $resource A resource instance
214 public function addResource(ResourceInterface $resource)
216 if (!$this->trackResources) {
220 $this->resources[] = $resource;
226 * Sets the resources for this configuration.
228 * @param ResourceInterface[] $resources An array of resources
232 public function setResources(array $resources)
234 if (!$this->trackResources) {
238 $this->resources = $resources;
244 * Adds the object class hierarchy as resources.
246 * @param object $object An object instance
250 public function addObjectResource($object)
252 if ($this->trackResources) {
253 $this->addClassResource(new \ReflectionClass($object));
260 * Adds the given class hierarchy as resources.
262 * @param \ReflectionClass $class
266 public function addClassResource(\ReflectionClass $class)
268 if (!$this->trackResources) {
273 if (is_file($class->getFileName())) {
274 $this->addResource(new FileResource($class->getFileName()));
276 } while ($class = $class->getParentClass());
282 * Loads the configuration for an extension.
284 * @param string $extension The extension alias or namespace
285 * @param array $values An array of values that customizes the extension
289 * @throws BadMethodCallException When this ContainerBuilder is frozen
290 * @throws \LogicException if the container is frozen
292 public function loadFromExtension($extension, array $values = array())
294 if ($this->isFrozen()) {
295 throw new BadMethodCallException('Cannot load from an extension on a frozen container.');
298 $namespace = $this->getExtension($extension)->getAlias();
300 $this->extensionConfigs[$namespace][] = $values;
306 * Adds a compiler pass.
308 * @param CompilerPassInterface $pass A compiler pass
309 * @param string $type The type of compiler pass
313 public function addCompilerPass(CompilerPassInterface $pass, $type = PassConfig::TYPE_BEFORE_OPTIMIZATION)
315 $this->getCompiler()->addPass($pass, $type);
317 $this->addObjectResource($pass);
323 * Returns the compiler pass config which can then be modified.
325 * @return PassConfig The compiler pass config
327 public function getCompilerPassConfig()
329 return $this->getCompiler()->getPassConfig();
333 * Returns the compiler.
335 * @return Compiler The compiler
337 public function getCompiler()
339 if (null === $this->compiler) {
340 $this->compiler = new Compiler();
343 return $this->compiler;
347 * Returns all Scopes.
349 * @return array An array of scopes
351 * @deprecated since version 2.8, to be removed in 3.0.
353 public function getScopes($triggerDeprecationError = true)
355 if ($triggerDeprecationError) {
356 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
359 return $this->scopes;
363 * Returns all Scope children.
365 * @return array An array of scope children
367 * @deprecated since version 2.8, to be removed in 3.0.
369 public function getScopeChildren($triggerDeprecationError = true)
371 if ($triggerDeprecationError) {
372 @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
375 return $this->scopeChildren;
381 * Note: The $scope parameter is deprecated since version 2.8 and will be removed in 3.0.
383 * @param string $id The service identifier
384 * @param object $service The service instance
385 * @param string $scope The scope
387 * @throws BadMethodCallException When this ContainerBuilder is frozen
389 public function set($id, $service, $scope = self::SCOPE_CONTAINER)
391 $id = strtolower($id);
392 $set = isset($this->definitions[$id]);
394 if ($this->isFrozen() && ($set || isset($this->obsoleteDefinitions[$id])) && !$this->{$set ? 'definitions' : 'obsoleteDefinitions'}[$id]->isSynthetic()) {
395 // setting a synthetic service on a frozen container is alright
396 throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
400 $this->obsoleteDefinitions[$id] = $this->definitions[$id];
403 unset($this->definitions[$id], $this->aliasDefinitions[$id]);
405 parent::set($id, $service, $scope);
407 if (isset($this->obsoleteDefinitions[$id]) && $this->obsoleteDefinitions[$id]->isSynchronized(false)) {
408 $this->synchronize($id);
413 * Removes a service definition.
415 * @param string $id The service identifier
417 public function removeDefinition($id)
419 unset($this->definitions[strtolower($id)]);
423 * Returns true if the given service is defined.
425 * @param string $id The service identifier
427 * @return bool true if the service is defined, false otherwise
429 public function has($id)
431 $id = strtolower($id);
433 return isset($this->definitions[$id]) || isset($this->aliasDefinitions[$id]) || parent::has($id);
439 * @param string $id The service identifier
440 * @param int $invalidBehavior The behavior when the service does not exist
442 * @return object The associated service
444 * @throws InvalidArgumentException when no definitions are available
445 * @throws ServiceCircularReferenceException When a circular reference is detected
446 * @throws ServiceNotFoundException When the service is not defined
451 public function get($id, $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE)
453 $id = strtolower($id);
455 if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
459 if (!array_key_exists($id, $this->definitions) && isset($this->aliasDefinitions[$id])) {
460 return $this->get((string) $this->aliasDefinitions[$id], $invalidBehavior);
464 $definition = $this->getDefinition($id);
465 } catch (ServiceNotFoundException $e) {
466 if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
473 $this->loading[$id] = true;
476 $service = $this->createService($definition, $id);
477 } catch (\Exception $e) {
478 unset($this->loading[$id]);
480 if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
485 } catch (\Throwable $e) {
486 unset($this->loading[$id]);
491 unset($this->loading[$id]);
497 * Merges a ContainerBuilder with the current ContainerBuilder configuration.
499 * Service definitions overrides the current defined ones.
501 * But for parameters, they are overridden by the current ones. It allows
502 * the parameters passed to the container constructor to have precedence
503 * over the loaded ones.
505 * $container = new ContainerBuilder(array('foo' => 'bar'));
506 * $loader = new LoaderXXX($container);
507 * $loader->load('resource_name');
508 * $container->register('foo', new stdClass());
510 * In the above example, even if the loaded resource defines a foo
511 * parameter, the value will still be 'bar' as defined in the ContainerBuilder
514 * @param ContainerBuilder $container The ContainerBuilder instance to merge
516 * @throws BadMethodCallException When this ContainerBuilder is frozen
518 public function merge(ContainerBuilder $container)
520 if ($this->isFrozen()) {
521 throw new BadMethodCallException('Cannot merge on a frozen container.');
524 $this->addDefinitions($container->getDefinitions());
525 $this->addAliases($container->getAliases());
526 $this->getParameterBag()->add($container->getParameterBag()->all());
528 if ($this->trackResources) {
529 foreach ($container->getResources() as $resource) {
530 $this->addResource($resource);
534 foreach ($this->extensions as $name => $extension) {
535 if (!isset($this->extensionConfigs[$name])) {
536 $this->extensionConfigs[$name] = array();
539 $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name));
544 * Returns the configuration array for the given extension.
546 * @param string $name The name of the extension
548 * @return array An array of configuration
550 public function getExtensionConfig($name)
552 if (!isset($this->extensionConfigs[$name])) {
553 $this->extensionConfigs[$name] = array();
556 return $this->extensionConfigs[$name];
560 * Prepends a config array to the configs of the given extension.
562 * @param string $name The name of the extension
563 * @param array $config The config to set
565 public function prependExtensionConfig($name, array $config)
567 if (!isset($this->extensionConfigs[$name])) {
568 $this->extensionConfigs[$name] = array();
571 array_unshift($this->extensionConfigs[$name], $config);
575 * Compiles the container.
577 * This method passes the container to compiler
578 * passes whose job is to manipulate and optimize
581 * The main compiler passes roughly do four things:
583 * * The extension configurations are merged;
584 * * Parameter values are resolved;
585 * * The parameter bag is frozen;
586 * * Extension loading is disabled.
588 public function compile()
590 $compiler = $this->getCompiler();
592 if ($this->trackResources) {
593 foreach ($compiler->getPassConfig()->getPasses() as $pass) {
594 $this->addObjectResource($pass);
598 $compiler->compile($this);
600 if ($this->trackResources) {
601 foreach ($this->definitions as $definition) {
602 if ($definition->isLazy() && ($class = $definition->getClass()) && class_exists($class)) {
603 $this->addClassResource(new \ReflectionClass($class));
608 $this->extensionConfigs = array();
614 * Gets all service ids.
616 * @return array An array of all defined service ids
618 public function getServiceIds()
620 return array_unique(array_merge(array_keys($this->getDefinitions()), array_keys($this->aliasDefinitions), parent::getServiceIds()));
624 * Adds the service aliases.
626 * @param array $aliases An array of aliases
628 public function addAliases(array $aliases)
630 foreach ($aliases as $alias => $id) {
631 $this->setAlias($alias, $id);
636 * Sets the service aliases.
638 * @param array $aliases An array of aliases
640 public function setAliases(array $aliases)
642 $this->aliasDefinitions = array();
643 $this->addAliases($aliases);
647 * Sets an alias for an existing service.
649 * @param string $alias The alias to create
650 * @param string|Alias $id The service to alias
652 * @throws InvalidArgumentException if the id is not a string or an Alias
653 * @throws InvalidArgumentException if the alias is for itself
655 public function setAlias($alias, $id)
657 $alias = strtolower($alias);
659 if (is_string($id)) {
660 $id = new Alias($id);
661 } elseif (!$id instanceof Alias) {
662 throw new InvalidArgumentException('$id must be a string, or an Alias object.');
665 if ($alias === (string) $id) {
666 throw new InvalidArgumentException(sprintf('An alias can not reference itself, got a circular reference on "%s".', $alias));
669 unset($this->definitions[$alias]);
671 $this->aliasDefinitions[$alias] = $id;
677 * @param string $alias The alias to remove
679 public function removeAlias($alias)
681 unset($this->aliasDefinitions[strtolower($alias)]);
685 * Returns true if an alias exists under the given identifier.
687 * @param string $id The service identifier
689 * @return bool true if the alias exists, false otherwise
691 public function hasAlias($id)
693 return isset($this->aliasDefinitions[strtolower($id)]);
697 * Gets all defined aliases.
699 * @return Alias[] An array of aliases
701 public function getAliases()
703 return $this->aliasDefinitions;
709 * @param string $id The service identifier
711 * @return Alias An Alias instance
713 * @throws InvalidArgumentException if the alias does not exist
715 public function getAlias($id)
717 $id = strtolower($id);
719 if (!isset($this->aliasDefinitions[$id])) {
720 throw new InvalidArgumentException(sprintf('The service alias "%s" does not exist.', $id));
723 return $this->aliasDefinitions[$id];
727 * Registers a service definition.
729 * This methods allows for simple registration of service definition
730 * with a fluid interface.
732 * @param string $id The service identifier
733 * @param string $class The service class
735 * @return Definition A Definition instance
737 public function register($id, $class = null)
739 return $this->setDefinition($id, new Definition($class));
743 * Adds the service definitions.
745 * @param Definition[] $definitions An array of service definitions
747 public function addDefinitions(array $definitions)
749 foreach ($definitions as $id => $definition) {
750 $this->setDefinition($id, $definition);
755 * Sets the service definitions.
757 * @param Definition[] $definitions An array of service definitions
759 public function setDefinitions(array $definitions)
761 $this->definitions = array();
762 $this->addDefinitions($definitions);
766 * Gets all service definitions.
768 * @return Definition[] An array of Definition instances
770 public function getDefinitions()
772 return $this->definitions;
776 * Sets a service definition.
778 * @param string $id The service identifier
779 * @param Definition $definition A Definition instance
781 * @return Definition the service definition
783 * @throws BadMethodCallException When this ContainerBuilder is frozen
785 public function setDefinition($id, Definition $definition)
787 if ($this->isFrozen()) {
788 throw new BadMethodCallException('Adding definition to a frozen container is not allowed');
791 $id = strtolower($id);
793 unset($this->aliasDefinitions[$id]);
795 return $this->definitions[$id] = $definition;
799 * Returns true if a service definition exists under the given identifier.
801 * @param string $id The service identifier
803 * @return bool true if the service definition exists, false otherwise
805 public function hasDefinition($id)
807 return array_key_exists(strtolower($id), $this->definitions);
811 * Gets a service definition.
813 * @param string $id The service identifier
815 * @return Definition A Definition instance
817 * @throws ServiceNotFoundException if the service definition does not exist
819 public function getDefinition($id)
821 $id = strtolower($id);
823 if (!array_key_exists($id, $this->definitions)) {
824 throw new ServiceNotFoundException($id);
827 return $this->definitions[$id];
831 * Gets a service definition by id or alias.
833 * The method "unaliases" recursively to return a Definition instance.
835 * @param string $id The service identifier or alias
837 * @return Definition A Definition instance
839 * @throws ServiceNotFoundException if the service definition does not exist
841 public function findDefinition($id)
843 $id = strtolower($id);
845 while (isset($this->aliasDefinitions[$id])) {
846 $id = (string) $this->aliasDefinitions[$id];
849 return $this->getDefinition($id);
853 * Creates a service for a service definition.
855 * @param Definition $definition A service definition instance
856 * @param string $id The service identifier
857 * @param bool $tryProxy Whether to try proxying the service with a lazy proxy
859 * @return object The service described by the service definition
861 * @throws RuntimeException When the scope is inactive
862 * @throws RuntimeException When the factory definition is incomplete
863 * @throws RuntimeException When the service is a synthetic service
864 * @throws InvalidArgumentException When configure callable is not callable
866 * @internal this method is public because of PHP 5.3 limitations, do not use it explicitly in your code
868 public function createService(Definition $definition, $id, $tryProxy = true)
870 if ($definition instanceof DefinitionDecorator) {
871 throw new RuntimeException(sprintf('Constructing service "%s" from a parent definition is not supported at build time.', $id));
874 if ($definition->isSynthetic()) {
875 throw new RuntimeException(sprintf('You have requested a synthetic service ("%s"). The DIC does not know how to construct this service.', $id));
878 if ($definition->isDeprecated()) {
879 @trigger_error($definition->getDeprecationMessage($id), E_USER_DEPRECATED);
882 if ($tryProxy && $definition->isLazy()) {
886 ->getProxyInstantiator()
890 $id, function () use ($definition, $id, $container) {
891 return $container->createService($definition, $id, false);
894 $this->shareService($definition, $proxy, $id);
899 $parameterBag = $this->getParameterBag();
901 if (null !== $definition->getFile()) {
902 require_once $parameterBag->resolveValue($definition->getFile());
905 $arguments = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getArguments())));
907 if (null !== $factory = $definition->getFactory()) {
908 if (is_array($factory)) {
909 $factory = array($this->resolveServices($parameterBag->resolveValue($factory[0])), $factory[1]);
910 } elseif (!is_string($factory)) {
911 throw new RuntimeException(sprintf('Cannot create service "%s" because of invalid factory', $id));
914 $service = call_user_func_array($factory, $arguments);
916 if (!$definition->isDeprecated() && is_array($factory) && is_string($factory[0])) {
917 $r = new \ReflectionClass($factory[0]);
919 if (0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
920 @trigger_error(sprintf('The "%s" service relies on the deprecated "%s" factory class. It should either be deprecated or its factory upgraded.', $id, $r->name), E_USER_DEPRECATED);
923 } elseif (null !== $definition->getFactoryMethod(false)) {
924 if (null !== $definition->getFactoryClass(false)) {
925 $factory = $parameterBag->resolveValue($definition->getFactoryClass(false));
926 } elseif (null !== $definition->getFactoryService(false)) {
927 $factory = $this->get($parameterBag->resolveValue($definition->getFactoryService(false)));
929 throw new RuntimeException(sprintf('Cannot create service "%s" from factory method without a factory service or factory class.', $id));
932 $service = call_user_func_array(array($factory, $definition->getFactoryMethod(false)), $arguments);
934 $r = new \ReflectionClass($parameterBag->resolveValue($definition->getClass()));
936 $service = null === $r->getConstructor() ? $r->newInstance() : $r->newInstanceArgs($arguments);
938 if (!$definition->isDeprecated() && 0 < strpos($r->getDocComment(), "\n * @deprecated ")) {
939 @trigger_error(sprintf('The "%s" service relies on the deprecated "%s" class. It should either be deprecated or its implementation upgraded.', $id, $r->name), E_USER_DEPRECATED);
943 if ($tryProxy || !$definition->isLazy()) {
944 // share only if proxying failed, or if not a proxy
945 $this->shareService($definition, $service, $id);
948 $properties = $this->resolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($definition->getProperties())));
949 foreach ($properties as $name => $value) {
950 $service->$name = $value;
953 foreach ($definition->getMethodCalls() as $call) {
954 $this->callMethod($service, $call);
957 if ($callable = $definition->getConfigurator()) {
958 if (is_array($callable)) {
959 $callable[0] = $parameterBag->resolveValue($callable[0]);
961 if ($callable[0] instanceof Reference) {
962 $callable[0] = $this->get((string) $callable[0], $callable[0]->getInvalidBehavior());
963 } elseif ($callable[0] instanceof Definition) {
964 $callable[0] = $this->createService($callable[0], null);
968 if (!is_callable($callable)) {
969 throw new InvalidArgumentException(sprintf('The configure callable for class "%s" is not a callable.', get_class($service)));
972 call_user_func($callable, $service);
979 * Replaces service references by the real service instance and evaluates expressions.
981 * @param mixed $value A value
983 * @return mixed The same value with all service references replaced by
984 * the real service instances and all expressions evaluated
986 public function resolveServices($value)
988 if (is_array($value)) {
989 foreach ($value as $k => $v) {
990 $value[$k] = $this->resolveServices($v);
992 } elseif ($value instanceof Reference) {
993 $value = $this->get((string) $value, $value->getInvalidBehavior());
994 } elseif ($value instanceof Definition) {
995 $value = $this->createService($value, null);
996 } elseif ($value instanceof Expression) {
997 $value = $this->getExpressionLanguage()->evaluate($value, array('container' => $this));
1004 * Returns service ids for a given tag.
1008 * $container->register('foo')->addTag('my.tag', array('hello' => 'world'));
1010 * $serviceIds = $container->findTaggedServiceIds('my.tag');
1011 * foreach ($serviceIds as $serviceId => $tags) {
1012 * foreach ($tags as $tag) {
1013 * echo $tag['hello'];
1017 * @param string $name The tag name
1019 * @return array An array of tags with the tagged service as key, holding a list of attribute arrays
1021 public function findTaggedServiceIds($name)
1023 $this->usedTags[] = $name;
1025 foreach ($this->getDefinitions() as $id => $definition) {
1026 if ($definition->hasTag($name)) {
1027 $tags[$id] = $definition->getTag($name);
1035 * Returns all tags the defined services use.
1037 * @return array An array of tags
1039 public function findTags()
1042 foreach ($this->getDefinitions() as $id => $definition) {
1043 $tags = array_merge(array_keys($definition->getTags()), $tags);
1046 return array_unique($tags);
1050 * Returns all tags not queried by findTaggedServiceIds.
1052 * @return string[] An array of tags
1054 public function findUnusedTags()
1056 return array_values(array_diff($this->findTags(), $this->usedTags));
1059 public function addExpressionLanguageProvider(ExpressionFunctionProviderInterface $provider)
1061 $this->expressionLanguageProviders[] = $provider;
1065 * @return ExpressionFunctionProviderInterface[]
1067 public function getExpressionLanguageProviders()
1069 return $this->expressionLanguageProviders;
1073 * Returns the Service Conditionals.
1075 * @param mixed $value An array of conditionals to return
1077 * @return array An array of Service conditionals
1079 public static function getServiceConditionals($value)
1081 $services = array();
1083 if (is_array($value)) {
1084 foreach ($value as $v) {
1085 $services = array_unique(array_merge($services, self::getServiceConditionals($v)));
1087 } elseif ($value instanceof Reference && $value->getInvalidBehavior() === ContainerInterface::IGNORE_ON_INVALID_REFERENCE) {
1088 $services[] = (string) $value;
1095 * Retrieves the currently set proxy instantiator or instantiates one.
1097 * @return InstantiatorInterface
1099 private function getProxyInstantiator()
1101 if (!$this->proxyInstantiator) {
1102 $this->proxyInstantiator = new RealServiceInstantiator();
1105 return $this->proxyInstantiator;
1109 * Synchronizes a service change.
1111 * This method updates all services that depend on the given
1112 * service by calling all methods referencing it.
1114 * @param string $id A service id
1116 * @deprecated since version 2.7, will be removed in 3.0.
1118 private function synchronize($id)
1120 if ('request' !== $id) {
1121 @trigger_error('The '.__METHOD__.' method is deprecated in version 2.7 and will be removed in version 3.0.', E_USER_DEPRECATED);
1124 foreach ($this->definitions as $definitionId => $definition) {
1125 // only check initialized services
1126 if (!$this->initialized($definitionId)) {
1130 foreach ($definition->getMethodCalls() as $call) {
1131 foreach ($call[1] as $argument) {
1132 if ($argument instanceof Reference && $id == (string) $argument) {
1133 $this->callMethod($this->get($definitionId), $call);
1140 private function callMethod($service, $call)
1142 $services = self::getServiceConditionals($call[1]);
1144 foreach ($services as $s) {
1145 if (!$this->has($s)) {
1150 call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->unescapeValue($this->getParameterBag()->resolveValue($call[1]))));
1154 * Shares a given service in the container.
1156 * @param Definition $definition
1157 * @param mixed $service
1158 * @param string|null $id
1160 * @throws InactiveScopeException
1162 private function shareService(Definition $definition, $service, $id)
1164 if (null !== $id && $definition->isShared() && self::SCOPE_PROTOTYPE !== $scope = $definition->getScope(false)) {
1165 if (self::SCOPE_CONTAINER !== $scope && !isset($this->scopedServices[$scope])) {
1166 throw new InactiveScopeException($id, $scope);
1169 $this->services[$lowerId = strtolower($id)] = $service;
1171 if (self::SCOPE_CONTAINER !== $scope) {
1172 $this->scopedServices[$scope][$lowerId] = $service;
1177 private function getExpressionLanguage()
1179 if (null === $this->expressionLanguage) {
1180 if (!class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
1181 throw new RuntimeException('Unable to use expressions as the Symfony ExpressionLanguage component is not installed.');
1183 $this->expressionLanguage = new ExpressionLanguage(null, $this->expressionLanguageProviders);
1186 return $this->expressionLanguage;