ContainerBuilder.php 3.11 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\Core\DependencyInjection\Container.
 */

namespace Drupal\Core\DependencyInjection;

10 11
use Symfony\Component\DependencyInjection\ContainerBuilder as SymfonyContainerBuilder;
use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
12
use Symfony\Component\DependencyInjection\Reference;
13
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
14 15

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

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

32 33 34 35 36 37 38 39 40 41 42 43
  /**
   * 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.
   */
  public function set($id, $service, $scope = self::SCOPE_CONTAINER) {
44
    SymfonyContainer::set($id, $service, $scope);
45 46 47 48

    if ($this->hasDefinition($id) && ($definition = $this->getDefinition($id)) && $definition->isSynchronized()) {
      $this->synchronize($id);
    }
49 50 51 52 53

    // 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;
    }
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  }

  /**
   * Synchronizes a service change.
   *
   * This method is a copy of the ContainerBuilder of symfony.
   *
   * This method updates all services that depend on the given
   * service by calling all methods referencing it.
   *
   * @param string $id A service id
   */
  private function synchronize($id) {
    foreach ($this->getDefinitions() as $definitionId => $definition) {
      // only check initialized services
      if (!$this->initialized($definitionId)) {
        continue;
      }

      foreach ($definition->getMethodCalls() as $call) {
        foreach ($call[1] as $argument) {
          if ($argument instanceof Reference && $id == (string) $argument) {
            $this->callMethod($this->get($definitionId), $call);
          }
        }
      }
    }
  }

  /**
   * A 1to1 copy of parent::callMethod.
   */
  protected function callMethod($service, $call) {
    $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
96 97
  }

98 99 100 101 102 103 104 105
  /**
   * {@inheritdoc}
   */
  public function __sleep() {
    trigger_error('The container was serialized.', E_USER_ERROR);
    return array_keys(get_object_vars($this));
  }

106
}