Skip to content
Permalink
Browse files

DatabaseExtension: added getConfigSchema()

  • Loading branch information...
dg committed Mar 11, 2019
1 parent dfb79e3 commit 9b70c0ea7cbaed17efacdbd63f5ba9b31ad06abc
Showing with 29 additions and 29 deletions.
  1. +3 −0 composer.json
  2. +26 −29 src/Bridges/DatabaseDI/DatabaseExtension.php
@@ -26,6 +26,9 @@
"mockery/mockery": "^1.0.0",
"tracy/tracy": "^2.4"
},
"conflict": {
"nette/di": "<3.0"
},
"autoload": {
"classmap": ["src/"]
},
@@ -10,25 +10,14 @@
namespace Nette\Bridges\DatabaseDI;
use Nette;
use Nette\DI\Config\Expect;
/**
* Nette Framework Database services.
*/
class DatabaseExtension extends Nette\DI\CompilerExtension
{
public $databaseDefaults = [
'dsn' => null,
'user' => null,
'password' => null,
'options' => null,
'debugger' => true,
'explain' => true,
'reflection' => null, // BC
'conventions' => 'discovered', // Nette\Database\Conventions\DiscoveredConventions
'autowired' => null,
];
/** @var bool */
private $debugMode;
@@ -39,24 +28,32 @@ public function __construct(bool $debugMode = false)
}
public function loadConfiguration()
public function getConfigSchema(): Nette\DI\Config\Schema
{
$configs = $this->getConfig();
foreach ($configs as $k => $v) {
if (is_scalar($v)) {
$configs = ['default' => $configs];
break;
}
}
return Expect::arrayOf(Expect::struct([
'dsn' => Expect::string()->required()->dynamic(),
'user' => Expect::type('?string')->dynamic(),
'password' => Expect::type('?string')->dynamic(),
'options' => Expect::array(),
'debugger' => Expect::bool(true),
'explain' => Expect::bool(true),
'reflection' => Expect::string(), // BC
'conventions' => Expect::string('discovered'), // Nette\Database\Conventions\DiscoveredConventions
'autowired' => Expect::bool(),
]))->before(function ($val) {
return $val && is_scalar(reset($val))
? ['default' => $val]
: $val;
});
}
$defaults = $this->databaseDefaults;
$defaults['autowired'] = true;
foreach ((array) $configs as $name => $config) {
if (!is_array($config)) {
continue;
}
$config = $this->validateConfig($defaults, $config, $this->prefix($name));
$defaults['autowired'] = false;
public function loadConfiguration()
{
$autowired = true;
foreach ($this->config as $name => $config) {
$config['autowired'] = $config['autowired'] ?? $autowired;
$autowired = false;
$this->setupDatabase($config, $name);
}
}
@@ -66,7 +63,7 @@ private function setupDatabase(array $config, string $name): void
{
$builder = $this->getContainerBuilder();
foreach ((array) $config['options'] as $key => $value) {
foreach ($config['options'] as $key => $value) {
if (is_string($value) && preg_match('#^PDO::\w+\z#', $value)) {
$config['options'][$key] = $value = constant($value);
}

0 comments on commit 9b70c0e

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.