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 = [
19: 'debugger' => true,
20: 'accessors' => false,
21: 'excluded' => [],
22: 'parentClass' => null,
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: $builder = $this->getContainerBuilder();
43: $builder->addExcludedClasses($config['excluded']);
44: }
45:
46:
47: public function afterCompile(Nette\PhpGenerator\ClassType $class)
48: {
49: if ($this->config['parentClass']) {
50: $class->setExtends($this->config['parentClass']);
51: }
52:
53: $initialize = $class->getMethod('initialize');
54: $builder = $this->getContainerBuilder();
55:
56: if ($this->debugMode && $this->config['debugger']) {
57: Nette\Bridges\DITracy\ContainerPanel::$compilationTime = $this->time;
58: $initialize->addBody($builder->formatPhp('?;', [
59: new Nette\DI\Statement('@Tracy\Bar::addPanel', [new Nette\DI\Statement(Nette\Bridges\DITracy\ContainerPanel::class)]),
60: ]));
61: }
62:
63: foreach (array_filter($builder->findByTag('run')) as $name => $on) {
64: $initialize->addBody('$this->getService(?);', [$name]);
65: }
66: }
67: }
68: