10
10
namespace Nette \Bridges \DatabaseDI ;
11
11
12
12
use Nette ;
13
+ use Nette \Schema \Expect ;
13
14
14
15
15
16
/**
16
17
* Nette Framework Database services.
17
18
*/
18
19
class DatabaseExtension extends Nette \DI \CompilerExtension
19
20
{
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
-
32
21
/** @var bool */
33
22
private $ debugMode ;
34
23
@@ -39,84 +28,97 @@ public function __construct(bool $debugMode = false)
39
28
}
40
29
41
30
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
+
42
53
public function loadConfiguration ()
43
54
{
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 ;
57
59
$ this ->setupDatabase ($ config , $ name );
58
60
}
59
61
}
60
62
61
63
62
- private function setupDatabase (array $ config , string $ name ): void
64
+ private function setupDatabase (\ stdClass $ config , string $ name ): void
63
65
{
64
66
$ builder = $ this ->getContainerBuilder ();
65
67
66
- foreach (( array ) $ config[ ' options ' ] as $ key => $ value ) {
68
+ foreach ($ config-> options as $ key => $ value ) {
67
69
if (is_string ($ value ) && preg_match ('#^PDO::\w+\z# ' , $ value )) {
68
- $ config[ ' options ' ] [$ key ] = $ value = constant ($ value );
70
+ $ config-> options [$ key ] = $ value = constant ($ value );
69
71
}
70
72
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 ;
73
75
}
74
76
}
75
77
76
78
$ 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 );
79
81
80
82
$ structure = $ builder ->addDefinition ($ this ->prefix ("$ name.structure " ))
81
83
->setFactory (Nette \Database \Structure::class)
82
84
->setArguments ([$ connection ])
83
- ->setAutowired ($ config[ ' autowired ' ] );
85
+ ->setAutowired ($ config-> autowired );
84
86
85
- if (!empty ($ config[ ' reflection ' ] )) {
87
+ if (!empty ($ config-> reflection )) {
86
88
$ 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 ' ;
90
92
}
91
93
} else {
92
94
$ conventionsServiceName = 'conventions ' ;
93
95
}
94
96
95
- if (!$ config[ ' conventions ' ] ) {
97
+ if (!$ config-> conventions ) {
96
98
$ conventions = null ;
97
99
98
- } elseif (is_string ($ config[ ' conventions ' ] )) {
100
+ } elseif (is_string ($ config-> conventions )) {
99
101
$ 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 );
105
107
106
108
} else {
107
109
$ conventions = Nette \DI \Config \Processor::processArguments ([$ config ['conventions ' ]])[0 ];
108
110
}
109
111
110
112
$ builder ->addDefinition ($ this ->prefix ("$ name.context " ))
111
113
->setFactory (Nette \Database \Context::class, [$ connection , $ structure , $ conventions ])
112
- ->setAutowired ($ config[ ' autowired ' ] );
114
+ ->setAutowired ($ config-> autowired );
113
115
114
- if ($ config[ ' debugger ' ] ) {
116
+ if ($ config-> debugger ) {
115
117
$ connection ->addSetup ('@Tracy\BlueScreen::addPanel ' , [
116
118
[Nette \Bridges \DatabaseTracy \ConnectionPanel::class, 'renderException ' ],
117
119
]);
118
120
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 ]);
120
122
}
121
123
}
122
124
0 commit comments