|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Webino (http://webino.sk) |
|
4
|
|
|
* |
|
5
|
|
|
* @link https://github.com/webino/WebinoDbDump/ for the canonical source repository |
|
6
|
|
|
* @copyright Copyright (c) 2014-2017 Webino, s. r. o. (http://webino.sk) |
|
7
|
|
|
* @author Peter Bačinský <[email protected]> |
|
8
|
|
|
* @license BSD-3-Clause |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace WebinoDbDump\Db\Dump; |
|
12
|
|
|
|
|
13
|
|
|
use SplFileObject as File; |
|
14
|
|
|
use WebinoDbDump\Db\Dump\Platform\PlatformInterface; |
|
15
|
|
|
use WebinoDbDump\Db\Sql\SqlFile; |
|
16
|
|
|
use WebinoDbDump\Db\Sql\SqlInterface; |
|
17
|
|
|
use WebinoDbDump\Exception; |
|
18
|
|
|
use WebinoDbDump\Module; |
|
19
|
|
|
use Zend\Db\Adapter\AdapterInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Database dump utility |
|
23
|
|
|
*/ |
|
24
|
|
|
class Dump implements DumpInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* SQL chunk size |
|
28
|
|
|
*/ |
|
29
|
|
|
const MAX_SQL_SIZE = 1e6; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Adapter |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $adapter; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var PlatformInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $dumpPlatform; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param AdapterInterface|Adapter $adapter |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct($adapter) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->setAdapter($adapter); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param AdapterInterface|Adapter $adapter |
|
51
|
|
|
* @return $this |
|
52
|
|
|
* @throws Exception\InvalidArgumentException |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function setAdapter($adapter) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($adapter instanceof AdapterInterface) { |
|
57
|
|
|
$adapter = new Adapter($adapter); |
|
58
|
|
|
|
|
59
|
|
|
} elseif (!($adapter instanceof Adapter)) { |
|
60
|
|
|
throw new Exception\InvalidArgumentException('Expected Db\Adapter or Dump\Adapter'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->adapter = $adapter; |
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return PlatformInterface |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getDumpPlatform() |
|
71
|
|
|
{ |
|
72
|
|
|
if (null === $this->dumpPlatform) { |
|
73
|
|
|
$this->setDumpPlatform(); |
|
74
|
|
|
} |
|
75
|
|
|
return $this->dumpPlatform; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param PlatformInterface $dumpPlatform |
|
80
|
|
|
* @return $this |
|
81
|
|
|
*/ |
|
82
|
|
|
public function setDumpPlatform(PlatformInterface $dumpPlatform = null) |
|
83
|
|
|
{ |
|
84
|
|
|
if (null === $dumpPlatform) { |
|
85
|
|
|
$parts = explode('\\', get_class($this->adapter->getPlatform())); |
|
86
|
|
|
$platformName = end($parts); |
|
87
|
|
|
$platformClass = __NAMESPACE__ . '\Platform\\' . $platformName . '\\' . $platformName; |
|
88
|
|
|
$dumpPlatform = new $platformClass($this->adapter); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$this->dumpPlatform = $dumpPlatform; |
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Return new file object |
|
97
|
|
|
* |
|
98
|
|
|
* Prepend file path with `compress.zlib://` wrapper to use compression. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $filePath |
|
101
|
|
|
* @return File |
|
102
|
|
|
* @throws Exception\RuntimeException |
|
103
|
|
|
*/ |
|
104
|
|
|
public function createOutputFile($filePath) |
|
105
|
|
|
{ |
|
106
|
|
|
try { |
|
107
|
|
|
return new File($filePath, 'wb'); |
|
108
|
|
|
} catch (\Exception $exc) { |
|
109
|
|
|
throw new Exception\RuntimeException( |
|
110
|
|
|
sprintf('Can\'t open file for writing `%s`', $filePath), |
|
111
|
|
|
$exc->getCode(), |
|
112
|
|
|
$exc |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Return new sql file object |
|
119
|
|
|
* |
|
120
|
|
|
* Prepend file path with `compress.zlib://` wrapper to use compression. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $filePath |
|
123
|
|
|
* @return SqlFile |
|
124
|
|
|
* @throws Exception\RuntimeException |
|
125
|
|
|
*/ |
|
126
|
|
|
public function createInputFile($filePath) |
|
127
|
|
|
{ |
|
128
|
|
|
try { |
|
129
|
|
|
return new SqlFile($filePath, 'rb'); |
|
130
|
|
|
} catch (\Exception $exc) { |
|
131
|
|
|
throw new Exception\RuntimeException( |
|
132
|
|
|
sprintf('Can\'t open file for reading `%s`', $filePath), |
|
133
|
|
|
$exc->getCode(), |
|
134
|
|
|
$exc |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $filePath |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
|
|
public function save($filePath) |
|
144
|
|
|
{ |
|
145
|
|
|
$file = $this->createOutputFile($filePath); |
|
146
|
|
|
$this->write($file); |
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param File $file |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function write(File $file) |
|
155
|
|
|
{ |
|
156
|
|
|
$file->fwrite('-- WebinoDbDump ' . Module::VERSION . PHP_EOL . PHP_EOL); |
|
157
|
|
|
|
|
158
|
|
|
$dumpPlatform = $this->getDumpPlatform(); |
|
159
|
|
|
$file->fwrite($dumpPlatform->header()); |
|
160
|
|
|
|
|
161
|
|
|
foreach ($dumpPlatform->showTables() as $table) { |
|
162
|
|
|
$dumpPlatform->createTable($table)->write($file); |
|
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
$file->fwrite($dumpPlatform->footer()); |
|
166
|
|
|
$file->fwrite('-- ' . date('Y-m-d H:i:s') . PHP_EOL); |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @param string $filePath |
|
173
|
|
|
* @return $this |
|
174
|
|
|
*/ |
|
175
|
|
|
public function load($filePath) |
|
176
|
|
|
{ |
|
177
|
|
|
$file = $this->createInputFile($filePath); |
|
178
|
|
|
$this->read($file); |
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param SqlInterface $file |
|
184
|
|
|
* @return $this |
|
185
|
|
|
*/ |
|
186
|
|
|
public function read(SqlInterface $file) |
|
187
|
|
|
{ |
|
188
|
|
|
foreach ($file as $sql) { |
|
189
|
|
|
$this->adapter->executeQuery($sql); |
|
190
|
|
|
} |
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.