|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of graze/sprout. |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright © 2018 Nature Delivered Ltd. <https://www.graze.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license https://github.com/graze/sprout/blob/master/LICENSE.md |
|
11
|
|
|
* @link https://github.com/graze/sprout |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Graze\Sprout\Dump; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\Sprout\Config\SchemaConfigInterface; |
|
17
|
|
|
use League\Flysystem\AdapterInterface; |
|
18
|
|
|
use League\Flysystem\Config; |
|
19
|
|
|
use RuntimeException; |
|
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
21
|
|
|
|
|
22
|
|
|
class Dumper |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var SchemaConfigInterface */ |
|
25
|
|
|
private $schemaConfig; |
|
26
|
|
|
/** @var OutputInterface */ |
|
27
|
|
|
private $output; |
|
28
|
|
|
/** @var TableDumperFactory */ |
|
29
|
|
|
private $factory; |
|
30
|
|
|
/** @var AdapterInterface */ |
|
31
|
|
|
private $filesystem; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Dumper constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param SchemaConfigInterface $schemaConfig |
|
37
|
|
|
* @param OutputInterface $output |
|
38
|
|
|
* @param TableDumperFactory $factory |
|
39
|
|
|
* @param AdapterInterface $filesystem |
|
40
|
|
|
*/ |
|
41
|
5 |
|
public function __construct( |
|
42
|
|
|
SchemaConfigInterface $schemaConfig, |
|
43
|
|
|
OutputInterface $output, |
|
44
|
|
|
TableDumperFactory $factory, |
|
45
|
|
|
AdapterInterface $filesystem |
|
46
|
|
|
) { |
|
47
|
5 |
|
$this->schemaConfig = $schemaConfig; |
|
48
|
5 |
|
$this->output = $output; |
|
49
|
5 |
|
$this->factory = $factory; |
|
50
|
5 |
|
$this->filesystem = $filesystem; |
|
51
|
5 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Dump a collection of tables to disk |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $path |
|
57
|
|
|
* @param string[] $tables |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function dump(string $path, array $tables = []) |
|
60
|
|
|
{ |
|
61
|
5 |
|
$tables = array_unique($tables); |
|
62
|
|
|
|
|
63
|
5 |
|
if (count($tables) === 0) { |
|
64
|
1 |
|
$this->output->writeln('<warning>No tables specified, nothing to do</warning>'); |
|
65
|
1 |
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
4 |
|
if ($this->filesystem->createDir($path, new Config()) === false) { |
|
69
|
1 |
|
throw new RuntimeException('dump: failed to create directory: ' . $path); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
$tableDumper = $this->factory->getDumper($this->schemaConfig->getConnection()); |
|
73
|
3 |
|
$schema = $this->schemaConfig->getSchema(); |
|
74
|
|
|
|
|
75
|
3 |
|
foreach ($tables as $table) { |
|
76
|
3 |
|
$file = sprintf('%s/%s.sql', $path, $table); |
|
77
|
3 |
|
$tableDumper->dump($schema, $table, $file); |
|
78
|
|
|
} |
|
79
|
3 |
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|