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 $this->setResourceTracking(FALSE);
31 parent::__construct($parameterBag);
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;
51 protected function shareService(Definition $definition, $service, $id)
53 if ($definition->isShared()) {
54 $this->services[$lowerId = strtolower($id)] = $service;
59 * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
61 * Drupal's container builder can be used at runtime after compilation, so we
62 * override Symfony's ContainerBuilder's restriction on setting services in a
65 * @todo Restrict this to synthetic services only. Ideally, the upstream
66 * ContainerBuilder class should be fixed to allow setting synthetic
67 * services in a frozen builder.
69 public function set($id, $service) {
70 if (strtolower($id) !== $id) {
71 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
73 SymfonyContainer::set($id, $service);
75 // Ensure that the _serviceId property is set on synthetic services as well.
76 if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
77 $this->services[$id]->_serviceId = $id;
84 public function register($id, $class = null) {
85 if (strtolower($id) !== $id) {
86 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
88 return parent::register($id, $class);
94 public function setParameter($name, $value) {
95 if (strtolower($name) !== $name) {
96 throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
98 parent::setParameter($name, $value);
102 * A 1to1 copy of parent::callMethod.
104 protected function callMethod($service, $call) {
105 $services = self::getServiceConditionals($call[1]);
107 foreach ($services as $s) {
108 if (!$this->has($s)) {
113 call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
119 public function __sleep() {
120 assert(FALSE, 'The container was serialized.');
121 return array_keys(get_object_vars($this));