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