|
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\Seed; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\Sprout\Config\SchemaConfigInterface; |
|
17
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
|
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
19
|
|
|
|
|
20
|
|
|
class Seeder |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var SchemaConfigInterface */ |
|
23
|
|
|
private $schemaConfig; |
|
24
|
|
|
/** @var OutputInterface */ |
|
25
|
|
|
private $output; |
|
26
|
|
|
/** @var TableSeederFactory */ |
|
27
|
|
|
private $factory; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Dumper constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param SchemaConfigInterface $schemaConfig |
|
33
|
|
|
* @param OutputInterface $output |
|
34
|
|
|
* @param TableSeederFactory $factory |
|
35
|
|
|
*/ |
|
36
|
4 |
|
public function __construct( |
|
37
|
|
|
SchemaConfigInterface $schemaConfig, |
|
38
|
|
|
OutputInterface $output, |
|
39
|
|
|
TableSeederFactory $factory |
|
40
|
|
|
) { |
|
41
|
4 |
|
$this->schemaConfig = $schemaConfig; |
|
42
|
4 |
|
$this->output = $output; |
|
43
|
4 |
|
$this->factory = $factory; |
|
44
|
4 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Seed a collection of files to tables |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @param string[] $tables |
|
51
|
|
|
*/ |
|
52
|
4 |
|
public function seed(string $path, array $tables = []) |
|
53
|
|
|
{ |
|
54
|
4 |
|
$tables = array_unique($tables); |
|
55
|
|
|
|
|
56
|
4 |
|
if (count($tables) === 0) { |
|
57
|
1 |
|
$this->output->writeln('<warning>No tables specified, nothing to do</warning>'); |
|
58
|
1 |
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
$tableSeeder = $this->factory->getSeeder($this->schemaConfig->getConnection()); |
|
62
|
3 |
|
$schema = $this->schemaConfig->getSchema(); |
|
63
|
|
|
|
|
64
|
3 |
|
foreach ($tables as $table) { |
|
65
|
3 |
|
$file = sprintf('%s/%s.sql', $path, $table); |
|
66
|
3 |
|
$tableSeeder->seed($file, $schema, $table); |
|
67
|
|
|
} |
|
68
|
3 |
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|