1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\DI\Extensions;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class DIExtension extends Nette\DI\CompilerExtension
17: {
18: public $defaults = array(
19: 'debugger' => FALSE,
20: 'accessors' => FALSE,
21: );
22:
23:
24: private $debugMode;
25:
26:
27: private $time;
28:
29:
30: public function __construct($debugMode = FALSE)
31: {
32: $this->debugMode = $debugMode;
33: $this->time = microtime(TRUE);
34: }
35:
36:
37: public function loadConfiguration()
38: {
39: $config = $this->validateConfig($this->defaults);
40: if ($config['accessors']) {
41: $this->getContainerBuilder()->parameters['container']['accessors'] = TRUE;
42: }
43: }
44:
45:
46: public function afterCompile(Nette\PhpGenerator\ClassType $class)
47: {
48: $initialize = $class->getMethod('initialize');
49: $container = $this->getContainerBuilder();
50:
51: if ($this->debugMode && $this->config['debugger']) {
52: Nette\Bridges\DITracy\ContainerPanel::$compilationTime = $this->time;
53: $initialize->addBody($container->formatPhp('?;', array(
54: new Nette\DI\Statement('@Tracy\Bar::addPanel', array(new Nette\DI\Statement('Nette\Bridges\DITracy\ContainerPanel'))),
55: )));
56: }
57:
58: foreach (array_filter($container->findByTag('run')) as $name => $on) {
59: $initialize->addBody('$this->getService(?);', array($name));
60: }
61:
62: if (!empty($this->config['accessors'])) {
63: $definitions = $container->getDefinitions();
64: ksort($definitions);
65: foreach ($definitions as $name => $def) {
66: if (Nette\PhpGenerator\Helpers::isIdentifier($name)) {
67: $type = $def->getImplement() ?: $def->getClass();
68: $class->addComment("@property $type \$$name");
69: }
70: }
71: }
72: }
73:
74: }
75: