PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/setup/src/Magento/Setup/Model/ConfigGenerator.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 270 lines | 163 code | 31 blank | 76 comment | 15 complexity | 13b3b8dd9ae17fefa57432c9dbeadaeb MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Setup\Model;
  7. use Magento\Framework\Config\Data\ConfigData;
  8. use Magento\Framework\Config\File\ConfigFilePool;
  9. use Magento\Framework\Math\Random;
  10. use Magento\Framework\App\DeploymentConfig;
  11. use Magento\Framework\Config\ConfigOptionsListConstants;
  12. use Magento\Framework\App\State;
  13. use Magento\Framework\App\ObjectManagerFactory;
  14. /**
  15. * Creates deployment config data based on user input array
  16. * this class introduced to break down Magento\Setup\Model\ConfigOptionsList::createConfig
  17. */
  18. class ConfigGenerator
  19. {
  20. /**
  21. * Maps configuration parameters to array keys in deployment config file
  22. *
  23. * @var array
  24. */
  25. private static $paramMap = [
  26. ConfigOptionsListConstants::INPUT_KEY_DB_HOST => ConfigOptionsListConstants::KEY_HOST,
  27. ConfigOptionsListConstants::INPUT_KEY_DB_NAME => ConfigOptionsListConstants::KEY_NAME,
  28. ConfigOptionsListConstants::INPUT_KEY_DB_USER => ConfigOptionsListConstants::KEY_USER,
  29. ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD => ConfigOptionsListConstants::KEY_PASSWORD,
  30. ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX => ConfigOptionsListConstants::KEY_PREFIX,
  31. ConfigOptionsListConstants::INPUT_KEY_DB_MODEL => ConfigOptionsListConstants::KEY_MODEL,
  32. ConfigOptionsListConstants::INPUT_KEY_DB_ENGINE => ConfigOptionsListConstants::KEY_ENGINE,
  33. ConfigOptionsListConstants::INPUT_KEY_DB_INIT_STATEMENTS => ConfigOptionsListConstants::KEY_INIT_STATEMENTS,
  34. ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY => ConfigOptionsListConstants::KEY_ENCRYPTION_KEY,
  35. ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE => ConfigOptionsListConstants::KEY_SAVE,
  36. ConfigOptionsListConstants::INPUT_KEY_RESOURCE => ConfigOptionsListConstants::KEY_RESOURCE,
  37. ];
  38. /**
  39. * @var Random
  40. */
  41. protected $random;
  42. /**
  43. * @var DeploymentConfig
  44. */
  45. protected $deploymentConfig;
  46. /**
  47. * Constructor
  48. *
  49. * @param Random $random
  50. * @param DeploymentConfig $deploymentConfig
  51. */
  52. public function __construct(Random $random, DeploymentConfig $deploymentConfig)
  53. {
  54. $this->random = $random;
  55. $this->deploymentConfig = $deploymentConfig;
  56. }
  57. /**
  58. * Creates install segment config data
  59. *
  60. * @deprecated
  61. *
  62. * @return ConfigData
  63. */
  64. public function createInstallConfig()
  65. {
  66. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  67. if ($this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE) === null) {
  68. $configData->set(ConfigOptionsListConstants::CONFIG_PATH_INSTALL_DATE, date('r'));
  69. }
  70. return $configData;
  71. }
  72. /**
  73. * Creates encryption key config data
  74. * @param array $data
  75. * @return ConfigData
  76. */
  77. public function createCryptConfig(array $data)
  78. {
  79. $currentKey = $this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY);
  80. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  81. if (isset($data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY])) {
  82. if ($currentKey !== null) {
  83. $key = $currentKey . "\n" . $data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY];
  84. } else {
  85. $key = $data[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY];
  86. }
  87. $configData->set(ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY, $key);
  88. } else {
  89. if ($currentKey === null) {
  90. $configData->set(
  91. ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY,
  92. md5($this->random->getRandomString(ConfigOptionsListConstants::STORE_KEY_RANDOM_STRING_SIZE))
  93. );
  94. }
  95. }
  96. return $configData;
  97. }
  98. /**
  99. * Creates session config data
  100. *
  101. * @param array $data
  102. * @return ConfigData
  103. */
  104. public function createSessionConfig(array $data)
  105. {
  106. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  107. if (isset($data[ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE])) {
  108. $configData->set(
  109. ConfigOptionsListConstants::CONFIG_PATH_SESSION_SAVE,
  110. $data[ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE]
  111. );
  112. }
  113. return $configData;
  114. }
  115. /**
  116. * Creates definitions config data
  117. *
  118. * @param array $data
  119. * @return ConfigData
  120. */
  121. public function createDefinitionsConfig(array $data)
  122. {
  123. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  124. if (!empty($data[ConfigOptionsListConstants::INPUT_KEY_DEFINITION_FORMAT])) {
  125. $configData->set(
  126. ObjectManagerFactory::CONFIG_PATH_DEFINITION_FORMAT,
  127. $data[ConfigOptionsListConstants::INPUT_KEY_DEFINITION_FORMAT]
  128. );
  129. }
  130. return $configData;
  131. }
  132. /**
  133. * Creates db config data
  134. *
  135. * @param array $data
  136. * @return ConfigData
  137. */
  138. public function createDbConfig(array $data)
  139. {
  140. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  141. $optional = [
  142. ConfigOptionsListConstants::INPUT_KEY_DB_HOST,
  143. ConfigOptionsListConstants::INPUT_KEY_DB_NAME,
  144. ConfigOptionsListConstants::INPUT_KEY_DB_USER,
  145. ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD,
  146. ConfigOptionsListConstants::INPUT_KEY_DB_MODEL,
  147. ConfigOptionsListConstants::INPUT_KEY_DB_ENGINE,
  148. ConfigOptionsListConstants::INPUT_KEY_DB_INIT_STATEMENTS,
  149. ];
  150. if (isset($data[ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX])) {
  151. $configData->set(
  152. ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX,
  153. $data[ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX]
  154. );
  155. }
  156. foreach ($optional as $key) {
  157. if (isset($data[$key])) {
  158. $configData->set(
  159. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/' . self::$paramMap[$key],
  160. $data[$key]
  161. );
  162. }
  163. }
  164. $currentStatus = $this->deploymentConfig->get(
  165. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT . '/' . ConfigOptionsListConstants::KEY_ACTIVE
  166. );
  167. if ($currentStatus === null) {
  168. $configData->set(
  169. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT
  170. . '/' . ConfigOptionsListConstants::KEY_ACTIVE,
  171. '1'
  172. );
  173. }
  174. return $configData;
  175. }
  176. /**
  177. * Creates resource config data
  178. *
  179. * @return ConfigData
  180. */
  181. public function createResourceConfig()
  182. {
  183. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  184. if ($this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP) === null) {
  185. $configData->set(ConfigOptionsListConstants::CONFIG_PATH_RESOURCE_DEFAULT_SETUP, 'default');
  186. }
  187. return $configData;
  188. }
  189. /**
  190. * Creates x-frame-options header config data
  191. *
  192. * @return ConfigData
  193. */
  194. public function createXFrameConfig()
  195. {
  196. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  197. if ($this->deploymentConfig->get(ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT) === null) {
  198. $configData->set(ConfigOptionsListConstants::CONFIG_PATH_X_FRAME_OPT, 'SAMEORIGIN');
  199. }
  200. return $configData;
  201. }
  202. /**
  203. * Create default entry for mode config option
  204. *
  205. * @return ConfigData
  206. */
  207. public function createModeConfig()
  208. {
  209. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  210. $configData->set(State::PARAM_MODE, State::MODE_DEFAULT);
  211. return $configData;
  212. }
  213. /**
  214. * Creates cache hosts config data
  215. *
  216. * @param array $data
  217. * @return ConfigData
  218. */
  219. public function createCacheHostsConfig(array $data)
  220. {
  221. $configData = new ConfigData(ConfigFilePool::APP_ENV);
  222. if (isset($data[ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS])) {
  223. $hostData = explode(',', $data[ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS]);
  224. $hosts = [];
  225. foreach ($hostData as $data) {
  226. $dataArray = explode(':', trim($data));
  227. $host = [];
  228. $host['host'] = $dataArray[0];
  229. if (isset($dataArray[1])) {
  230. $host['port'] = $dataArray[1];
  231. }
  232. $hosts[] = $host;
  233. }
  234. $configData->set(ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS, $hosts);
  235. }
  236. $configData->setOverrideWhenSave(true);
  237. return $configData;
  238. }
  239. }