2 // @codingStandardsIgnoreFile
4 namespace Drupal\Core\DependencyInjection;
6 use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
7 use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
8 use Symfony\Component\DependencyInjection\Definition;
9 use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
10 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
13 * Drupal's dependency injection container builder.
15 * @todo Submit upstream patches to Symfony to not require these overrides.
19 class ContainerBuilder extends SymfonyContainerBuilder {
22 * @var \Doctrine\Instantiator\InstantiatorInterface|null
24 private $proxyInstantiator;
29 public function __construct(ParameterBagInterface $parameterBag = NULL) {
30 parent::__construct($parameterBag);
31 $this->setResourceTracking(FALSE);
35 * Retrieves the currently set proxy instantiator or instantiates one.
37 * @return InstantiatorInterface
39 private function getProxyInstantiator()
41 if (!$this->proxyInstantiator) {
42 $this->proxyInstantiator = new RealServiceInstantiator();
45 return $this->proxyInstantiator;
49 * A 1to1 copy of parent::shareService.
51 * @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
52 * 3.4 this is not a 1to1 copy.
54 protected function shareService(Definition $definition, $service, $id, array &$inlineServices)
56 if ($definition->isShared()) {
57 $this->services[$lowerId = strtolower($id)] = $service;
62 * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
64 * Drupal's container builder can be used at runtime after compilation, so we
65 * override Symfony's ContainerBuilder's restriction on setting services in a
68 * @todo Restrict this to synthetic services only. Ideally, the upstream
69 * ContainerBuilder class should be fixed to allow setting synthetic
70 * services in a frozen builder.
72 public function set($id, $service) {
73 if (strtolower($id) !== $id) {
74 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
76 SymfonyContainer::set($id, $service);
78 // Ensure that the _serviceId property is set on synthetic services as well.
79 if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
80 $this->services[$id]->_serviceId = $id;
87 public function register($id, $class = null) {
88 if (strtolower($id) !== $id) {
89 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
91 return parent::register($id, $class);
97 public function setAlias($alias, $id) {
98 $alias = parent::setAlias($alias, $id);
99 // As of Symfony 3.4 all aliases are private by default.
100 $alias->setPublic(TRUE);
107 public function setDefinition($id, Definition $definition) {
108 $definition = parent::setDefinition($id, $definition);
109 // As of Symfony 3.4 all definitions are private by default.
110 // \Symfony\Component\DependencyInjection\Compiler\ResolvePrivatesPassOnly
111 // removes services marked as private from the container even if they are
112 // also marked as public. Drupal requires services that are public to
113 // remain in the container and not be removed.
114 if ($definition->isPublic()) {
115 $definition->setPrivate(FALSE);
123 public function setParameter($name, $value) {
124 if (strtolower($name) !== $name) {
125 throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
127 parent::setParameter($name, $value);
131 * A 1to1 copy of parent::callMethod.
133 * @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
134 * 3.4 this is not a 1to1 copy.
136 protected function callMethod($service, $call, array &$inlineServices = array()) {
137 $services = self::getServiceConditionals($call[1]);
139 foreach ($services as $s) {
140 if (!$this->has($s)) {
145 call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
151 public function __sleep() {
152 assert(FALSE, 'The container was serialized.');
153 return array_keys(get_object_vars($this));