annotate core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 1fec387a4317
children
rev   line source
Chris@0 1 <?php
Chris@0 2 // @codingStandardsIgnoreFile
Chris@0 3
Chris@0 4 namespace Drupal\Core\DependencyInjection;
Chris@0 5
Chris@0 6 use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
Chris@0 7 use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
Chris@0 8 use Symfony\Component\DependencyInjection\Definition;
Chris@0 9 use Symfony\Component\DependencyInjection\LazyProxy\Instantiator\RealServiceInstantiator;
Chris@0 10 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
Chris@0 11
Chris@0 12 /**
Chris@0 13 * Drupal's dependency injection container builder.
Chris@0 14 *
Chris@0 15 * @todo Submit upstream patches to Symfony to not require these overrides.
Chris@0 16 *
Chris@0 17 * @ingroup container
Chris@0 18 */
Chris@0 19 class ContainerBuilder extends SymfonyContainerBuilder {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * @var \Doctrine\Instantiator\InstantiatorInterface|null
Chris@0 23 */
Chris@0 24 private $proxyInstantiator;
Chris@0 25
Chris@0 26 /**
Chris@0 27 * {@inheritdoc}
Chris@0 28 */
Chris@0 29 public function __construct(ParameterBagInterface $parameterBag = NULL) {
Chris@14 30 parent::__construct($parameterBag);
Chris@0 31 $this->setResourceTracking(FALSE);
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Retrieves the currently set proxy instantiator or instantiates one.
Chris@0 36 *
Chris@0 37 * @return InstantiatorInterface
Chris@0 38 */
Chris@0 39 private function getProxyInstantiator()
Chris@0 40 {
Chris@0 41 if (!$this->proxyInstantiator) {
Chris@0 42 $this->proxyInstantiator = new RealServiceInstantiator();
Chris@0 43 }
Chris@0 44
Chris@0 45 return $this->proxyInstantiator;
Chris@0 46 }
Chris@0 47
Chris@0 48 /**
Chris@14 49 * A 1to1 copy of parent::shareService.
Chris@14 50 *
Chris@14 51 * @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
Chris@14 52 * 3.4 this is not a 1to1 copy.
Chris@0 53 */
Chris@14 54 protected function shareService(Definition $definition, $service, $id, array &$inlineServices)
Chris@0 55 {
Chris@0 56 if ($definition->isShared()) {
Chris@0 57 $this->services[$lowerId = strtolower($id)] = $service;
Chris@0 58 }
Chris@0 59 }
Chris@0 60
Chris@0 61 /**
Chris@0 62 * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
Chris@0 63 *
Chris@0 64 * Drupal's container builder can be used at runtime after compilation, so we
Chris@0 65 * override Symfony's ContainerBuilder's restriction on setting services in a
Chris@0 66 * frozen builder.
Chris@0 67 *
Chris@0 68 * @todo Restrict this to synthetic services only. Ideally, the upstream
Chris@0 69 * ContainerBuilder class should be fixed to allow setting synthetic
Chris@0 70 * services in a frozen builder.
Chris@0 71 */
Chris@0 72 public function set($id, $service) {
Chris@0 73 if (strtolower($id) !== $id) {
Chris@0 74 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
Chris@0 75 }
Chris@0 76 SymfonyContainer::set($id, $service);
Chris@0 77
Chris@0 78 // Ensure that the _serviceId property is set on synthetic services as well.
Chris@0 79 if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
Chris@0 80 $this->services[$id]->_serviceId = $id;
Chris@0 81 }
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * {@inheritdoc}
Chris@0 86 */
Chris@0 87 public function register($id, $class = null) {
Chris@0 88 if (strtolower($id) !== $id) {
Chris@0 89 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
Chris@0 90 }
Chris@0 91 return parent::register($id, $class);
Chris@0 92 }
Chris@0 93
Chris@0 94 /**
Chris@0 95 * {@inheritdoc}
Chris@0 96 */
Chris@14 97 public function setAlias($alias, $id) {
Chris@14 98 $alias = parent::setAlias($alias, $id);
Chris@14 99 // As of Symfony 3.4 all aliases are private by default.
Chris@14 100 $alias->setPublic(TRUE);
Chris@14 101 return $alias;
Chris@14 102 }
Chris@14 103
Chris@14 104 /**
Chris@14 105 * {@inheritdoc}
Chris@14 106 */
Chris@14 107 public function setDefinition($id, Definition $definition) {
Chris@14 108 $definition = parent::setDefinition($id, $definition);
Chris@14 109 // As of Symfony 3.4 all definitions are private by default.
Chris@14 110 // \Symfony\Component\DependencyInjection\Compiler\ResolvePrivatesPassOnly
Chris@14 111 // removes services marked as private from the container even if they are
Chris@14 112 // also marked as public. Drupal requires services that are public to
Chris@14 113 // remain in the container and not be removed.
Chris@14 114 if ($definition->isPublic()) {
Chris@14 115 $definition->setPrivate(FALSE);
Chris@14 116 }
Chris@14 117 return $definition;
Chris@14 118 }
Chris@14 119
Chris@14 120 /**
Chris@14 121 * {@inheritdoc}
Chris@14 122 */
Chris@0 123 public function setParameter($name, $value) {
Chris@0 124 if (strtolower($name) !== $name) {
Chris@0 125 throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
Chris@0 126 }
Chris@0 127 parent::setParameter($name, $value);
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * A 1to1 copy of parent::callMethod.
Chris@14 132 *
Chris@14 133 * @todo https://www.drupal.org/project/drupal/issues/2937010 Since Symfony
Chris@14 134 * 3.4 this is not a 1to1 copy.
Chris@0 135 */
Chris@14 136 protected function callMethod($service, $call, array &$inlineServices = array()) {
Chris@0 137 $services = self::getServiceConditionals($call[1]);
Chris@0 138
Chris@0 139 foreach ($services as $s) {
Chris@0 140 if (!$this->has($s)) {
Chris@0 141 return;
Chris@0 142 }
Chris@0 143 }
Chris@0 144
Chris@0 145 call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
Chris@0 146 }
Chris@0 147
Chris@0 148 /**
Chris@0 149 * {@inheritdoc}
Chris@0 150 */
Chris@0 151 public function __sleep() {
Chris@0 152 assert(FALSE, 'The container was serialized.');
Chris@0 153 return array_keys(get_object_vars($this));
Chris@0 154 }
Chris@0 155
Chris@0 156 }