Skip to content

Commit

Permalink
[database] DatabaseExtension: detects Tracy by presence of service @t…
Browse files Browse the repository at this point in the history
…racy\BlueScreen [Closes nette/di#245]
  • Loading branch information
dg committed Oct 6, 2020
1 parent 15103df commit 1510402
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions database/src/Bridges/DatabaseDI/DatabaseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getConfigSchema(): Nette\Schema\Schema
'user' => Expect::string()->nullable()->dynamic(),
'password' => Expect::string()->nullable()->dynamic(),
'options' => Expect::array(),
'debugger' => Expect::bool(true),
'debugger' => Expect::bool(),
'explain' => Expect::bool(true),
'reflection' => Expect::string(), // BC
'conventions' => Expect::string('discovered'), // Nette\Database\Conventions\DiscoveredConventions
Expand All @@ -61,6 +61,24 @@ public function loadConfiguration()
}


public function beforeCompile()
{
$builder = $this->getContainerBuilder();

foreach ($this->config as $name => $config) {
if ($config->debugger ?? $builder->getByType(\Tracy\BlueScreen::class)) {
$connection = $builder->getDefinition($this->prefix("$name.connection"));
$connection->addSetup('@Tracy\BlueScreen::addPanel', [
[Nette\Bridges\DatabaseTracy\ConnectionPanel::class, 'renderException'],
]);
if ($this->debugMode) {
$connection->addSetup([Nette\Database\Helpers::class, 'createDebugPanel'], [$connection, !empty($config->explain), $name]);
}
}
}
}


private function setupDatabase(\stdClass $config, string $name): void
{
$builder = $this->getContainerBuilder();
Expand Down Expand Up @@ -113,15 +131,6 @@ private function setupDatabase(\stdClass $config, string $name): void
->setFactory(Nette\Database\Context::class, [$connection, $structure, $conventions])
->setAutowired($config->autowired);

if ($config->debugger) {
$connection->addSetup('@Tracy\BlueScreen::addPanel', [
[Nette\Bridges\DatabaseTracy\ConnectionPanel::class, 'renderException'],
]);
if ($this->debugMode) {
$connection->addSetup([Nette\Database\Helpers::class, 'createDebugPanel'], [$connection, !empty($config->explain), $name]);
}
}

if ($this->name === 'database') {
$builder->addAlias($this->prefix($name), $this->prefix("$name.connection"));
$builder->addAlias("nette.database.$name", $this->prefix($name));
Expand Down

0 comments on commit 1510402

Please sign in to comment.