1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Tracy\Bridges\Nette;
9:
10: use Nette;
11: use Tracy;
12:
13:
14: 15: 16:
17: class TracyExtension extends Nette\DI\CompilerExtension
18: {
19: public $defaults = [
20: 'email' => null,
21: 'fromEmail' => null,
22: 'logSeverity' => null,
23: 'editor' => null,
24: 'browser' => null,
25: 'errorTemplate' => null,
26: 'strictMode' => null,
27: 'showBar' => null,
28: 'maxLen' => null,
29: 'maxLength' => null,
30: 'maxDepth' => null,
31: 'showLocation' => null,
32: 'scream' => null,
33: 'bar' => [],
34: 'blueScreen' => [],
35: 'editorMapping' => [],
36: 'netteMailer' => true,
37: ];
38:
39:
40: private $debugMode;
41:
42:
43: private $cliMode;
44:
45:
46: public function __construct($debugMode = false, $cliMode = false)
47: {
48: $this->debugMode = $debugMode;
49: $this->cliMode = $cliMode;
50: }
51:
52:
53: public function loadConfiguration()
54: {
55: $this->validateConfig($this->defaults);
56: $builder = $this->getContainerBuilder();
57:
58: $builder->addDefinition($this->prefix('logger'))
59: ->setClass('Tracy\ILogger')
60: ->setFactory('Tracy\Debugger::getLogger');
61:
62: $builder->addDefinition($this->prefix('blueScreen'))
63: ->setFactory('Tracy\Debugger::getBlueScreen');
64:
65: $builder->addDefinition($this->prefix('bar'))
66: ->setFactory('Tracy\Debugger::getBar');
67: }
68:
69:
70: public function afterCompile(Nette\PhpGenerator\ClassType $class)
71: {
72: $initialize = $class->getMethod('initialize');
73: $builder = $this->getContainerBuilder();
74: $class = method_exists('Nette\DI\Helpers', 'filterArguments') ? 'Nette\DI\Helpers' : 'Nette\DI\Compiler';
75:
76: $options = $this->config;
77: unset($options['bar'], $options['blueScreen'], $options['netteMailer']);
78: if (isset($options['logSeverity'])) {
79: $res = 0;
80: foreach ((array) $options['logSeverity'] as $level) {
81: $res |= is_int($level) ? $level : constant($level);
82: }
83: $options['logSeverity'] = $res;
84: }
85: foreach ($options as $key => $value) {
86: if ($value !== null) {
87: $key = ($key === 'fromEmail' ? 'getLogger()->' : '$') . $key;
88: $initialize->addBody($builder->formatPhp(
89: 'Tracy\Debugger::' . $key . ' = ?;',
90: $class::filterArguments([$value])
91: ));
92: }
93: }
94:
95: $logger = $builder->getDefinition($this->prefix('logger'));
96: if ($logger->getFactory()->getEntity() !== ['Tracy\Debugger', 'getLogger']) {
97: $initialize->addBody($builder->formatPhp('Tracy\Debugger::setLogger(?);', [$logger]));
98: }
99: if ($this->config['netteMailer'] && $builder->getByType('Nette\Mail\IMailer')) {
100: $initialize->addBody($builder->formatPhp('Tracy\Debugger::getLogger(?)->mailer = ?;', [
101: $logger,
102: [new Nette\DI\Statement('Tracy\Bridges\Nette\MailSender', ['fromEmail' => $this->config['fromEmail']]), 'send'],
103: ]));
104: }
105:
106: if ($this->debugMode) {
107: foreach ((array) $this->config['bar'] as $item) {
108: $initialize->addBody($builder->formatPhp(
109: '$this->getService(?)->addPanel(?);',
110: $class::filterArguments([
111: $this->prefix('bar'),
112: is_string($item) ? new Nette\DI\Statement($item) : $item,
113: ])
114: ));
115: }
116:
117: if (!$this->cliMode) {
118: $initialize->addBody('if ($tmp = $this->getByType("Nette\Http\Session", false)) { $tmp->start(); Tracy\Debugger::dispatch(); };');
119: }
120: }
121:
122: foreach ((array) $this->config['blueScreen'] as $item) {
123: $initialize->addBody($builder->formatPhp(
124: '$this->getService(?)->addPanel(?);',
125: $class::filterArguments([$this->prefix('blueScreen'), $item])
126: ));
127: }
128:
129: if (($dir = Tracy\Debugger::$logDirectory) && !is_writable($dir)) {
130: throw new Nette\InvalidStateException("Make directory '$dir' writable.");
131: }
132: }
133: }
134: