You are here

class ContainerBuilder in Service Container 7

Same name in this branch
  1. 7 src/DependencyInjection/ContainerBuilder.php \Drupal\service_container\DependencyInjection\ContainerBuilder
  2. 7 lib/Drupal/Core/DependencyInjection/ContainerBuilder.php \Drupal\Core\DependencyInjection\ContainerBuilder
  3. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ContainerBuilder.php \Symfony\Component\DependencyInjection\ContainerBuilder
Same name and namespace in other branches
  1. 7.2 lib/Drupal/Core/DependencyInjection/ContainerBuilder.php \Drupal\Core\DependencyInjection\ContainerBuilder

Drupal's dependency injection container builder.

@todo Submit upstream patches to Symfony to not require these overrides.

Hierarchy

Expanded class hierarchy of ContainerBuilder

2 files declare their use of ContainerBuilder
DatabaseStorageExpirableTest.php in lib/Drupal/service_container/Tests/KeyValue/DatabaseStorageExpirableTest.php
Contains Drupal\system\Tests\KeyValueStore\DatabaseStorageExpirableTest.
ServiceContainerSymfonyServiceProvider.php in modules/providers/service_container_symfony/src/ServiceContainer/ServiceProvider/ServiceContainerSymfonyServiceProvider.php
Contains \Drupal\service_container_symfony\ServiceContainer\ServiceProvider\ServiceContainerSymfonyServiceProvider

File

lib/Drupal/Core/DependencyInjection/ContainerBuilder.php, line 22
Contains \Drupal\Core\DependencyInjection\ContainerBuilder.

Namespace

Drupal\Core\DependencyInjection
View source
class ContainerBuilder extends SymfonyContainerBuilder {

  /**
   * {@inheritdoc}
   */
  public function __construct(ParameterBagInterface $parameterBag = NULL) {
    $this
      ->setResourceTracking(FALSE);
    parent::__construct($parameterBag);
  }

  /**
   * 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) {
    if (strtolower($id) !== $id) {
      throw new \InvalidArgumentException("Service ID names must be lowercase: {$id}");
    }
    SymfonyContainer::set($id, $service, $scope);

    // 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;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function register($id, $class = null) {
    if (strtolower($id) !== $id) {
      throw new \InvalidArgumentException("Service ID names must be lowercase: {$id}");
    }
    return parent::register($id, $class);
  }

  /**
   * {@inheritdoc}
   */
  public function setParameter($name, $value) {
    if (strtolower($name) !== $name) {
      throw new \InvalidArgumentException("Parameter names must be lowercase: {$name}");
    }
    parent::setParameter($name, $value);
  }

  /**
   * 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])));
  }

  /**
   * {@inheritdoc}
   */
  public function __sleep() {
    trigger_error('The container was serialized.', E_USER_ERROR);
    return array_keys(get_object_vars($this));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContainerBuilder::$aliasDefinitions private property
ContainerBuilder::$compiler private property
ContainerBuilder::$definitions private property
ContainerBuilder::$expressionLanguage private property
ContainerBuilder::$expressionLanguageProviders private property
ContainerBuilder::$extensionConfigs private property
ContainerBuilder::$extensions private property
ContainerBuilder::$extensionsByNs private property
ContainerBuilder::$obsoleteDefinitions private property
ContainerBuilder::$proxyInstantiator private property
ContainerBuilder::$resources private property
ContainerBuilder::$trackResources private property
ContainerBuilder::addAliases public function Adds the service aliases.
ContainerBuilder::addClassResource public function Adds the given class hierarchy as resources.
ContainerBuilder::addCompilerPass public function Adds a compiler pass.
ContainerBuilder::addDefinitions public function Adds the service definitions.
ContainerBuilder::addExpressionLanguageProvider public function
ContainerBuilder::addObjectResource public function Adds the object class hierarchy as resources.
ContainerBuilder::addResource public function Adds a resource for this configuration.
ContainerBuilder::callMethod protected function A 1to1 copy of parent::callMethod. Overrides ContainerBuilder::callMethod
ContainerBuilder::compile public function Compiles the container.
ContainerBuilder::createService public function Creates a service for a service definition.
ContainerBuilder::findDefinition public function Gets a service definition by id or alias.
ContainerBuilder::findTaggedServiceIds public function Returns service ids for a given tag. Overrides TaggedContainerInterface::findTaggedServiceIds
ContainerBuilder::findTags public function Returns all tags the defined services use.
ContainerBuilder::get public function Gets a service.
ContainerBuilder::getAlias public function Gets an alias.
ContainerBuilder::getAliases public function Gets all defined aliases.
ContainerBuilder::getCompiler public function Returns the compiler.
ContainerBuilder::getCompilerPassConfig public function Returns the compiler pass config which can then be modified.
ContainerBuilder::getDefinition public function Gets a service definition.
ContainerBuilder::getDefinitions public function Gets all service definitions.
ContainerBuilder::getExpressionLanguage private function
ContainerBuilder::getExpressionLanguageProviders public function
ContainerBuilder::getExtension public function Returns an extension by alias or namespace.
ContainerBuilder::getExtensionConfig public function Returns the configuration array for the given extension.
ContainerBuilder::getExtensions public function Returns all registered extensions.
ContainerBuilder::getProxyInstantiator private function Retrieves the currently set proxy instantiator or instantiates one.
ContainerBuilder::getResources public function Returns an array of resources loaded to build this configuration.
ContainerBuilder::getScopeChildren public function Returns all Scope children.
ContainerBuilder::getScopes public function Returns all Scopes.
ContainerBuilder::getServiceConditionals public static function Returns the Service Conditionals.
ContainerBuilder::getServiceIds public function Gets all service ids.
ContainerBuilder::has public function Returns true if the given service is defined.
ContainerBuilder::hasAlias public function Returns true if an alias exists under the given identifier.
ContainerBuilder::hasDefinition public function Returns true if a service definition exists under the given identifier.
ContainerBuilder::hasExtension public function Checks if we have an extension.
ContainerBuilder::isTrackingResources public function Checks if resources are tracked.
ContainerBuilder::loadFromExtension public function Loads the configuration for an extension.
ContainerBuilder::merge public function Merges a ContainerBuilder with the current ContainerBuilder configuration.
ContainerBuilder::prependExtensionConfig public function Prepends a config array to the configs of the given extension.
ContainerBuilder::register public function Registers a service definition. Overrides ContainerBuilder::register
ContainerBuilder::registerExtension public function Registers an extension.
ContainerBuilder::removeAlias public function Removes an alias.
ContainerBuilder::removeDefinition public function Removes a service definition.
ContainerBuilder::resolveServices public function Replaces service references by the real service instance and evaluates expressions.
ContainerBuilder::set public function Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set(). Overrides ContainerBuilder::set
ContainerBuilder::setAlias public function Sets an alias for an existing service.
ContainerBuilder::setAliases public function Sets the service aliases.
ContainerBuilder::setDefinition public function Sets a service definition.
ContainerBuilder::setDefinitions public function Sets the service definitions.
ContainerBuilder::setParameter public function
ContainerBuilder::setProxyInstantiator public function Sets the instantiator to be used when fetching proxies.
ContainerBuilder::setResources public function Sets the resources for this configuration.
ContainerBuilder::setResourceTracking public function Sets the track resources flag.
ContainerBuilder::shareService private function Shares a given service in the container.
ContainerBuilder::synchronize private function Synchronizes a service change. Overrides ContainerBuilder::synchronize
ContainerBuilder::__construct public function
ContainerBuilder::__sleep public function