You are here

public function ContainerBuilder::set in Service Container 7

Same name in this branch
  1. 7 lib/Drupal/Core/DependencyInjection/ContainerBuilder.php \Drupal\Core\DependencyInjection\ContainerBuilder::set()
  2. 7 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ContainerBuilder.php \Symfony\Component\DependencyInjection\ContainerBuilder::set()
Same name and namespace in other branches
  1. 7.2 modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ContainerBuilder.php \Symfony\Component\DependencyInjection\ContainerBuilder::set()

Sets a service.

@api

Parameters

string $id The service identifier:

object $service The service instance:

string $scope The scope:

Throws

BadMethodCallException When this ContainerBuilder is frozen

1 method overrides ContainerBuilder::set()
ContainerBuilder::set in lib/Drupal/Core/DependencyInjection/ContainerBuilder.php
Overrides Symfony\Component\DependencyInjection\ContainerBuilder::set().

File

modules/providers/service_container_symfony/lib/Symfony/Component/DependencyInjection/ContainerBuilder.php, line 390

Class

ContainerBuilder
ContainerBuilder is a DI container that provides an API to easily describe services.

Namespace

Symfony\Component\DependencyInjection

Code

public function set($id, $service, $scope = self::SCOPE_CONTAINER) {
  $id = strtolower($id);
  if ($this
    ->isFrozen()) {

    // setting a synthetic service on a frozen container is alright
    if (!isset($this->definitions[$id]) && !isset($this->obsoleteDefinitions[$id]) || isset($this->definitions[$id]) && !$this->definitions[$id]
      ->isSynthetic() || isset($this->obsoleteDefinitions[$id]) && !$this->obsoleteDefinitions[$id]
      ->isSynthetic()) {
      throw new BadMethodCallException(sprintf('Setting service "%s" on a frozen container is not allowed.', $id));
    }
  }
  if (isset($this->definitions[$id])) {
    $this->obsoleteDefinitions[$id] = $this->definitions[$id];
  }
  unset($this->definitions[$id], $this->aliasDefinitions[$id]);
  parent::set($id, $service, $scope);
  if (isset($this->obsoleteDefinitions[$id]) && $this->obsoleteDefinitions[$id]
    ->isSynchronized(false)) {
    $this
      ->synchronize($id);
  }
}