1010namespace Nette \Bridges \DatabaseDI ;
1111
1212use Nette ;
13+ use Nette \DI \Config \Expect ;
1314
1415
1516/**
1617 * Nette Framework Database services.
1718 */
1819class DatabaseExtension extends Nette \DI \CompilerExtension
1920{
20- public $ databaseDefaults = [
21- 'dsn ' => null ,
22- 'user ' => null ,
23- 'password ' => null ,
24- 'options ' => null ,
25- 'debugger ' => true ,
26- 'explain ' => true ,
27- 'reflection ' => null , // BC
28- 'conventions ' => 'discovered ' , // Nette\Database\Conventions\DiscoveredConventions
29- 'autowired ' => null ,
30- ];
31-
3221 /** @var bool */
3322 private $ debugMode ;
3423
@@ -39,24 +28,32 @@ public function __construct(bool $debugMode = false)
3928 }
4029
4130
42- public function loadConfiguration ()
31+ public function getConfigSchema (): Nette \ DI \ Config \ Schema
4332 {
44- $ configs = $ this ->getConfig ();
45- foreach ($ configs as $ k => $ v ) {
46- if (is_scalar ($ v )) {
47- $ configs = ['default ' => $ configs ];
48- break ;
49- }
50- }
33+ return Expect::arrayOf (Expect::struct ([
34+ 'dsn ' => Expect::string ()->required ()->dynamic (),
35+ 'user ' => Expect::type ('?string ' )->dynamic (),
36+ 'password ' => Expect::type ('?string ' )->dynamic (),
37+ 'options ' => Expect::array (),
38+ 'debugger ' => Expect::bool (true ),
39+ 'explain ' => Expect::bool (true ),
40+ 'reflection ' => Expect::string (), // BC
41+ 'conventions ' => Expect::string ('discovered ' ), // Nette\Database\Conventions\DiscoveredConventions
42+ 'autowired ' => Expect::bool (),
43+ ]))->before (function ($ val ) {
44+ return $ val && is_scalar (reset ($ val ))
45+ ? ['default ' => $ val ]
46+ : $ val ;
47+ });
48+ }
5149
52- $ defaults = $ this ->databaseDefaults ;
53- $ defaults ['autowired ' ] = true ;
54- foreach ((array ) $ configs as $ name => $ config ) {
55- if (!is_array ($ config )) {
56- continue ;
57- }
58- $ config = $ this ->validateConfig ($ defaults , $ config , $ this ->prefix ($ name ));
59- $ defaults ['autowired ' ] = false ;
50+
51+ public function loadConfiguration ()
52+ {
53+ $ autowired = true ;
54+ foreach ($ this ->config as $ name => $ config ) {
55+ $ config ['autowired ' ] = $ config ['autowired ' ] ?? $ autowired ;
56+ $ autowired = false ;
6057 $ this ->setupDatabase ($ config , $ name );
6158 }
6259 }
@@ -66,7 +63,7 @@ private function setupDatabase(array $config, string $name): void
6663 {
6764 $ builder = $ this ->getContainerBuilder ();
6865
69- foreach (( array ) $ config ['options ' ] as $ key => $ value ) {
66+ foreach ($ config ['options ' ] as $ key => $ value ) {
7067 if (is_string ($ value ) && preg_match ('#^PDO::\w+\z# ' , $ value )) {
7168 $ config ['options ' ][$ key ] = $ value = constant ($ value );
7269 }
0 commit comments