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
|
2 |
|
public function addTable(Table $table, array $groups = []): void |
15
|
|
|
{ |
16
|
2 |
|
$this->tables[$table->getTableName()] = $table; |
17
|
|
|
|
18
|
2 |
|
foreach ($groups as $group) { |
19
|
|
|
if (!isset($this->groups[$group])) { |
20
|
|
|
$this->groups[$group] = []; |
21
|
|
|
} |
22
|
|
|
$this->groups[$group][] = $table->getTableName(); |
23
|
|
|
} |
24
|
2 |
|
} |
25
|
|
|
|
26
|
2 |
|
public function getSchema(array $groups = []): Schema |
|
|
|
|
27
|
|
|
{ |
28
|
2 |
|
$schema = new Schema(); |
29
|
|
|
|
30
|
2 |
|
foreach ($this->getTables() as $table) { |
31
|
1 |
|
$table->addTableToSchema($schema); |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
return $schema; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
public function getFixtures(array $groups = []): iterable |
38
|
|
|
{ |
39
|
2 |
|
foreach ($this->getTables($groups) 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
|
2 |
|
} |
48
|
|
|
|
49
|
3 |
|
private function getTables(array $groups = []): array |
50
|
|
|
{ |
51
|
|
|
// Check that all groups exists. |
52
|
3 |
|
foreach ($groups as $groups) { |
53
|
|
|
if (!isset($this->groups[$group])) { |
|
|
|
|
54
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown group %s. Available: [%s]', $group, implode(', ', array_keys($this->groups)))); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
3 |
|
if (0 === count($groups)) { |
59
|
3 |
|
$tables = $this->tables; |
60
|
|
|
} else { |
61
|
|
|
$tables = []; |
62
|
|
|
foreach ($groups as $group) { |
63
|
|
|
foreach ($this->groups[$group] as $tableInGroup) { |
64
|
|
|
$tables[] = $this->tables[$tableInGroup]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Order all tables |
70
|
3 |
|
usort($tables, function ($a, $b) { |
71
|
2 |
|
if ($a->getOrder() === $b->getOrder()) { |
72
|
2 |
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
return $a->getOrder() < $b->getOrder() ? -1 : 1; |
76
|
3 |
|
}); |
77
|
|
|
|
78
|
3 |
|
return $tables; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.