Symfony\Component\DependencyInjection\ContainerBuilder::merge PHP Method

merge() public method

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(array('foo' => 'bar')); $loader = new LoaderXXX($container); $loader->load('resource_name'); $container->register('foo', new 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.
public merge ( ContainerBuilder $container )
$container ContainerBuilder The ContainerBuilder instance to merge
    public function merge(ContainerBuilder $container)
    {
        if ($this->isFrozen()) {
            throw new BadMethodCallException('Cannot merge on a frozen container.');
        }

        $this->addDefinitions($container->getDefinitions());
        $this->addAliases($container->getAliases());
        $this->getParameterBag()->add($container->getParameterBag()->all());

        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] = array();
            }

            $this->extensionConfigs[$name] = array_merge($this->extensionConfigs[$name], $container->getExtensionConfig($name));
        }

        if ($this->getParameterBag() instanceof EnvPlaceholderParameterBag && $container->getParameterBag() instanceof EnvPlaceholderParameterBag) {
            $this->getParameterBag()->mergeEnvPlaceholders($container->getParameterBag());
        }

        foreach ($container->envCounters as $env => $count) {
            if (!isset($this->envCounters[$env])) {
                $this->envCounters[$env] = $count;
            } else {
                $this->envCounters[$env] += $count;
            }
        }
    }

Usage Example

Example #1
0
 /** @dataProvider provideAllServicesDefined */
 public function testMergingContainsHaveAllServicesAfterCompilation($definitionId)
 {
     $container2 = new ContainerBuilder();
     $container2->merge($this->container);
     $container2->compile();
     $this->assertTrue($container2->hasDefinition($definitionId));
 }
All Usage Examples Of Symfony\Component\DependencyInjection\ContainerBuilder::merge