pastebin

Paste Search Dynamic
Recent pastes
ContainerBuilder expand
  1. <?php
  2.  
  3. /**
  4.  * Test: NetteDIContainerBuilder.
  5.  *
  6.  * @author     David Grudl
  7.  * @package    NetteDI
  8.  * @subpackage UnitTests
  9.  */
  10.  
  11. use NetteDI;
  12.  
  13.  
  14.  
  15. require __DIR__ . '/../bootstrap.php';
  16.  
  17.  
  18.  
  19. class Service
  20. {
  21.         public $args;
  22.         public $methods;
  23.  
  24.         static function create(DIIContainer $container = null)
  25.         {
  26.                 $args = func_get_args();
  27.                 unset($args[0]);
  28.                 return new self($args);
  29.         }
  30.  
  31.         function __construct()
  32.         {
  33.                 $this->args = func_get_args();
  34.         }
  35.  
  36.         function __call($nm, $args)
  37.         {
  38.                 $this->methods[] = array($nm, $args);
  39.         }
  40.  
  41. }
  42.  
  43.  
  44.  
  45. $builder = new DIContainerBuilder;
  46. $builder->parameters = array(
  47.         'serviceClass' => 'Service',
  48.         'arg1' => 'a',
  49.         'tag' => 'attrs',
  50. );
  51. $builder->addDefinition('one')
  52.         ->setClass('%serviceClass%', array('%arg1%', 'b'))
  53.         ->addSetup('methodA', array('%arg1%', 'b'));
  54.  
  55. $builder->addDefinition('two')
  56.         ->setFactory('%serviceClass%::create', array('@container', '%arg1%', '@one'));
  57.  
  58. $builder->addDefinition('three')
  59.         ->setFactory(array('%serviceClass%', 'create'));
  60.  
  61.  
  62. $code = (string) $builder->generateClass();
  63. file_put_contents(TEMP_DIR . '/code.php', "<?phpn$code");
  64. require TEMP_DIR . '/code.php';
  65.  
  66. $container = new Container;
  67.  
  68.  
  69. assert::true( $container->getService('one') instanceof Service );
  70. assert::same( array('a', 'b'), $container->getService('one')->args );
  71. assert::same( array(array('methodA', array('a', 'b'))), $container->getService('one')->methods );
  72.  
  73. assert::true( $container->getService('two') instanceof Service );
  74. assert::equal( array(array(1 => 'a', $container->getService('one'))), $container->getService('two')->args );
  75.  
  76. assert::true( $container->getService('three') instanceof Service );
Parsed in 0.251 seconds