1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace Codeception \Lib ;
4
6
5
7
/**
6
8
* Populates a db using a parameterized command built from the Db module configuration.
7
9
*/
8
10
class DbPopulator
9
11
{
10
- /**
11
- * @var array
12
- */
13
- protected $ config ;
12
+ protected array $ config = [];
14
13
15
- /**
16
- * @var array
17
- */
18
- protected $ commands ;
14
+ protected array $ commands = [];
19
15
20
16
/**
21
17
* Constructs a DbPopulator object for the given command and Db module.
22
18
*
23
- * @param $config
24
19
* @internal param string $command The parameterized command to evaluate and execute later.
25
20
* @internal param Codeception\Module\Db|null $dbModule The Db module used to build the populator command or null.
26
21
*/
27
- public function __construct ($ config )
22
+ public function __construct (array $ config )
28
23
{
29
24
$ this ->config = $ config ;
30
-
31
25
//Convert To Array Format
32
- if (isset ( $ this -> config [ ' dump ' ]) && ! is_array ($ this ->config ['dump ' ])) {
33
- $ this -> config [ ' dump ' ] = [ $ this -> config [ ' dump ' ]] ;
26
+ if (! isset ($ this ->config ['dump ' ])) {
27
+ return ;
34
28
}
29
+
30
+ if (is_array ($ this ->config ['dump ' ])) {
31
+ return ;
32
+ }
33
+
34
+ $ this ->config ['dump ' ] = [$ this ->config ['dump ' ]];
35
35
}
36
36
37
37
/**
@@ -53,15 +53,15 @@ public function __construct($config)
53
53
* @param string|null $dumpFile The dump file to build the command with.
54
54
* @return string The resulting command string after evaluating any configuration's key
55
55
*/
56
- protected function buildCommand ($ command , $ dumpFile = null )
56
+ protected function buildCommand (string $ command , string $ dumpFile = null ): string
57
57
{
58
- $ dsn = isset ( $ this ->config ['dsn ' ]) ? $ this -> config [ ' dsn ' ] : '' ;
58
+ $ dsn = $ this ->config ['dsn ' ] ?? '' ;
59
59
$ dsnVars = [];
60
- $ dsnWithoutDriver = preg_replace ('/ ^[a-z]+:/ i ' , '' , $ dsn );
60
+ $ dsnWithoutDriver = preg_replace ('# ^[a-z]+:# i ' , '' , $ dsn );
61
61
foreach (explode ('; ' , $ dsnWithoutDriver ) as $ item ) {
62
62
$ keyValueTuple = explode ('= ' , $ item );
63
63
if (count ($ keyValueTuple ) > 1 ) {
64
- list ( $ k , $ v) = array_values ($ keyValueTuple );
64
+ [ $ k , $ v] = array_values ($ keyValueTuple );
65
65
$ dsnVars [$ k ] = $ v ;
66
66
}
67
67
}
@@ -79,17 +79,16 @@ protected function buildCommand($command, $dumpFile = null)
79
79
80
80
unset($ vars [$ key ]);
81
81
}
82
+
82
83
return str_replace (array_keys ($ vars ), $ vars , $ command );
83
84
}
84
85
85
86
/**
86
87
* Executes the command built using the Db module configuration.
87
88
*
88
89
* Uses the PHP `exec` to spin off a child process for the built command.
89
- *
90
- * @return bool
91
90
*/
92
- public function run ()
91
+ public function run (): bool
93
92
{
94
93
foreach ($ this ->buildCommands () as $ command ) {
95
94
$ this ->runCommand ($ command );
@@ -98,26 +97,26 @@ public function run()
98
97
return true ;
99
98
}
100
99
101
- private function runCommand ($ command )
100
+ private function runCommand ($ command ): void
102
101
{
103
- codecept_debug ("[Db] Executing Populator: ` $ command` " );
102
+ codecept_debug ("[Db] Executing Populator: ` { $ command} ` " );
104
103
105
104
exec ($ command , $ output , $ exitCode );
106
105
107
106
if (0 !== $ exitCode ) {
108
107
throw new \RuntimeException (
109
108
"The populator command did not end successfully: \n" .
110
- " Exit code: $ exitCode \n" .
109
+ " Exit code: { $ exitCode} \n" .
111
110
" Output: " . implode ("\n" , $ output )
112
111
);
113
112
}
114
113
115
114
codecept_debug ("[Db] Populator Finished. " );
116
115
}
117
116
118
- public function buildCommands ()
117
+ public function buildCommands (): array
119
118
{
120
- if ($ this ->commands !== null ) {
119
+ if ($ this ->commands !== [] ) {
121
120
return $ this ->commands ;
122
121
} elseif (!isset ($ this ->config ['dump ' ]) || $ this ->config ['dump ' ] === false ) {
123
122
return [$ this ->buildCommand ($ this ->config ['populator ' ])];
0 commit comments