66
77namespace Magento \Setup \Model ;
88
9+ use Magento \Framework \App \ObjectManager ;
910use Magento \Framework \Config \Data \ConfigData ;
11+ use Magento \Framework \Config \Data \ConfigDataFactory ;
1012use Magento \Framework \Config \File \ConfigFilePool ;
11- use Magento \Framework \Math \Random ;
1213use Magento \Framework \App \DeploymentConfig ;
1314use Magento \Framework \Config \ConfigOptionsListConstants ;
1415use Magento \Framework \App \State ;
15- use Magento \Framework \App \ ObjectManagerFactory ;
16+ use Magento \Framework \Math \ Random ;
1617
1718/**
1819 * Creates deployment config data based on user input array
@@ -26,39 +27,58 @@ class ConfigGenerator
2627 * @var array
2728 */
2829 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 ,
3637 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 ,
4041 ];
4142
43+ /**
44+ * @var DeploymentConfig
45+ */
46+ protected $ deploymentConfig ;
47+
4248 /**
4349 * @var Random
50+ * @deprecated since 100.2.0
4451 */
4552 protected $ random ;
4653
4754 /**
48- * @var DeploymentConfig
55+ * @var ConfigDataFactory
4956 */
50- protected $ deploymentConfig ;
57+ private $ configDataFactory ;
58+
59+ /**
60+ * @var CryptKeyGeneratorInterface
61+ */
62+ private $ cryptKeyGenerator ;
5163
5264 /**
5365 * Constructor
5466 *
55- * @param Random $random
67+ * @param Random $random Deprecated since 100.2.0
5668 * @param DeploymentConfig $deploymentConfig
69+ * @param ConfigDataFactory|null $configDataFactory
70+ * @param CryptKeyGeneratorInterface|null $cryptKeyGenerator
5771 */
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+ ) {
6078 $ this ->random = $ random ;
6179 $ this ->deploymentConfig = $ deploymentConfig ;
80+ $ this ->configDataFactory = $ configDataFactory ?? ObjectManager::getInstance ()->get (ConfigDataFactory::class);
81+ $ this ->cryptKeyGenerator = $ cryptKeyGenerator ?? ObjectManager::getInstance ()->get (CryptKeyGenerator::class);
6282 }
6383
6484 /**
@@ -70,23 +90,17 @@ public function createCryptConfig(array $data)
7090 {
7191 $ currentKey = $ this ->deploymentConfig ->get (ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY );
7292
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 );
8094
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 );
90104
91105 return $ configData ;
92106 }
@@ -99,7 +113,7 @@ public function createCryptConfig(array $data)
99113 */
100114 public function createSessionConfig (array $ data )
101115 {
102- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
116+ $ configData = $ this -> configDataFactory -> create (ConfigFilePool::APP_ENV );
103117
104118 if (isset ($ data [ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE ])) {
105119 $ configData ->set (
@@ -132,7 +146,7 @@ public function createDefinitionsConfig(array $data)
132146 */
133147 public function createDbConfig (array $ data )
134148 {
135- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
149+ $ configData = $ this -> configDataFactory -> create (ConfigFilePool::APP_ENV );
136150
137151 $ optional = [
138152 ConfigOptionsListConstants::INPUT_KEY_DB_HOST ,
@@ -151,25 +165,18 @@ public function createDbConfig(array $data)
151165 );
152166 }
153167
168+ $ dbConnectionPrefix = ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/ ' ;
169+
154170 foreach ($ optional as $ key ) {
155171 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 ]);
160173 }
161174 }
162175
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 );
166177
167178 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 ' );
173180 }
174181
175182 return $ configData ;
@@ -182,7 +189,7 @@ public function createDbConfig(array $data)
182189 */
183190 public function createResourceConfig ()
184191 {
185- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
192+ $ configData = $ this -> configDataFactory -> create (ConfigFilePool::APP_ENV );
186193
187194 if ($ this ->deploymentConfig ->get (ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP ) === null ) {
188195 $ configData ->set (ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP , 'default ' );
@@ -198,10 +205,12 @@ public function createResourceConfig()
198205 */
199206 public function createXFrameConfig ()
200207 {
201- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
208+ $ configData = $ this ->configDataFactory ->create (ConfigFilePool::APP_ENV );
209+
202210 if ($ this ->deploymentConfig ->get (ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT ) === null ) {
203211 $ configData ->set (ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT , 'SAMEORIGIN ' );
204212 }
213+
205214 return $ configData ;
206215 }
207216
@@ -212,10 +221,12 @@ public function createXFrameConfig()
212221 */
213222 public function createModeConfig ()
214223 {
215- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
224+ $ configData = $ this ->configDataFactory ->create (ConfigFilePool::APP_ENV );
225+
216226 if ($ this ->deploymentConfig ->get (State::PARAM_MODE ) === null ) {
217227 $ configData ->set (State::PARAM_MODE , State::MODE_DEFAULT );
218228 }
229+
219230 return $ configData ;
220231 }
221232
@@ -227,21 +238,29 @@ public function createModeConfig()
227238 */
228239 public function createCacheHostsConfig (array $ data )
229240 {
230- $ configData = new ConfigData (ConfigFilePool::APP_ENV );
241+ $ configData = $ this ->configDataFactory ->create (ConfigFilePool::APP_ENV );
242+
231243 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+
243261 $ configData ->set (ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS , $ hosts );
244262 }
263+
245264 $ configData ->setOverrideWhenSave (true );
246265 return $ configData ;
247266 }
0 commit comments