annotate core/lib/Drupal/Core/DependencyInjection/ContainerBuilder.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
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@0 30 $this->setResourceTracking(FALSE);
Chris@0 31 parent::__construct($parameterBag);
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@0 49 * {@inheritdoc}
Chris@0 50 */
Chris@0 51 protected function shareService(Definition $definition, $service, $id)
Chris@0 52 {
Chris@0 53 if ($definition->isShared()) {
Chris@0 54 $this->services[$lowerId = strtolower($id)] = $service;
Chris@0 55 }
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().
Chris@0 60 *
Chris@0 61 * Drupal's container builder can be used at runtime after compilation, so we
Chris@0 62 * override Symfony's ContainerBuilder's restriction on setting services in a
Chris@0 63 * frozen builder.
Chris@0 64 *
Chris@0 65 * @todo Restrict this to synthetic services only. Ideally, the upstream
Chris@0 66 * ContainerBuilder class should be fixed to allow setting synthetic
Chris@0 67 * services in a frozen builder.
Chris@0 68 */
Chris@0 69 public function set($id, $service) {
Chris@0 70 if (strtolower($id) !== $id) {
Chris@0 71 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
Chris@0 72 }
Chris@0 73 SymfonyContainer::set($id, $service);
Chris@0 74
Chris@0 75 // Ensure that the _serviceId property is set on synthetic services as well.
Chris@0 76 if (isset($this->services[$id]) && is_object($this->services[$id]) && !isset($this->services[$id]->_serviceId)) {
Chris@0 77 $this->services[$id]->_serviceId = $id;
Chris@0 78 }
Chris@0 79 }
Chris@0 80
Chris@0 81 /**
Chris@0 82 * {@inheritdoc}
Chris@0 83 */
Chris@0 84 public function register($id, $class = null) {
Chris@0 85 if (strtolower($id) !== $id) {
Chris@0 86 throw new \InvalidArgumentException("Service ID names must be lowercase: $id");
Chris@0 87 }
Chris@0 88 return parent::register($id, $class);
Chris@0 89 }
Chris@0 90
Chris@0 91 /**
Chris@0 92 * {@inheritdoc}
Chris@0 93 */
Chris@0 94 public function setParameter($name, $value) {
Chris@0 95 if (strtolower($name) !== $name) {
Chris@0 96 throw new \InvalidArgumentException("Parameter names must be lowercase: $name");
Chris@0 97 }
Chris@0 98 parent::setParameter($name, $value);
Chris@0 99 }
Chris@0 100
Chris@0 101 /**
Chris@0 102 * A 1to1 copy of parent::callMethod.
Chris@0 103 */
Chris@0 104 protected function callMethod($service, $call) {
Chris@0 105 $services = self::getServiceConditionals($call[1]);
Chris@0 106
Chris@0 107 foreach ($services as $s) {
Chris@0 108 if (!$this->has($s)) {
Chris@0 109 return;
Chris@0 110 }
Chris@0 111 }
Chris@0 112
Chris@0 113 call_user_func_array(array($service, $call[0]), $this->resolveServices($this->getParameterBag()->resolveValue($call[1])));
Chris@0 114 }
Chris@0 115
Chris@0 116 /**
Chris@0 117 * {@inheritdoc}
Chris@0 118 */
Chris@0 119 public function __sleep() {
Chris@0 120 assert(FALSE, 'The container was serialized.');
Chris@0 121 return array_keys(get_object_vars($this));
Chris@0 122 }
Chris@0 123
Chris@0 124 }