-
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com) | ||
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Dibi\Bridges\Nette; | ||
|
||
use Dibi; | ||
use Nette; | ||
use Nette\Schema\Expect; | ||
use Tracy; | ||
|
||
|
||
/** | ||
* Dibi extension for Nette Framework 3. Creates 'connection' & 'panel' services. | ||
*/ | ||
class DibiExtension22 extends Nette\DI\CompilerExtension | ||
{ | ||
private ?bool $debugMode; | ||
private ?bool $cliMode; | ||
|
||
|
||
public function __construct(?bool $debugMode = null, ?bool $cliMode = null) | ||
{ | ||
$this->debugMode = $debugMode; | ||
$this->cliMode = $cliMode; | ||
} | ||
|
||
|
||
public function getConfigSchema(): Nette\Schema\Schema | ||
{ | ||
return Expect::structure([ | ||
'autowired' => Expect::bool(true), | ||
'flags' => Expect::anyOf(Expect::arrayOf('string'), Expect::type('dynamic')), | ||
'profiler' => Expect::bool(), | ||
'explain' => Expect::bool(true), | ||
'filter' => Expect::bool(true), | ||
'driver' => Expect::string()->dynamic(), | ||
'name' => Expect::string()->dynamic(), | ||
'lazy' => Expect::bool(false)->dynamic(), | ||
'onConnect' => Expect::array()->dynamic(), | ||
'substitutes' => Expect::arrayOf('string')->dynamic(), | ||
'result' => Expect::structure([ | ||
'normalize' => Expect::bool(true), | ||
'formatDateTime' => Expect::string(), | ||
'formatTimeInterval' => Expect::string(), | ||
'formatJson' => Expect::string(), | ||
]), | ||
])->otherItems(Expect::type('mixed')) | ||
->castTo('array'); | ||
} | ||
|
||
|
||
public function loadConfiguration() | ||
{ | ||
$container = $this->getContainerBuilder(); | ||
$config = $this->getConfig(); | ||
$this->debugMode ??= $container->parameters['debugMode']; | ||
$this->cliMode ??= $container->parameters['consoleMode']; | ||
|
||
$useProfiler = $config['profiler'] ?? (class_exists(Tracy\Debugger::class) && $this->debugMode && !$this->cliMode); | ||
unset($config['profiler']); | ||
|
||
if (is_array($config['flags'])) { | ||
$flags = 0; | ||
foreach ((array) $config['flags'] as $flag) { | ||
$flags |= constant($flag); | ||
} | ||
$config['flags'] = $flags; | ||
} | ||
|
||
$connection = $container->addDefinition($this->prefix('connection')) | ||
->setCreator(Dibi\Connection::class, [$config]) | ||
->setAutowired($config['autowired']); | ||
|
||
if (class_exists(Tracy\Debugger::class)) { | ||
$connection->addSetup( | ||
[new Nette\DI\Definitions\Statement('Tracy\Debugger::getBlueScreen'), 'addPanel'], | ||
[[Dibi\Bridges\Tracy\Panel::class, 'renderException']], | ||
); | ||
} | ||
|
||
if ($useProfiler) { | ||
$panel = $container->addDefinition($this->prefix('panel')) | ||
->setCreator(Dibi\Bridges\Tracy\Panel::class, [ | ||
$config['explain'], | ||
$config['filter'] ? Dibi\Event::QUERY : Dibi\Event::ALL, | ||
]); | ||
$connection->addSetup([$panel, 'register'], [$connection]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters