6
6
7
7
namespace Magento \Setup \Model ;
8
8
9
+ use Magento \Framework \App \ObjectManager ;
9
10
use Magento \Framework \Config \Data \ConfigData ;
11
+ use Magento \Framework \Config \Data \ConfigDataFactory ;
10
12
use Magento \Framework \Config \File \ConfigFilePool ;
11
- use Magento \Framework \Math \Random ;
12
13
use Magento \Framework \App \DeploymentConfig ;
13
14
use Magento \Framework \Config \ConfigOptionsListConstants ;
14
15
use Magento \Framework \App \State ;
15
- use Magento \Framework \App \ ObjectManagerFactory ;
16
+ use Magento \Framework \Math \ Random ;
16
17
17
18
/**
18
19
* Creates deployment config data based on user input array
@@ -26,39 +27,58 @@ class ConfigGenerator
26
27
* @var array
27
28
*/
28
29
private static $ paramMap = [
29
- ConfigOptionsListConstants::INPUT_KEY_DB_HOST => ConfigOptionsListConstants::KEY_HOST ,
30
- ConfigOptionsListConstants::INPUT_KEY_DB_NAME => ConfigOptionsListConstants::KEY_NAME ,
31
- ConfigOptionsListConstants::INPUT_KEY_DB_USER => ConfigOptionsListConstants::KEY_USER ,
32
- ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD => ConfigOptionsListConstants::KEY_PASSWORD ,
33
- ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX => ConfigOptionsListConstants::KEY_PREFIX ,
34
- ConfigOptionsListConstants::INPUT_KEY_DB_MODEL => ConfigOptionsListConstants::KEY_MODEL ,
35
- ConfigOptionsListConstants::INPUT_KEY_DB_ENGINE => ConfigOptionsListConstants::KEY_ENGINE ,
30
+ ConfigOptionsListConstants::INPUT_KEY_DB_HOST => ConfigOptionsListConstants::KEY_HOST ,
31
+ ConfigOptionsListConstants::INPUT_KEY_DB_NAME => ConfigOptionsListConstants::KEY_NAME ,
32
+ ConfigOptionsListConstants::INPUT_KEY_DB_USER => ConfigOptionsListConstants::KEY_USER ,
33
+ ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD => ConfigOptionsListConstants::KEY_PASSWORD ,
34
+ ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX => ConfigOptionsListConstants::KEY_PREFIX ,
35
+ ConfigOptionsListConstants::INPUT_KEY_DB_MODEL => ConfigOptionsListConstants::KEY_MODEL ,
36
+ ConfigOptionsListConstants::INPUT_KEY_DB_ENGINE => ConfigOptionsListConstants::KEY_ENGINE ,
36
37
ConfigOptionsListConstants::INPUT_KEY_DB_INIT_STATEMENTS => ConfigOptionsListConstants::KEY_INIT_STATEMENTS ,
37
- ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY => ConfigOptionsListConstants::KEY_ENCRYPTION_KEY ,
38
- ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE => ConfigOptionsListConstants::KEY_SAVE ,
39
- ConfigOptionsListConstants::INPUT_KEY_RESOURCE => ConfigOptionsListConstants::KEY_RESOURCE ,
38
+ ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY => ConfigOptionsListConstants::KEY_ENCRYPTION_KEY ,
39
+ ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE => ConfigOptionsListConstants::KEY_SAVE ,
40
+ ConfigOptionsListConstants::INPUT_KEY_RESOURCE => ConfigOptionsListConstants::KEY_RESOURCE ,
40
41
];
41
42
43
+ /**
44
+ * @var DeploymentConfig
45
+ */
46
+ protected $ deploymentConfig ;
47
+
42
48
/**
43
49
* @var Random
50
+ * @deprecated since 100.2.0
44
51
*/
45
52
protected $ random ;
46
53
47
54
/**
48
- * @var DeploymentConfig
55
+ * @var ConfigDataFactory
49
56
*/
50
- protected $ deploymentConfig ;
57
+ private $ configDataFactory ;
58
+
59
+ /**
60
+ * @var CryptKeyGeneratorInterface
61
+ */
62
+ private $ cryptKeyGenerator ;
51
63
52
64
/**
53
65
* Constructor
54
66
*
55
- * @param Random $random
67
+ * @param Random $random Deprecated since 100.2.0
56
68
* @param DeploymentConfig $deploymentConfig
69
+ * @param ConfigDataFactory|null $configDataFactory
70
+ * @param CryptKeyGeneratorInterface|null $cryptKeyGenerator
57
71
*/
58
- public function __construct (Random $ random , DeploymentConfig $ deploymentConfig )
59
- {
72
+ public function __construct (
73
+ Random $ random ,
74
+ DeploymentConfig $ deploymentConfig ,
75
+ ConfigDataFactory $ configDataFactory = null ,
76
+ CryptKeyGeneratorInterface $ cryptKeyGenerator = null
77
+ ) {
60
78
$ this ->random = $ random ;
61
79
$ this ->deploymentConfig = $ deploymentConfig ;
80
+ $ this ->configDataFactory = $ configDataFactory ?? ObjectManager::getInstance ()->get (ConfigDataFactory::class);
81
+ $ this ->cryptKeyGenerator = $ cryptKeyGenerator ?? ObjectManager::getInstance ()->get (CryptKeyGenerator::class);
62
82
}
63
83
64
84
/**
@@ -70,23 +90,17 @@ public function createCryptConfig(array $data)
70
90
{
71
91
$ currentKey = $ this ->deploymentConfig ->get (ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY );
72
92
73
- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
74
- if (isset ($ data [ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY ])) {
75
- if ($ currentKey !== null ) {
76
- $ key = $ currentKey . "\n" . $ data [ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY ];
77
- } else {
78
- $ key = $ data [ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY ];
79
- }
93
+ $ configData = $ this ->configDataFactory ->create (ConfigFilePool::APP_ENV );
80
94
81
- $ configData -> set (ConfigOptionsListConstants:: CONFIG_PATH_CRYPT_KEY , $ key );
82
- } else {
83
- if ( $ currentKey === null ) {
84
- $ configData -> set (
85
- ConfigOptionsListConstants:: CONFIG_PATH_CRYPT_KEY ,
86
- md5 ( $ this -> random -> getRandomString (ConfigOptionsListConstants:: STORE_KEY_RANDOM_STRING_SIZE ))
87
- );
88
- }
89
- }
95
+ // Use given key if set, else use current
96
+ $ key = $ data [ConfigOptionsListConstants:: INPUT_KEY_ENCRYPTION_KEY ] ?? $ currentKey ;
97
+
98
+ // If there is no key given or currently set, generate a new one
99
+ $ key = $ key ?? $ this -> cryptKeyGenerator -> generate ();
100
+
101
+ // Chaining of ".. ?? .." is not used to keep it simpler to understand
102
+
103
+ $ configData -> set (ConfigOptionsListConstants:: CONFIG_PATH_CRYPT_KEY , $ key );
90
104
91
105
return $ configData ;
92
106
}
@@ -99,7 +113,7 @@ public function createCryptConfig(array $data)
99
113
*/
100
114
public function createSessionConfig (array $ data )
101
115
{
102
- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
116
+ $ configData = $ this -> configDataFactory -> create (ConfigFilePool::APP_ENV );
103
117
104
118
if (isset ($ data [ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE ])) {
105
119
$ configData ->set (
@@ -132,7 +146,7 @@ public function createDefinitionsConfig(array $data)
132
146
*/
133
147
public function createDbConfig (array $ data )
134
148
{
135
- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
149
+ $ configData = $ this -> configDataFactory -> create (ConfigFilePool::APP_ENV );
136
150
137
151
$ optional = [
138
152
ConfigOptionsListConstants::INPUT_KEY_DB_HOST ,
@@ -151,25 +165,18 @@ public function createDbConfig(array $data)
151
165
);
152
166
}
153
167
168
+ $ dbConnectionPrefix = ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/ ' ;
169
+
154
170
foreach ($ optional as $ key ) {
155
171
if (isset ($ data [$ key ])) {
156
- $ configData ->set (
157
- ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/ ' . self ::$ paramMap [$ key ],
158
- $ data [$ key ]
159
- );
172
+ $ configData ->set ($ dbConnectionPrefix . self ::$ paramMap [$ key ], $ data [$ key ]);
160
173
}
161
174
}
162
175
163
- $ currentStatus = $ this ->deploymentConfig ->get (
164
- ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/ ' . ConfigOptionsListConstants::KEY_ACTIVE
165
- );
176
+ $ currentStatus = $ this ->deploymentConfig ->get ($ dbConnectionPrefix . ConfigOptionsListConstants::KEY_ACTIVE );
166
177
167
178
if ($ currentStatus === null ) {
168
- $ configData ->set (
169
- ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT
170
- . '/ ' . ConfigOptionsListConstants::KEY_ACTIVE ,
171
- '1 '
172
- );
179
+ $ configData ->set ($ dbConnectionPrefix . ConfigOptionsListConstants::KEY_ACTIVE , '1 ' );
173
180
}
174
181
175
182
return $ configData ;
@@ -182,7 +189,7 @@ public function createDbConfig(array $data)
182
189
*/
183
190
public function createResourceConfig ()
184
191
{
185
- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
192
+ $ configData = $ this -> configDataFactory -> create (ConfigFilePool::APP_ENV );
186
193
187
194
if ($ this ->deploymentConfig ->get (ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP ) === null ) {
188
195
$ configData ->set (ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP , 'default ' );
@@ -198,10 +205,12 @@ public function createResourceConfig()
198
205
*/
199
206
public function createXFrameConfig ()
200
207
{
201
- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
208
+ $ configData = $ this ->configDataFactory ->create (ConfigFilePool::APP_ENV );
209
+
202
210
if ($ this ->deploymentConfig ->get (ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT ) === null ) {
203
211
$ configData ->set (ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT , 'SAMEORIGIN ' );
204
212
}
213
+
205
214
return $ configData ;
206
215
}
207
216
@@ -212,10 +221,12 @@ public function createXFrameConfig()
212
221
*/
213
222
public function createModeConfig ()
214
223
{
215
- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
224
+ $ configData = $ this ->configDataFactory ->create (ConfigFilePool::APP_ENV );
225
+
216
226
if ($ this ->deploymentConfig ->get (State::PARAM_MODE ) === null ) {
217
227
$ configData ->set (State::PARAM_MODE , State::MODE_DEFAULT );
218
228
}
229
+
219
230
return $ configData ;
220
231
}
221
232
@@ -227,21 +238,29 @@ public function createModeConfig()
227
238
*/
228
239
public function createCacheHostsConfig (array $ data )
229
240
{
230
- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
241
+ $ configData = $ this ->configDataFactory ->create (ConfigFilePool::APP_ENV );
242
+
231
243
if (isset ($ data [ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS ])) {
232
- $ hostData = explode (', ' , $ data [ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS ]);
233
- $ hosts = [];
234
- foreach ($ hostData as $ data ) {
235
- $ dataArray = explode (': ' , trim ($ data ));
236
- $ host = [];
237
- $ host ['host ' ] = $ dataArray [0 ];
238
- if (isset ($ dataArray [1 ])) {
239
- $ host ['port ' ] = $ dataArray [1 ];
240
- }
241
- $ hosts [] = $ host ;
242
- }
244
+ $ hosts = explode (', ' , $ data [ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS ]);
245
+
246
+ $ hosts = array_map (
247
+ function ($ hostData ) {
248
+ $ hostDataParts = explode (': ' , trim ($ hostData ));
249
+
250
+ $ tmp = ['host ' => $ hostDataParts [0 ]];
251
+
252
+ if (isset ($ hostDataParts [1 ])) {
253
+ $ tmp ['port ' ] = $ hostDataParts [1 ];
254
+ }
255
+
256
+ return $ tmp ;
257
+ },
258
+ $ hosts
259
+ );
260
+
243
261
$ configData ->set (ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS , $ hosts );
244
262
}
263
+
245
264
$ configData ->setOverrideWhenSave (true );
246
265
return $ configData ;
247
266
}
0 commit comments