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