10
10
use Magento \Framework \Component \ComponentRegistrar ;
11
11
use Magento \Framework \Config \FileResolverByModule ;
12
12
use Magento \Framework \Module \Dir ;
13
+ use Magento \Framework \Setup \Declaration \Schema \Declaration \SchemaBuilder ;
13
14
use Magento \Framework \Setup \Declaration \Schema \Diff \Diff ;
15
+ use Magento \Framework \Setup \Declaration \Schema \Dto \Schema ;
16
+ use Magento \Framework \Setup \Declaration \Schema \Dto \SchemaFactory ;
14
17
use Magento \Framework \Setup \JsonPersistor ;
15
18
use Magento \Framework \Setup \Declaration \Schema \Declaration \ReaderComposite ;
16
19
use Symfony \Component \Console \Command \Command ;
@@ -50,22 +53,38 @@ class TablesWhitelistGenerateCommand extends Command
50
53
*/
51
54
private $ primaryDbSchema ;
52
55
56
+ /**
57
+ * @var SchemaFactory
58
+ */
59
+ private $ schemaFactory ;
60
+
61
+ /**
62
+ * @var SchemaBuilder
63
+ */
64
+ private $ schemaBuilder ;
65
+
53
66
/**
54
67
* @param ComponentRegistrar $componentRegistrar
55
68
* @param ReaderComposite $readerComposite
56
69
* @param JsonPersistor $jsonPersistor
70
+ * @param SchemaFactory $schemaFactory
71
+ * @param SchemaBuilder $schemaBuilder
57
72
* @param string|null $name
58
73
*/
59
74
public function __construct (
60
75
ComponentRegistrar $ componentRegistrar ,
61
76
ReaderComposite $ readerComposite ,
62
77
JsonPersistor $ jsonPersistor ,
78
+ SchemaFactory $ schemaFactory ,
79
+ SchemaBuilder $ schemaBuilder ,
63
80
$ name = null
64
81
) {
65
82
parent ::__construct ($ name );
66
83
$ this ->componentRegistrar = $ componentRegistrar ;
67
84
$ this ->readerComposite = $ readerComposite ;
68
85
$ this ->jsonPersistor = $ jsonPersistor ;
86
+ $ this ->schemaFactory = $ schemaFactory ;
87
+ $ this ->schemaBuilder = $ schemaBuilder ;
69
88
}
70
89
71
90
/**
@@ -98,6 +117,7 @@ protected function configure()
98
117
*
99
118
* @param string $moduleName
100
119
* @return void
120
+ * @throws \Magento\Framework\Setup\Exception
101
121
*/
102
122
private function persistModule ($ moduleName )
103
123
{
@@ -113,15 +133,20 @@ private function persistModule($moduleName)
113
133
$ content = json_decode (file_get_contents ($ whiteListFileName ), true );
114
134
}
115
135
116
- $ newContent = $ this ->filterPrimaryTables ($ this ->readerComposite ->read ($ moduleName ));
136
+ $ schema = $ this ->schemaFactory ->create ();
137
+ $ data = $ this ->filterPrimaryTables ($ this ->readerComposite ->read ($ moduleName ));
138
+ if (isset ($ data ['table ' ])) {
139
+ $ this ->schemaBuilder ->addTablesData ($ data ['table ' ]);
140
+ $ schema = $ this ->schemaBuilder ->build ($ schema );
117
141
118
- //Do merge between what we have before, and what we have now and filter to only certain attributes.
119
- $ content = array_replace_recursive (
120
- $ content ,
121
- $ this ->filterAttributeNames ($ newContent )
122
- );
123
- if (!empty ($ content )) {
124
- $ this ->jsonPersistor ->persist ($ content , $ whiteListFileName );
142
+ //Do merge between what we have before, and what we have now and filter to only certain attributes.
143
+ $ content = array_replace_recursive (
144
+ $ content ,
145
+ $ this ->getDeclaredContent ($ schema )
146
+ );
147
+ if (!empty ($ content )) {
148
+ $ this ->jsonPersistor ->persist ($ content , $ whiteListFileName );
149
+ }
125
150
}
126
151
}
127
152
@@ -149,27 +174,30 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
149
174
}
150
175
151
176
/**
152
- * Filter attribute names
177
+ * Convert Schema into a whitelist structure.
153
178
*
154
179
* As for whitelist we do not need any specific attributes like nullable or indexType, we need to choose only names.
155
180
*
156
- * @param array $content
181
+ * @param Schema $schema
157
182
* @return array
158
183
*/
159
- private function filterAttributeNames ( array $ content ) : array
184
+ private function getDeclaredContent ( Schema $ schema ) : array
160
185
{
161
186
$ names = [];
162
- $ types = ['column ' , 'index ' , 'constraint ' ];
163
-
164
- foreach ($ content ['table ' ] as $ tableName => $ tableContent ) {
165
- foreach ($ types as $ type ) {
166
- if (isset ($ tableContent [$ type ])) {
167
- //Add elements to whitelist
168
- foreach (array_keys ($ tableContent [$ type ]) as $ elementName ) {
169
- //Depends on flag column will be whitelisted or not
170
- $ names [$ tableName ][$ type ][$ elementName ] = true ;
171
- }
172
- }
187
+ foreach ($ schema ->getTables () as $ tableName => $ table ) {
188
+ $ columns = array_keys ($ table ->getColumns ());
189
+ if ($ columns ) {
190
+ $ names [$ tableName ]['column ' ] = array_fill_keys ($ columns , true );
191
+ }
192
+
193
+ $ indexes = array_keys ($ table ->getIndexes ());
194
+ if ($ indexes ) {
195
+ $ names [$ tableName ]['index ' ] = array_fill_keys ($ indexes , true );
196
+ }
197
+
198
+ $ constraints = array_keys ($ table ->getConstraints ());
199
+ if ($ constraints ) {
200
+ $ names [$ tableName ]['constraint ' ] = array_fill_keys ($ constraints , true );
173
201
}
174
202
}
175
203
0 commit comments