1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\DatabaseChecker\Factory; |
4
|
|
|
|
5
|
|
|
use Starkerxp\DatabaseChecker\Structure\MysqlDatabaseColumn; |
6
|
|
|
use Starkerxp\DatabaseChecker\Structure\MysqlDatabaseTable; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Convert json data to array object. |
11
|
|
|
* |
12
|
|
|
* @package Starkerxp\DatabaseChecker\Factory |
13
|
|
|
*/ |
14
|
|
|
class JsonDatabaseFactory |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $json; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* JsonDatabaseFactory constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $json |
26
|
|
|
*/ |
27
|
|
|
public function __construct($json) |
28
|
|
|
{ |
29
|
|
|
$this->json = $json; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return MysqlDatabaseTable[] |
34
|
|
|
* |
35
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException |
36
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException |
37
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException |
38
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException |
39
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
40
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException |
41
|
|
|
* @throws \LogicException |
42
|
|
|
* @throws \RuntimeException |
43
|
|
|
*/ |
44
|
|
|
public function generate() |
45
|
|
|
{ |
46
|
|
|
$tables = []; |
47
|
|
|
$dataTables = $this->resolve(); |
48
|
|
|
foreach ($dataTables as $tableName => $dataTable) { |
49
|
|
|
try { |
50
|
|
|
$table = new MysqlDatabaseTable($tableName); |
51
|
|
|
} catch (\Exception $e) { |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
if (isset($row['collate'])) { |
55
|
|
|
$table->setCollate($dataTable['collate']); |
56
|
|
|
} |
57
|
|
|
foreach ((array)$dataTable['columns'] as $columnName => $row) { |
58
|
|
|
$column = new MysqlDatabaseColumn($columnName, $row['type'], $row['length'], $row['nullable'], $row['defaultValue'], $row['extra']); |
59
|
|
|
if (isset($row['collate'])) { |
60
|
|
|
$column->setCollate($row['collate']); |
61
|
|
|
} |
62
|
|
|
$table->addColumn($column); |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
foreach ((array)$dataTable['indexes'] as $row) { |
66
|
|
|
$table->addIndex($row['columns'], $row['name']); |
67
|
|
|
} |
68
|
|
|
if (isset($dataTable['primary'])) { |
69
|
|
|
$table->addPrimary((array)$dataTable['primary']); |
70
|
|
|
} |
71
|
|
|
foreach ((array)$dataTable['uniques'] as $row) { |
72
|
|
|
$table->addUnique($row['columns'], $row['name']); |
73
|
|
|
} |
74
|
|
|
$tables[] = $table; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $tables; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Check syntax of array. |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
* |
85
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException |
86
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException |
87
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException |
88
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
89
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException |
90
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException |
91
|
|
|
*/ |
92
|
|
|
protected function resolve() |
93
|
|
|
{ |
94
|
|
|
$data = $this->generateJsonData(); |
95
|
|
|
|
96
|
|
|
// On force les valeurs par d�faut. |
97
|
|
|
$resolverTable = new OptionsResolver(); |
98
|
|
|
$resolverTable->setRequired(['columns']); |
99
|
|
|
$resolverTable->setDefaults( |
100
|
|
|
[ |
101
|
|
|
'columns' => [], |
102
|
|
|
'indexes' => [], |
103
|
|
|
'primary' => null, |
104
|
|
|
'uniques' => [], |
105
|
|
|
'collate' => null, |
106
|
|
|
] |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$resolverColumns = new OptionsResolver(); |
110
|
|
|
$resolverColumns->setRequired(['type']); |
111
|
|
|
$resolverColumns->setDefaults( |
112
|
|
|
[ |
113
|
|
|
'length' => null, |
114
|
|
|
'nullable' => false, |
115
|
|
|
'defaultValue' => null, |
116
|
|
|
'extra' => null, |
117
|
|
|
'collate' => null, |
118
|
|
|
] |
119
|
|
|
); |
120
|
|
|
|
121
|
|
|
$resolverIndex = new OptionsResolver(); |
122
|
|
|
$resolverIndex->setDefaults( |
123
|
|
|
[ |
124
|
|
|
'name' => '', |
125
|
|
|
'columns' => [], |
126
|
|
|
] |
127
|
|
|
); |
128
|
|
|
$export = []; |
129
|
|
|
foreach ($data as $nomTable => $table) { |
130
|
|
|
$dataTable = $resolverTable->resolve($table); |
131
|
|
|
foreach ((array)$dataTable['columns'] as $columnName => $column) { |
132
|
|
|
$dataTable['columns'][$columnName] = $resolverColumns->resolve($column); |
133
|
|
|
} |
134
|
|
|
foreach (['indexes', 'uniques'] as $indexKey) { |
135
|
|
|
foreach ((array)$dataTable[$indexKey] as $keyIndex => $index) { |
136
|
|
|
$dataTable[$indexKey][$keyIndex] = $resolverIndex->resolve($index); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
$export[$nomTable] = $dataTable; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $export; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
private function generateJsonData() |
146
|
|
|
{ |
147
|
|
|
if (empty($this->json)) { |
148
|
|
|
return []; |
149
|
|
|
} |
150
|
|
|
if (!$data = json_decode($this->json, true)) { |
151
|
|
|
$data = []; |
152
|
|
|
} |
153
|
|
|
return $data; |
154
|
|
|
} |
155
|
|
|
} |
|
|
|
|
156
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.