1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Velikonja\LabbyBundle\Database\Mysql; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
6
|
|
|
use Symfony\Component\Process\ProcessBuilder; |
7
|
|
|
use Velikonja\LabbyBundle\Database\DatabaseException; |
8
|
|
|
use Velikonja\LabbyBundle\Database\ImporterInterface; |
9
|
|
|
|
10
|
|
|
class MySqlImporter implements ImporterInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \Symfony\Component\Process\ProcessBuilder |
14
|
|
|
*/ |
15
|
|
|
private $processBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $executable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param array $options |
24
|
|
|
* @param int $timeout |
25
|
|
|
* @param null|string $executable |
26
|
|
|
* @param null|ProcessBuilder $processBuilder |
27
|
|
|
*/ |
28
|
6 |
|
public function __construct( |
29
|
|
|
array $options, |
30
|
|
|
$timeout = 60, |
31
|
|
|
$executable = null, |
32
|
|
|
ProcessBuilder $processBuilder = null |
33
|
|
|
) { |
34
|
6 |
|
if (! $executable) { |
35
|
6 |
|
$executable = '/usr/bin/mysql'; |
36
|
|
|
} |
37
|
|
|
|
38
|
6 |
|
$this->executable = $executable; |
39
|
|
|
|
40
|
6 |
|
if (! $processBuilder) { |
41
|
6 |
|
$processBuilder = new ProcessBuilder(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$processBuilder |
45
|
6 |
|
->setTimeout($timeout) |
46
|
6 |
|
->setPrefix($this->executable) |
47
|
6 |
|
->setArguments(array( |
48
|
6 |
|
'--user=' . $options['user'], |
49
|
6 |
|
'--password=' . $options['password'], |
50
|
6 |
|
'--host=' . $options['host'], |
51
|
6 |
|
'--database=' . $options['dbname'], |
52
|
|
|
)); |
53
|
|
|
|
54
|
6 |
|
$this->processBuilder = $processBuilder; |
55
|
|
|
|
56
|
6 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Import dump to database. |
60
|
|
|
* |
61
|
|
|
* @param string $file |
62
|
|
|
* @param null|callable $callback |
63
|
|
|
* |
64
|
|
|
* @throws DatabaseException |
65
|
|
|
* |
66
|
|
|
* @return void |
67
|
|
|
*/ |
68
|
5 |
|
public function import($file, $callback = null) |
69
|
|
|
{ |
70
|
5 |
|
$process = $this->processBuilder->getProcess(); |
71
|
|
|
|
72
|
5 |
|
$dump = file_get_contents($file); |
73
|
|
|
|
74
|
5 |
|
if (method_exists($process, 'setInput')) { |
75
|
5 |
|
$process->setInput($dump); |
76
|
|
|
} else { |
77
|
|
|
// support for SF2.3 |
78
|
|
|
$process->setStdin($dump); |
79
|
|
|
} |
80
|
|
|
|
81
|
5 |
|
if ($callback) { |
82
|
3 |
|
call_user_func($callback, Process::OUT, 'Running shell command: ' . $process->getCommandLine()); |
83
|
|
|
} |
84
|
|
|
|
85
|
5 |
|
$process->run($callback); |
86
|
|
|
|
87
|
5 |
|
if (! $process->isSuccessful()) { |
88
|
|
|
throw new DatabaseException($process->getErrorOutput()); |
89
|
|
|
} |
90
|
5 |
|
} |
91
|
|
|
} |
92
|
|
|
|