Exemplo n.º 1
0
 public function testAddService()
 {
     $container = (include self::$fixturesPath . '/containers/container9.php');
     $dumper = new YamlDumper($container);
     $this->assertEquals(str_replace('%path%', self::$fixturesPath . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR, file_get_contents(self::$fixturesPath . '/yaml/services9.yml')), $dumper->dump(), '->dump() dumps services');
     $dumper = new YamlDumper($container = new ContainerBuilder());
     $container->register('foo', 'FooClass')->addArgument(new \stdClass());
     try {
         $dumper->dump();
         $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\RuntimeException', $e, '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
         $this->assertEquals('Unable to dump a service container if a parameter is an object or a resource.', $e->getMessage(), '->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources');
     }
 }
Exemplo n.º 2
0
<?php

require_once __DIR__ . '/../includes/classes.php';
use Symfony\Components\DependencyInjection\ContainerBuilder;
use Symfony\Components\DependencyInjection\Reference;
$container = new ContainerBuilder();
$container->register('foo', 'FooClass')->addArgument(new Reference('bar'));
return $container;
Exemplo n.º 3
0
<?php

require_once __DIR__ . '/../includes/classes.php';
use Symfony\Components\DependencyInjection\ContainerInterface;
use Symfony\Components\DependencyInjection\ContainerBuilder;
use Symfony\Components\DependencyInjection\Reference;
use Symfony\Components\DependencyInjection\Parameter;
$container = new ContainerBuilder();
$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;
Exemplo n.º 4
0
 /**
  * @covers Symfony\Components\DependencyInjection\ContainerBuilder::findAnnotatedServiceIds
  */
 public function testFindAnnotatedServiceIds()
 {
     $builder = new ContainerBuilder();
     $builder->register('foo', 'FooClass')->addAnnotation('foo', array('foo' => 'foo'))->addAnnotation('bar', array('bar' => 'bar'))->addAnnotation('foo', array('foofoo' => 'foofoo'));
     $this->assertEquals($builder->findAnnotatedServiceIds('foo'), array('foo' => array(array('foo' => 'foo'), array('foofoo' => 'foofoo'))), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes');
     $this->assertEquals(array(), $builder->findAnnotatedServiceIds('foobar'), '->findAnnotatedServiceIds() returns an empty array if there is annotated services');
 }