1010namespace Nette \Bridges \DatabaseDI ;
1111
1212use Nette ;
13+ use Nette \Schema \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,84 +28,97 @@ public function __construct(bool $debugMode = false)
3928 }
4029
4130
31+ public function getConfigSchema (): Nette \Schema \Schema
32+ {
33+ return Expect::arrayOf (
34+ Expect::structure ([
35+ 'dsn ' => Expect::string ()->required ()->dynamic (),
36+ 'user ' => Expect::string ()->nullable ()->dynamic (),
37+ 'password ' => Expect::string ()->nullable ()->dynamic (),
38+ 'options ' => Expect::array (),
39+ 'debugger ' => Expect::bool (true ),
40+ 'explain ' => Expect::bool (true ),
41+ 'reflection ' => Expect::string (), // BC
42+ 'conventions ' => Expect::string ('discovered ' ), // Nette\Database\Conventions\DiscoveredConventions
43+ 'autowired ' => Expect::bool (),
44+ ])
45+ )->before (function ($ val ) {
46+ return is_array (reset ($ val )) || reset ($ val ) === null
47+ ? $ val
48+ : ['default ' => $ val ];
49+ });
50+ }
51+
52+
4253 public function loadConfiguration ()
4354 {
44- $ configs = $ this ->getConfig ();
45- $ configs = is_array (reset ($ configs ))
46- ? $ configs
47- : ['default ' => $ configs ];
48-
49- $ defaults = $ this ->databaseDefaults ;
50- $ defaults ['autowired ' ] = true ;
51- foreach ((array ) $ configs as $ name => $ config ) {
52- if (!is_array ($ config )) {
53- continue ;
54- }
55- $ config = $ this ->validateConfig ($ defaults , $ config , $ this ->prefix ($ name ));
56- $ defaults ['autowired ' ] = false ;
55+ $ autowired = true ;
56+ foreach ($ this ->config as $ name => $ config ) {
57+ $ config ->autowired = $ config ->autowired ?? $ autowired ;
58+ $ autowired = false ;
5759 $ this ->setupDatabase ($ config , $ name );
5860 }
5961 }
6062
6163
62- private function setupDatabase (array $ config , string $ name ): void
64+ private function setupDatabase (\ stdClass $ config , string $ name ): void
6365 {
6466 $ builder = $ this ->getContainerBuilder ();
6567
66- foreach (( array ) $ config[ ' options ' ] as $ key => $ value ) {
68+ foreach ($ config-> options as $ key => $ value ) {
6769 if (is_string ($ value ) && preg_match ('#^PDO::\w+\z# ' , $ value )) {
68- $ config[ ' options ' ] [$ key ] = $ value = constant ($ value );
70+ $ config-> options [$ key ] = $ value = constant ($ value );
6971 }
7072 if (preg_match ('#^PDO::\w+\z# ' , $ key )) {
71- unset($ config[ ' options ' ] [$ key ]);
72- $ config[ ' options ' ] [constant ($ key )] = $ value ;
73+ unset($ config-> options [$ key ]);
74+ $ config-> options [constant ($ key )] = $ value ;
7375 }
7476 }
7577
7678 $ connection = $ builder ->addDefinition ($ this ->prefix ("$ name.connection " ))
77- ->setFactory (Nette \Database \Connection::class, [$ config[ ' dsn ' ] , $ config[ ' user ' ] , $ config[ ' password ' ] , $ config[ ' options ' ] ])
78- ->setAutowired ($ config[ ' autowired ' ] );
79+ ->setFactory (Nette \Database \Connection::class, [$ config-> dsn , $ config-> user , $ config-> password , $ config-> options ])
80+ ->setAutowired ($ config-> autowired );
7981
8082 $ structure = $ builder ->addDefinition ($ this ->prefix ("$ name.structure " ))
8183 ->setFactory (Nette \Database \Structure::class)
8284 ->setArguments ([$ connection ])
83- ->setAutowired ($ config[ ' autowired ' ] );
85+ ->setAutowired ($ config-> autowired );
8486
85- if (!empty ($ config[ ' reflection ' ] )) {
87+ if (!empty ($ config-> reflection )) {
8688 $ conventionsServiceName = 'reflection ' ;
87- $ config[ ' conventions ' ] = $ config[ ' reflection ' ] ;
88- if (is_string ($ config[ ' conventions ' ] ) && strtolower ($ config[ ' conventions ' ] ) === 'conventional ' ) {
89- $ config[ ' conventions ' ] = 'Static ' ;
89+ $ config-> conventions = $ config-> reflection ;
90+ if (is_string ($ config-> conventions ) && strtolower ($ config-> conventions ) === 'conventional ' ) {
91+ $ config-> conventions = 'Static ' ;
9092 }
9193 } else {
9294 $ conventionsServiceName = 'conventions ' ;
9395 }
9496
95- if (!$ config[ ' conventions ' ] ) {
97+ if (!$ config-> conventions ) {
9698 $ conventions = null ;
9799
98- } elseif (is_string ($ config[ ' conventions ' ] )) {
100+ } elseif (is_string ($ config-> conventions )) {
99101 $ conventions = $ builder ->addDefinition ($ this ->prefix ("$ name. $ conventionsServiceName " ))
100- ->setFactory (preg_match ('#^[a-z]+\z#i ' , $ config[ ' conventions ' ] )
101- ? 'Nette\Database\Conventions \\' . ucfirst ($ config[ ' conventions ' ] ) . 'Conventions '
102- : $ config[ ' conventions ' ] )
103- ->setArguments (strtolower ($ config[ ' conventions ' ] ) === 'discovered ' ? [$ structure ] : [])
104- ->setAutowired ($ config[ ' autowired ' ] );
102+ ->setFactory (preg_match ('#^[a-z]+\z#i ' , $ config-> conventions )
103+ ? 'Nette\Database\Conventions \\' . ucfirst ($ config-> conventions ) . 'Conventions '
104+ : $ config-> conventions )
105+ ->setArguments (strtolower ($ config-> conventions ) === 'discovered ' ? [$ structure ] : [])
106+ ->setAutowired ($ config-> autowired );
105107
106108 } else {
107109 $ conventions = Nette \DI \Config \Processor::processArguments ([$ config ['conventions ' ]])[0 ];
108110 }
109111
110112 $ builder ->addDefinition ($ this ->prefix ("$ name.context " ))
111113 ->setFactory (Nette \Database \Context::class, [$ connection , $ structure , $ conventions ])
112- ->setAutowired ($ config[ ' autowired ' ] );
114+ ->setAutowired ($ config-> autowired );
113115
114- if ($ config[ ' debugger ' ] ) {
116+ if ($ config-> debugger ) {
115117 $ connection ->addSetup ('@Tracy\BlueScreen::addPanel ' , [
116118 [Nette \Bridges \DatabaseTracy \ConnectionPanel::class, 'renderException ' ],
117119 ]);
118120 if ($ this ->debugMode ) {
119- $ connection ->addSetup ([Nette \Database \Helpers::class, 'createDebugPanel ' ], [$ connection , !empty ($ config[ ' explain ' ] ), $ name ]);
121+ $ connection ->addSetup ([Nette \Database \Helpers::class, 'createDebugPanel ' ], [$ connection , !empty ($ config-> explain ), $ name ]);
120122 }
121123 }
122124
0 commit comments