container9.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. require_once __DIR__.'/../includes/classes.php';
  3. use Symfony\Component\DependencyInjection\ContainerInterface;
  4. use Symfony\Component\DependencyInjection\ContainerBuilder;
  5. use Symfony\Component\DependencyInjection\Reference;
  6. use Symfony\Component\DependencyInjection\Parameter;
  7. $container = new ContainerBuilder();
  8. $container->
  9. register('foo', 'FooClass')->
  10. addTag('foo', array('foo' => 'foo'))->
  11. addTag('foo', array('bar' => 'bar'))->
  12. setFactoryClass('FooClass')->
  13. setFactoryMethod('getInstance')->
  14. setArguments(array('foo', new Reference('foo.baz'), array('%foo%' => 'foo is %foo%', 'bar' => '%foo%'), true, new Reference('service_container')))->
  15. setScope('prototype')->
  16. addMethodCall('setBar', array(new Reference('bar')))->
  17. addMethodCall('initialize')->
  18. setConfigurator('sc_configure')
  19. ;
  20. $container->
  21. register('bar', 'FooClass')->
  22. setArguments(array('foo', new Reference('foo.baz'), new Parameter('foo_bar')))->
  23. setScope('container')->
  24. setConfigurator(array(new Reference('foo.baz'), 'configure'))
  25. ;
  26. $container->
  27. register('foo.baz', '%baz_class%')->
  28. setFactoryClass('%baz_class%')->
  29. setFactoryMethod('getInstance')->
  30. setConfigurator(array('%baz_class%', 'configureStatic1'))
  31. ;
  32. $container->register('foo_bar', '%foo_class%');
  33. $container->getParameterBag()->clear();
  34. $container->getParameterBag()->add(array(
  35. 'baz_class' => 'BazClass',
  36. 'foo_class' => 'FooClass',
  37. 'foo' => 'bar',
  38. ));
  39. $container->setAlias('alias_for_foo', 'foo');
  40. $container->
  41. register('method_call1', 'FooClass')->
  42. setFile(realpath(__DIR__.'/../includes/foo.php'))->
  43. addMethodCall('setBar', array(new Reference('foo')))->
  44. addMethodCall('setBar', array(new Reference('foo2', ContainerInterface::NULL_ON_INVALID_REFERENCE)))->
  45. addMethodCall('setBar', array(new Reference('foo3', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))->
  46. addMethodCall('setBar', array(new Reference('foobaz', ContainerInterface::IGNORE_ON_INVALID_REFERENCE)))
  47. ;
  48. $container->
  49. register('factory_service')->
  50. setFactoryService('foo.baz')->
  51. setFactoryMethod('getInstance')
  52. ;
  53. return $container;