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: 'showBar' => NULL,
27: 'maxLen' => NULL,
28: 'maxDepth' => NULL,
29: 'showLocation' => NULL,
30: 'scream' => NULL,
31: 'bar' => array(),
32: 'blueScreen' => array(),
33: );
34:
35:
36: private $debugMode;
37:
38:
39: public function __construct($debugMode = FALSE)
40: {
41: $this->debugMode = $debugMode;
42: }
43:
44:
45: public function loadConfiguration()
46: {
47: $this->validateConfig($this->defaults);
48: $container = $this->getContainerBuilder();
49:
50: $container->addDefinition($this->prefix('logger'))
51: ->setClass('Tracy\ILogger')
52: ->setFactory('Tracy\Debugger::getLogger');
53:
54: $container->addDefinition($this->prefix('blueScreen'))
55: ->setFactory('Tracy\Debugger::getBlueScreen');
56:
57: $container->addDefinition($this->prefix('bar'))
58: ->setFactory('Tracy\Debugger::getBar');
59: }
60:
61:
62: public function afterCompile(Nette\PhpGenerator\ClassType $class)
63: {
64: $initialize = $class->getMethod('initialize');
65: $container = $this->getContainerBuilder();
66:
67: $options = $this->config;
68: unset($options['bar'], $options['blueScreen']);
69: foreach ($options as $key => $value) {
70: if ($value !== NULL) {
71: $key = ($key === 'fromEmail' ? 'getLogger()->' : '$') . $key;
72: $initialize->addBody($container->formatPhp(
73: 'Tracy\Debugger::' . $key . ' = ?;',
74: Nette\DI\Compiler::filterArguments(array($value))
75: ));
76: }
77: }
78:
79: if ($this->debugMode) {
80: foreach ((array) $this->config['bar'] as $item) {
81: $initialize->addBody($container->formatPhp(
82: '$this->getService(?)->addPanel(?);',
83: Nette\DI\Compiler::filterArguments(array(
84: $this->prefix('bar'),
85: is_string($item) ? new Nette\DI\Statement($item) : $item,
86: ))
87: ));
88: }
89: }
90:
91: foreach ((array) $this->config['blueScreen'] as $item) {
92: $initialize->addBody($container->formatPhp(
93: '$this->getService(?)->addPanel(?);',
94: Nette\DI\Compiler::filterArguments(array($this->prefix('blueScreen'), $item))
95: ));
96: }
97: }
98:
99: }
100: