mirror of
https://github.com/dg/dibi.git
synced 2025-02-21 09:23:57 +01:00
added DibiExtension3
This commit is contained in:
parent
7ca47508cb
commit
680026747e
@ -639,7 +639,7 @@ In the configuration file, we will register the DI extensions and add the `dibi`
|
||||
|
||||
```neon
|
||||
extensions:
|
||||
dibi: Dibi\Bridges\Nette\DibiExtension22
|
||||
dibi: Dibi\Bridges\Nette\DibiExtension3
|
||||
|
||||
dibi:
|
||||
host: localhost
|
||||
|
96
src/Dibi/Bridges/Nette/DibiExtension3.php
Normal file
96
src/Dibi/Bridges/Nette/DibiExtension3.php
Normal file
@ -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 DibiExtension3 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]);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
# This will create service named 'dibi.connection'.
|
||||
# Requires Nette Framework 2.2 or later
|
||||
# Requires Nette Framework 3 or later
|
||||
|
||||
extensions:
|
||||
dibi: Dibi\Bridges\Nette\DibiExtension22
|
||||
dibi: Dibi\Bridges\Nette\DibiExtension3
|
||||
|
||||
dibi:
|
||||
host: localhost
|
||||
|
Loading…
x
Reference in New Issue
Block a user