7
7
8
8
namespace Magento \Developer \Console \Command ;
9
9
10
- use Magento \Framework \ Component \ ComponentRegistrar ;
10
+ use Magento \Developer \ Model \ Setup \ Declaration \ Schema \ WhitelistGenerator ;
11
11
use Magento \Framework \Config \FileResolverByModule ;
12
- use Magento \Framework \Module \Dir ;
13
- use Magento \Framework \Setup \Declaration \Schema \Diff \Diff ;
14
- use Magento \Framework \Setup \JsonPersistor ;
15
- use Magento \Framework \Setup \Declaration \Schema \Declaration \ReaderComposite ;
12
+ use Magento \Framework \Exception \ConfigurationMismatchException ;
16
13
use Symfony \Component \Console \Command \Command ;
17
14
use Symfony \Component \Console \Input \InputInterface ;
18
15
use Symfony \Component \Console \Input \InputOption ;
@@ -31,41 +28,20 @@ class TablesWhitelistGenerateCommand extends Command
31
28
const MODULE_NAME_KEY = 'module-name ' ;
32
29
33
30
/**
34
- * @var ComponentRegistrar
31
+ * @var WhitelistGenerator
35
32
*/
36
- private $ componentRegistrar ;
33
+ private $ whitelistGenerator ;
37
34
38
35
/**
39
- * @var ReaderComposite
40
- */
41
- private $ readerComposite ;
42
-
43
- /**
44
- * @var JsonPersistor
45
- */
46
- private $ jsonPersistor ;
47
-
48
- /**
49
- * @var array
50
- */
51
- private $ primaryDbSchema ;
52
-
53
- /**
54
- * @param ComponentRegistrar $componentRegistrar
55
- * @param ReaderComposite $readerComposite
56
- * @param JsonPersistor $jsonPersistor
36
+ * @param WhitelistGenerator $whitelistGenerator
57
37
* @param string|null $name
58
38
*/
59
39
public function __construct (
60
- ComponentRegistrar $ componentRegistrar ,
61
- ReaderComposite $ readerComposite ,
62
- JsonPersistor $ jsonPersistor ,
40
+ WhitelistGenerator $ whitelistGenerator ,
63
41
$ name = null
64
42
) {
43
+ $ this ->whitelistGenerator = $ whitelistGenerator ;
65
44
parent ::__construct ($ name );
66
- $ this ->componentRegistrar = $ componentRegistrar ;
67
- $ this ->readerComposite = $ readerComposite ;
68
- $ this ->jsonPersistor = $ jsonPersistor ;
69
45
}
70
46
71
47
/**
@@ -93,38 +69,6 @@ protected function configure()
93
69
parent ::configure ();
94
70
}
95
71
96
- /**
97
- * Update whitelist tables for all modules that are enabled on the moment.
98
- *
99
- * @param string $moduleName
100
- * @return void
101
- */
102
- private function persistModule ($ moduleName )
103
- {
104
- $ content = [];
105
- $ modulePath = $ this ->componentRegistrar ->getPath ('module ' , $ moduleName );
106
- $ whiteListFileName = $ modulePath
107
- . DIRECTORY_SEPARATOR
108
- . Dir::MODULE_ETC_DIR
109
- . DIRECTORY_SEPARATOR
110
- . Diff::GENERATED_WHITELIST_FILE_NAME ;
111
- //We need to load whitelist file and update it with new revision of code.
112
- if (file_exists ($ whiteListFileName )) {
113
- $ content = json_decode (file_get_contents ($ whiteListFileName ), true );
114
- }
115
-
116
- $ newContent = $ this ->filterPrimaryTables ($ this ->readerComposite ->read ($ moduleName ));
117
-
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 );
125
- }
126
- }
127
-
128
72
/**
129
73
* @inheritdoc
130
74
*/
@@ -133,77 +77,14 @@ protected function execute(InputInterface $input, OutputInterface $output) : int
133
77
$ moduleName = $ input ->getOption (self ::MODULE_NAME_KEY );
134
78
135
79
try {
136
- if ($ moduleName === FileResolverByModule::ALL_MODULES ) {
137
- foreach (array_keys ($ this ->componentRegistrar ->getPaths ('module ' )) as $ moduleName ) {
138
- $ this ->persistModule ($ moduleName );
139
- }
140
- } else {
141
- $ this ->persistModule ($ moduleName );
142
- }
80
+ $ this ->whitelistGenerator ->generate ($ moduleName );
81
+ } catch (ConfigurationMismatchException $ e ) {
82
+ $ output ->writeln ("<info> " . $ e . "</info> " );
83
+ return \Magento \Framework \Console \Cli::RETURN_FAILURE ;
143
84
} catch (\Exception $ e ) {
144
85
return \Magento \Framework \Console \Cli::RETURN_FAILURE ;
145
86
}
146
87
147
- //If script comes here, that we sucessfully write whitelist configuration
148
88
return \Magento \Framework \Console \Cli::RETURN_SUCCESS ;
149
89
}
150
-
151
- /**
152
- * Filter attribute names
153
- *
154
- * As for whitelist we do not need any specific attributes like nullable or indexType, we need to choose only names.
155
- *
156
- * @param array $content
157
- * @return array
158
- */
159
- private function filterAttributeNames (array $ content ) : array
160
- {
161
- $ 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
- }
173
- }
174
- }
175
-
176
- return $ names ;
177
- }
178
-
179
- /**
180
- * Load db_schema content from the primary scope app/etc/db_schema.xml.
181
- *
182
- * @return array
183
- */
184
- private function getPrimaryDbSchema ()
185
- {
186
- if (!$ this ->primaryDbSchema ) {
187
- $ this ->primaryDbSchema = $ this ->readerComposite ->read ('primary ' );
188
- }
189
- return $ this ->primaryDbSchema ;
190
- }
191
-
192
- /**
193
- * Filter tables from module db_schema.xml as they should not contain the primary system tables.
194
- *
195
- * @param array $moduleDbSchema
196
- * @return array
197
- * @SuppressWarnings(PHPMD.UnusedLocalVariable)
198
- */
199
- private function filterPrimaryTables (array $ moduleDbSchema )
200
- {
201
- $ primaryDbSchema = $ this ->getPrimaryDbSchema ();
202
- if (isset ($ moduleDbSchema ['table ' ]) && isset ($ primaryDbSchema ['table ' ])) {
203
- foreach ($ primaryDbSchema ['table ' ] as $ tableNameKey => $ tableContents ) {
204
- unset($ moduleDbSchema ['table ' ][$ tableNameKey ]);
205
- }
206
- }
207
- return $ moduleDbSchema ;
208
- }
209
90
}
0 commit comments