1: <?php
2:
3: 4: 5: 6:
7:
8:
9:
10: namespace Dibi\Bridges\Nette;
11:
12: use Dibi;
13: use Nette;
14: use Tracy;
15:
16:
17: 18: 19:
20: class DibiExtension22 extends Nette\DI\CompilerExtension
21: {
22:
23: private $debugMode;
24:
25:
26: public function __construct(bool $debugMode = null)
27: {
28: $this->debugMode = $debugMode;
29: }
30:
31:
32: public function loadConfiguration()
33: {
34: $container = $this->getContainerBuilder();
35: $config = $this->getConfig();
36:
37: if ($this->debugMode === null) {
38: $this->debugMode = $container->parameters['debugMode'];
39: }
40:
41: $useProfiler = $config['profiler'] ?? (class_exists(Tracy\Debugger::class) && $this->debugMode);
42:
43: unset($config['profiler']);
44:
45: if (isset($config['flags'])) {
46: $flags = 0;
47: foreach ((array) $config['flags'] as $flag) {
48: $flags |= constant($flag);
49: }
50: $config['flags'] = $flags;
51: }
52:
53: $connection = $container->addDefinition($this->prefix('connection'))
54: ->setFactory(Dibi\Connection::class, [$config])
55: ->setAutowired($config['autowired'] ?? true);
56:
57: if (class_exists(Tracy\Debugger::class)) {
58: $connection->addSetup(
59: [new Nette\DI\Statement('Tracy\Debugger::getBlueScreen'), 'addPanel'],
60: [[Dibi\Bridges\Tracy\Panel::class, 'renderException']]
61: );
62: }
63: if ($useProfiler) {
64: $panel = $container->addDefinition($this->prefix('panel'))
65: ->setFactory(Dibi\Bridges\Tracy\Panel::class, [
66: $config['explain'] ?? true,
67: isset($config['filter']) && $config['filter'] === false ? Dibi\Event::ALL : Dibi\Event::QUERY,
68: ]);
69: $connection->addSetup([$panel, 'register'], [$connection]);
70: }
71: }
72: }
73: