Esempio n. 1
0
 /**
  * @covers Symfony\Components\DependencyInjection\Builder::merge
  */
 public function testMerge()
 {
     $container = new Builder();
     $container->merge(null);
     $this->assertEquals(array(), $container->getParameterBag()->all(), '->merge() accepts null as an argument');
     $this->assertEquals(array(), $container->getDefinitions(), '->merge() accepts null as an argument');
     $container = new Builder(new ParameterBag(array('bar' => 'foo')));
     $config = new BuilderConfiguration(array(), new ParameterBag(array('foo' => 'bar')));
     $container->merge($config);
     $this->assertEquals(array('bar' => 'foo', 'foo' => 'bar'), $container->getParameterBag()->all(), '->merge() merges current parameters with the loaded ones');
     $container = new Builder(new ParameterBag(array('bar' => 'foo', 'foo' => 'baz')));
     $config = new BuilderConfiguration(array(), new ParameterBag(array('foo' => 'bar')));
     $container->merge($config);
     $this->assertEquals(array('bar' => 'foo', 'foo' => 'baz'), $container->getParameterBag()->all(), '->merge() does not change the already defined parameters');
     $container = new Builder(new ParameterBag(array('bar' => 'foo')));
     $config = new BuilderConfiguration(array(), new ParameterBag(array('foo' => '%bar%')));
     $container->merge($config);
     $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
     $container = new Builder(new ParameterBag(array('bar' => 'foo')));
     $config = new BuilderConfiguration(array(), new ParameterBag(array('foo' => '%bar%', 'baz' => '%foo%')));
     $container->merge($config);
     $this->assertEquals(array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), $container->getParameterBag()->all(), '->merge() evaluates the values of the parameters towards already defined ones');
     $container = new Builder();
     $container->register('foo', 'FooClass');
     $container->register('bar', 'BarClass');
     $config = new BuilderConfiguration();
     $config->setDefinition('baz', new Definition('BazClass'));
     $config->setAlias('alias_for_foo', 'foo');
     $container->merge($config);
     $this->assertEquals(array('foo', 'bar', 'baz'), array_keys($container->getDefinitions()), '->merge() merges definitions already defined ones');
     $this->assertEquals(array('alias_for_foo' => 'foo'), $container->getAliases(), '->merge() registers defined aliases');
     $container = new Builder();
     $container->register('foo', 'FooClass');
     $config->setDefinition('foo', new Definition('BazClass'));
     $container->merge($config);
     $this->assertEquals('BazClass', $container->getDefinition('foo')->getClass(), '->merge() overrides already defined services');
 }
Esempio n. 2
0
<?php

require_once __DIR__ . '/../includes/classes.php';
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\DependencyInjection\Builder;
use Symfony\Components\DependencyInjection\Reference;
use Symfony\Components\DependencyInjection\Parameter;
$container = new Builder();
$container->register('foo', 'FooClass')->addAnnotation('foo', array('foo' => 'foo'))->addAnnotation('foo', array('bar' => 'bar'))->setFactoryMethod('getInstance')->setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->setFile(realpath(__DIR__ . '/../includes/foo.php'))->setShared(false)->addMethodCall('setBar', array('bar'))->addMethodCall('initialize')->setConfigurator('sc_configure');
$container->register('bar', 'FooClass')->setArguments(array('foo', new Reference('foo.baz'), new Parameter('foo_bar')))->setShared(true)->setConfigurator(array(new Reference('foo.baz'), 'configure'));
$container->register('foo.baz', '%baz_class%')->setFactoryMethod('getInstance')->setConfigurator(array('%baz_class%', 'configureStatic1'));
$container->register('foo_bar', '%foo_class%');
$container->getParameterBag()->clear();
$container->getParameterBag()->add(array('baz_class' => 'BazClass', 'foo_class' => 'FooClass', 'foo' => 'bar'));
$container->setAlias('alias_for_foo', 'foo');
$container->register('method_call1', 'FooClass')->addMethodCall('setBar', array(new Reference('foo')))->addMethodCall('setBar', array(new Reference('foo', ContainerInterface::NULL_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foo', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)));
$container->register('factory_service')->setFactoryService('foo.baz')->setFactoryMethod('getInstance');
return $container;