ContainerBuilder.php 4.56 KB
Newer Older
1
<?php
2
// @codingStandardsIgnoreFile
3 4 5

namespace Drupal\Core\DependencyInjection;

6 7
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
8 9
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
10
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
11 12

/**
13 14 15
 * Drupal's dependency injection container builder.
 *
 * @todo Submit upstream patches to Symfony to not require these overrides.
16 17
 *
 * @ingroup container
18
 */
19
class ContainerBuilder extends SymfonyContainerBuilder {
20

21 22 23 24 25
  /**
   * @var \Doctrine\Instantiator\InstantiatorInterface|null
   */
  private $proxyInstantiator;

26
  /**
27
   * {@inheritdoc}
28
   */
29 30
  public function __construct(ParameterBagInterface $parameterBag = NULL) {
    parent::__construct($parameterBag);
31
    $this->setResourceTracking(FALSE);
32
  }
33

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
  /**
   * Retrieves the currently set proxy instantiator or instantiates one.
   *
   * @return InstantiatorInterface
   */
  private function getProxyInstantiator()
  {
    if (!$this->proxyInstantiator) {
      $this->proxyInstantiator = new RealServiceInstantiator();
    }

    return $this->proxyInstantiator;
  }

  /**
49 50 51 52
   * A 1to1 copy of parent::shareService.
   *
   * @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
   *   3.4 this is not a 1to1 copy.
53
   */
54
  protected function shareService(Definition $definition, $service, $id, array &$inlineServices)
55
  {
56
    if ($definition->isShared()) {
57 58 59 60
      $this->services[$lowerId = strtolower($id)] = $service;
    }
  }

61 62 63 64 65 66 67 68 69 70 71
  /**
   * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
   *
   * Drupal's container builder can be used at runtime after compilation, so we
   * override Symfony's ContainerBuilder's restriction on setting services in a
   * frozen builder.
   *
   * @todo Restrict this to synthetic services only. Ideally, the upstream
   *   ContainerBuilder class should be fixed to allow setting synthetic
   *   services in a frozen builder.
   */
72
  public function set($id, $service) {
73 74 75
    if (strtolower($id) !== $id) {
      throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
    }
76
    SymfonyContainer::set($id, $service);
77

78 79 80 81
    // Ensure that the _serviceId property is set on synthetic services as well.
    if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
      $this->services[$id]->_serviceId = $id;
    }
82 83
  }

84 85 86 87 88 89 90
  /**
   * {@inheritdoc}
   */
  public function register($id, $class = null) {
    if (strtolower($id) !== $id) {
      throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
    }
91
    return parent::register($id, $class);
92 93 94 95 96 97 98 99 100 101
  }

  /**
   * {@inheritdoc}
   */
  public function setAlias($alias, $id) {
    $alias = parent::setAlias($alias, $id);
    // As of Symfony 3.4 all aliases are private by default.
    $alias->setPublic(TRUE);
    return $alias;
102 103
  }

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
  /**
   * {@inheritdoc}
   */
  public function setDefinition($id, Definition $definition) {
    $definition = parent::setDefinition($id, $definition);
    // As of Symfony 3.4 all definitions are private by default.
    // \Symfony\Component\DependencyInjection\Compiler\ResolvePrivatesPassOnly
    // removes services marked as private from the container even if they are
    // also marked as public. Drupal requires services that are public to
    // remain in the container and not be removed.
    if ($definition->isPublic()) {
      $definition->setPrivate(FALSE);
    }
    return $definition;
  }

120 121 122 123 124 125 126 127 128 129
  /**
   * {@inheritdoc}
   */
  public function setParameter($name, $value) {
    if (strtolower($name) !== $name) {
      throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
    }
    parent::setParameter($name, $value);
  }

130 131
  /**
   * A 1to1 copy of parent::callMethod.
132 133 134
   *
   * @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
   *   3.4 this is not a 1to1 copy.
135
   */
136
  protected function callMethod($service, $call, array &$inlineServices = array()) {
137 138 139 140 141 142 143 144 145
    $services = self::getServiceConditionals($call[1]);

    foreach ($services as $s) {
      if (!$this->has($s)) {
        return;
      }
    }

    call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
katbailey's avatar
katbailey committed
146 147
  }

148 149 150 151
  /**
   * {@inheritdoc}
   */
  public function __sleep() {
152
    assert(FALSE, 'The container was serialized.');
153 154 155
    return array_keys(get_object_vars($this));
  }

156
}