Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ContainerBuilder.php

function ContainerBuilder::merge

Merges a ContainerBuilder with the current ContainerBuilder configuration.

Service definitions overrides the current defined ones.

But for parameters, they are overridden by the current ones. It allows the parameters passed to the container constructor to have precedence over the loaded ones.

$container = new ContainerBuilder(new ParameterBag(['foo' => 'bar'])); $loader = new LoaderXXX($container); $loader->load('resource_name'); $container->register('foo', 'stdClass');

In the above example, even if the loaded resource defines a foo parameter, the value will still be 'bar' as defined in the ContainerBuilder constructor.

Throws

BadMethodCallException When this ContainerBuilder is compiled

File

vendor/symfony/dependency-injection/ContainerBuilder.php, line 654

Class

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

Namespace

Symfony\Component\DependencyInjection

Code

public function merge(self $container) : void {
    if ($this->isCompiled()) {
        throw new BadMethodCallException('Cannot merge on a compiled container.');
    }
    foreach ($container->getDefinitions() as $id => $definition) {
        if (!$definition->hasTag('container.excluded') || !$this->has($id)) {
            $this->setDefinition($id, $definition);
        }
    }
    $this->addAliases($container->getAliases());
    $parameterBag = $this->getParameterBag();
    $otherBag = $container->getParameterBag();
    $parameterBag->add($otherBag->all());
    if ($parameterBag instanceof ParameterBag && $otherBag instanceof ParameterBag) {
        foreach ($otherBag->allDeprecated() as $name => $deprecated) {
            $parameterBag->deprecate($name, ...$deprecated);
        }
        foreach ($otherBag->allNonEmpty() as $name => $message) {
            $parameterBag->cannotBeEmpty($name, $message);
        }
    }
    if ($this->trackResources) {
        foreach ($container->getResources() as $resource) {
            $this->addResource($resource);
        }
    }
    foreach ($this->extensions as $name => $extension) {
        if (!isset($this->extensionConfigs[$name])) {
            $this->extensionConfigs[$name] = [];
        }
        $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name));
    }
    if ($parameterBag instanceof EnvPlaceholderParameterBag && $otherBag instanceof EnvPlaceholderParameterBag) {
        $envPlaceholders = $otherBag->getEnvPlaceholders();
        $parameterBag->mergeEnvPlaceholders($otherBag);
    }
    else {
        $envPlaceholders = [];
    }
    foreach ($container->envCounters as $env => $count) {
        if (!$count && !isset($envPlaceholders[$env])) {
            continue;
        }
        if (!isset($this->envCounters[$env])) {
            $this->envCounters[$env] = $count;
        }
        else {
            $this->envCounters[$env] += $count;
        }
    }
    foreach ($container->getAutoconfiguredInstanceof() as $interface => $childDefinition) {
        if (isset($this->autoconfiguredInstanceof[$interface])) {
            throw new InvalidArgumentException(\sprintf('"%s" has already been autoconfigured and merge() does not support merging autoconfiguration for the same class/interface.', $interface));
        }
        $this->autoconfiguredInstanceof[$interface] = $childDefinition;
    }
    foreach ($container->getAutoconfiguredAttributes() as $attribute => $configurator) {
        if (isset($this->autoconfiguredAttributes[$attribute])) {
            throw new InvalidArgumentException(\sprintf('"%s" has already been autoconfigured and merge() does not support merging autoconfiguration for the same attribute.', $attribute));
        }
        $this->autoconfiguredAttributes[$attribute] = $configurator;
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal