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