1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Tracy\Bridges\Nette;
9:
10: use Nette;
11:
12:
13: 14: 15:
16: class TracyExtension extends Nette\DI\CompilerExtension
17: {
18: public $defaults = array(
19: 'email' => NULL,
20: 'fromEmail' => NULL,
21: 'logSeverity' => NULL,
22: 'editor' => NULL,
23: 'browser' => NULL,
24: 'errorTemplate' => NULL,
25: 'strictMode' => NULL,
26: 'maxLen' => NULL,
27: 'maxDepth' => NULL,
28: 'showLocation' => NULL,
29: 'scream' => NULL,
30: 'bar' => array(),
31: 'blueScreen' => array(),
32: );
33:
34:
35: private $debugMode;
36:
37:
38: public function __construct($debugMode = FALSE)
39: {
40: $this->debugMode = $debugMode;
41: }
42:
43:
44: public function loadConfiguration()
45: {
46: $this->validateConfig($this->defaults);
47: $container = $this->getContainerBuilder();
48:
49: $container->addDefinition($this->prefix('logger'))
50: ->setClass('Tracy\ILogger')
51: ->setFactory('Tracy\Debugger::getLogger');
52:
53: $container->addDefinition($this->prefix('blueScreen'))
54: ->setFactory('Tracy\Debugger::getBlueScreen');
55:
56: $container->addDefinition($this->prefix('bar'))
57: ->setFactory('Tracy\Debugger::getBar');
58: }
59:
60:
61: public function afterCompile(Nette\PhpGenerator\ClassType $class)
62: {
63: $initialize = $class->getMethod('initialize');
64: $container = $this->getContainerBuilder();
65:
66: $options = $this->config;
67: unset($options['bar'], $options['blueScreen']);
68: foreach ($options as $key => $value) {
69: if ($value !== NULL) {
70: $key = ($key === 'fromEmail' ? 'getLogger()->' : '$') . $key;
71: $initialize->addBody($container->formatPhp(
72: 'Tracy\Debugger::' . $key . ' = ?;',
73: Nette\DI\Compiler::filterArguments(array($value))
74: ));
75: }
76: }
77:
78: if ($this->debugMode) {
79: foreach ((array) $this->config['bar'] as $item) {
80: $initialize->addBody($container->formatPhp(
81: '$this->getService(?)->addPanel(?);',
82: Nette\DI\Compiler::filterArguments(array(
83: $this->prefix('bar'),
84: is_string($item) ? new Nette\DI\Statement($item) : $item,
85: ))
86: ));
87: }
88: }
89:
90: foreach ((array) $this->config['blueScreen'] as $item) {
91: $initialize->addBody($container->formatPhp(
92: '$this->getService(?)->addPanel(?);',
93: Nette\DI\Compiler::filterArguments(array($this->prefix('blueScreen'), $item))
94: ));
95: }
96: }
97:
98: }
99: