1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
namespace Codeception\Lib;
/**
* Populates a db using a parameterized command built from the Db module configuration.
*/
class DbPopulator
{
/**
* The command to be executed.
*
* @var string
*/
private $builtCommand;
/**
* @var array
*/
protected $config;
/**
* Constructs a DbPopulator object for the given command and Db module.
*
* @param $config
* @internal param string $command The parameterized command to evaluate and execute later.
* @internal param Codeception\Module\Db|null $dbModule The Db module used to build the populator command or null.
*/
public function __construct($config)
{
$this->config = $config;
$command = $this->config['populator'];
$this->builtCommand = $this->buildCommand((string) $command);
}
/**
* Builds out a command replacing any found `$key` with its value if found in the given configuration.
*
* Process any $key found in the configuration array as a key of the array and replaces it with
* the found value for the key. Example:
*
* ```php
* <?php
*
* $command = 'Hello $name';
* $config = ['name' => 'Mauro'];
*
* // With the above parameters it will return `'Hello Mauro'`.
* ```
*
* @param string $command The command to be evaluated using the given config
* @param array $config The configuration values used to replace any found $keys with values from this array.
* @return string The resulting command string after evaluating any configuration's key
*/
protected function buildCommand($command)
{
$dsn = isset($this->config['dsn']) ? $this->config['dsn'] : '';
$dsnVars = [];
$dsnWithoutDriver = preg_replace('/^[a-z]+:/i', '', $dsn);
foreach (explode(';', $dsnWithoutDriver) as $item) {
$keyValueTuple = explode('=', $item);
if (count($keyValueTuple) > 1) {
list($k, $v) = array_values($keyValueTuple);
$dsnVars[$k] = $v;
}
}
$vars = array_merge($dsnVars, $this->config);
foreach ($vars as $key => $value) {
$vars['$'.$key] = $value;
unset($vars[$key]);
}
return str_replace(array_keys($vars), array_values($vars), $command);
}
/**
* Executes the command built using the Db module configuration.
*
* Uses the PHP `exec` to spin off a child process for the built command.
*
* @return bool
*/
public function run()
{
$command = $this->getBuiltCommand();
codecept_debug("[Db] Executing Populator: `$command`");
exec($command, $output, $exitCode);
if (0 !== $exitCode) {
throw new \RuntimeException(
"The populator command did not end successfully: \n" .
" Exit code: $exitCode \n" .
" Output:" . implode("\n", $output)
);
}
codecept_debug("[Db] Populator Finished.");
return true;
}
public function getBuiltCommand()
{
return $this->builtCommand;
}
}