|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Shapin\Datagen\DBAL; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
8
|
|
|
|
|
9
|
|
|
class Loader |
|
10
|
|
|
{ |
|
11
|
|
|
private $tables = []; |
|
12
|
|
|
private $groups = []; |
|
13
|
|
|
|
|
14
|
7 |
|
public function addTable(Table $table, array $groups = []): void |
|
15
|
|
|
{ |
|
16
|
7 |
|
$this->tables[$table->getTableName()] = $table; |
|
17
|
|
|
|
|
18
|
7 |
|
foreach ($groups as $group) { |
|
19
|
7 |
|
if (!isset($this->groups[$group])) { |
|
20
|
7 |
|
$this->groups[$group] = []; |
|
21
|
|
|
} |
|
22
|
7 |
|
if (!in_array($table->getTableName(), $this->groups[$group])) { |
|
23
|
7 |
|
$this->groups[$group][] = $table->getTableName(); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
7 |
|
} |
|
27
|
|
|
|
|
28
|
8 |
|
public function getSchema(array $groups = [], array $excludeGroups = []): Schema |
|
29
|
|
|
{ |
|
30
|
8 |
|
$schema = new Schema(); |
|
31
|
|
|
|
|
32
|
8 |
|
foreach ($this->getTables($groups, $excludeGroups) as $table) { |
|
33
|
3 |
|
$table->addTableToSchema($schema); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
6 |
|
return $schema; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
3 |
|
public function getFixtures(array $groups = [], array $excludeGroups = []): iterable |
|
40
|
|
|
{ |
|
41
|
3 |
|
foreach ($this->getTables($groups, $excludeGroups) as $table) { |
|
42
|
1 |
|
$tableName = $table->getTableName(); |
|
43
|
1 |
|
$types = $table->getTypes(); |
|
44
|
|
|
|
|
45
|
1 |
|
foreach ($table->getRows() as $row) { |
|
46
|
1 |
|
yield [$tableName, $row, $types]; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
3 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function getGroups(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->groups; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
10 |
|
private function getTables(array $groups, array $excludeGroups): array |
|
57
|
|
|
{ |
|
58
|
10 |
|
$duplicatedGroups = array_intersect($groups, $excludeGroups); |
|
59
|
10 |
|
if (0 < count($duplicatedGroups)) { |
|
60
|
1 |
|
throw new \InvalidArgumentException(sprintf('You can\'t both select & ignore a given group. Errored: ["%s"]', implode('", "', $duplicatedGroups))); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// Check that all groups exists. |
|
64
|
9 |
|
foreach ($groups + $excludeGroups as $group) { |
|
65
|
4 |
|
if (!isset($this->groups[$group])) { |
|
66
|
1 |
|
throw new \InvalidArgumentException(sprintf('Unknown group "%s". Available: ["%s"]', $group, implode('", "', array_keys($this->groups)))); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
// Select all relevant tables according to asked groups |
|
71
|
8 |
|
if (0 === count($groups)) { |
|
72
|
7 |
|
$tables = $this->tables; |
|
73
|
|
|
} else { |
|
74
|
1 |
|
$tables = []; |
|
75
|
1 |
|
foreach ($groups as $group) { |
|
76
|
1 |
|
foreach ($this->groups[$group] as $tableInGroup) { |
|
77
|
1 |
|
$tables[$tableInGroup] = $this->tables[$tableInGroup]; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
// Remove all tables to exclude |
|
83
|
8 |
|
foreach ($excludeGroups as $excludeGroup) { |
|
84
|
2 |
|
foreach ($this->groups[$excludeGroup] as $tableToExclude) { |
|
85
|
2 |
|
if (array_key_exists($tableToExclude, $tables)) { |
|
86
|
2 |
|
unset($tables[$tableToExclude]); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// Order all tables |
|
92
|
8 |
|
usort($tables, function ($a, $b) { |
|
93
|
4 |
|
if ($a->getOrder() === $b->getOrder()) { |
|
94
|
3 |
|
return 0; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
return $a->getOrder() < $b->getOrder() ? -1 : 1; |
|
98
|
8 |
|
}); |
|
99
|
|
|
|
|
100
|
8 |
|
return $tables; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|