PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/crazybutterfly815/magento2
PHP | 360 lines | 259 code | 30 blank | 71 comment | 17 complexity | a5d3077d2185203bc6b9e43568100c7a 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\ObjectManager\DefinitionFactory;
  8. use Magento\Framework\Setup\ConfigOptionsListInterface;
  9. use Magento\Framework\Setup\Option\SelectConfigOption;
  10. use Magento\Framework\Setup\Option\TextConfigOption;
  11. use Magento\Framework\Setup\Option\FlagConfigOption;
  12. use Magento\Framework\App\DeploymentConfig;
  13. use Magento\Framework\Config\ConfigOptionsListConstants;
  14. use Magento\Setup\Validator\DbValidator;
  15. use Magento\Framework\App\ObjectManagerFactory;
  16. /**
  17. * Deployment configuration options needed for Setup application
  18. *
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class ConfigOptionsList implements ConfigOptionsListInterface
  22. {
  23. /**
  24. * Generate config data for individual segments
  25. *
  26. * @var ConfigGenerator
  27. */
  28. private $configGenerator;
  29. /** @var DbValidator */
  30. private $dbValidator;
  31. /** @var array */
  32. private $validSaveHandlers = [
  33. ConfigOptionsListConstants::SESSION_SAVE_FILES,
  34. ConfigOptionsListConstants::SESSION_SAVE_DB,
  35. ];
  36. /**
  37. * Constructor
  38. *
  39. * @param ConfigGenerator $configGenerator
  40. * @param DbValidator $dbValidator
  41. */
  42. public function __construct(ConfigGenerator $configGenerator, DbValidator $dbValidator)
  43. {
  44. $this->configGenerator = $configGenerator;
  45. $this->dbValidator = $dbValidator;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  50. */
  51. public function getOptions()
  52. {
  53. return [
  54. new TextConfigOption(
  55. ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY,
  56. TextConfigOption::FRONTEND_WIZARD_TEXT,
  57. ConfigOptionsListConstants::CONFIG_PATH_CRYPT_KEY,
  58. 'Encryption key'
  59. ),
  60. new SelectConfigOption(
  61. ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE,
  62. SelectConfigOption::FRONTEND_WIZARD_SELECT,
  63. $this->validSaveHandlers,
  64. ConfigOptionsListConstants::CONFIG_PATH_SESSION_SAVE,
  65. 'Session save handler',
  66. ConfigOptionsListConstants::SESSION_SAVE_FILES
  67. ),
  68. new SelectConfigOption(
  69. ConfigOptionsListConstants::INPUT_KEY_DEFINITION_FORMAT,
  70. SelectConfigOption::FRONTEND_WIZARD_SELECT,
  71. DefinitionFactory::getSupportedFormats(),
  72. ObjectManagerFactory::CONFIG_PATH_DEFINITION_FORMAT,
  73. 'Type of definitions used by Object Manager'
  74. ),
  75. new TextConfigOption(
  76. ConfigOptionsListConstants::INPUT_KEY_DB_HOST,
  77. TextConfigOption::FRONTEND_WIZARD_TEXT,
  78. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  79. '/' . ConfigOptionsListConstants::KEY_HOST,
  80. 'Database server host',
  81. 'localhost'
  82. ),
  83. new TextConfigOption(
  84. ConfigOptionsListConstants::INPUT_KEY_DB_NAME,
  85. TextConfigOption::FRONTEND_WIZARD_TEXT,
  86. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  87. '/' . ConfigOptionsListConstants::KEY_NAME,
  88. 'Database name',
  89. 'magento2'
  90. ),
  91. new TextConfigOption(
  92. ConfigOptionsListConstants::INPUT_KEY_DB_USER,
  93. TextConfigOption::FRONTEND_WIZARD_TEXT,
  94. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  95. '/' . ConfigOptionsListConstants::KEY_USER,
  96. 'Database server username',
  97. 'root'
  98. ),
  99. new TextConfigOption(
  100. ConfigOptionsListConstants::INPUT_KEY_DB_ENGINE,
  101. TextConfigOption::FRONTEND_WIZARD_TEXT,
  102. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  103. '/' . ConfigOptionsListConstants::KEY_ENGINE,
  104. 'Database server engine',
  105. 'innodb'
  106. ),
  107. new TextConfigOption(
  108. ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD,
  109. TextConfigOption::FRONTEND_WIZARD_PASSWORD,
  110. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  111. '/' . ConfigOptionsListConstants::KEY_PASSWORD,
  112. 'Database server password',
  113. ''
  114. ),
  115. new TextConfigOption(
  116. ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX,
  117. TextConfigOption::FRONTEND_WIZARD_TEXT,
  118. ConfigOptionsListConstants::CONFIG_PATH_DB_PREFIX,
  119. 'Database table prefix',
  120. ''
  121. ),
  122. new TextConfigOption(
  123. ConfigOptionsListConstants::INPUT_KEY_DB_MODEL,
  124. TextConfigOption::FRONTEND_WIZARD_TEXT,
  125. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  126. '/' . ConfigOptionsListConstants::KEY_MODEL,
  127. 'Database type',
  128. 'mysql4'
  129. ),
  130. new TextConfigOption(
  131. ConfigOptionsListConstants::INPUT_KEY_DB_INIT_STATEMENTS,
  132. TextConfigOption::FRONTEND_WIZARD_TEXT,
  133. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  134. '/' . ConfigOptionsListConstants::KEY_INIT_STATEMENTS,
  135. 'Database initial set of commands',
  136. 'SET NAMES utf8;'
  137. ),
  138. new FlagConfigOption(
  139. ConfigOptionsListConstants::INPUT_KEY_SKIP_DB_VALIDATION,
  140. '',
  141. 'If specified, then db connection validation will be skipped',
  142. '-s'
  143. ),
  144. new TextConfigOption(
  145. ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS,
  146. TextConfigOption::FRONTEND_WIZARD_TEXT,
  147. ConfigOptionsListConstants::CONFIG_PATH_CACHE_HOSTS,
  148. 'http Cache hosts'
  149. ),
  150. ];
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function createConfig(array $data, DeploymentConfig $deploymentConfig)
  156. {
  157. $configData = [];
  158. $configData[] = $this->configGenerator->createCryptConfig($data, $deploymentConfig);
  159. $configData[] = $this->configGenerator->createSessionConfig($data);
  160. $definitionConfig = $this->configGenerator->createDefinitionsConfig($data);
  161. if (isset($definitionConfig)) {
  162. $configData[] = $definitionConfig;
  163. }
  164. $configData[] = $this->configGenerator->createDbConfig($data);
  165. $configData[] = $this->configGenerator->createResourceConfig();
  166. $configData[] = $this->configGenerator->createXFrameConfig();
  167. $configData[] = $this->configGenerator->createModeConfig();
  168. $configData[] = $this->configGenerator->createCacheHostsConfig($data);
  169. return $configData;
  170. }
  171. /**
  172. * {@inheritdoc}
  173. */
  174. public function validate(array $options, DeploymentConfig $deploymentConfig)
  175. {
  176. $errors = [];
  177. if (isset($options[ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS])) {
  178. $errors = array_merge(
  179. $errors,
  180. $this->validateHttpCacheHosts($options[ConfigOptionsListConstants::INPUT_KEY_CACHE_HOSTS])
  181. );
  182. }
  183. if (isset($options[ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX])) {
  184. $errors = array_merge(
  185. $errors,
  186. $this->validateDbPrefix($options[ConfigOptionsListConstants::INPUT_KEY_DB_PREFIX])
  187. );
  188. }
  189. if (!$options[ConfigOptionsListConstants::INPUT_KEY_SKIP_DB_VALIDATION]) {
  190. $errors = array_merge($errors, $this->validateDbSettings($options, $deploymentConfig));
  191. }
  192. $errors = array_merge(
  193. $errors,
  194. $this->validateSessionSave($options),
  195. $this->validateEncryptionKey($options)
  196. );
  197. return $errors;
  198. }
  199. /**
  200. * Returns other parts of existing db config in case is only one value is presented by user
  201. *
  202. * @param array $options
  203. * @param DeploymentConfig $deploymentConfig
  204. *
  205. * @return array
  206. */
  207. private function getDbSettings(array $options, DeploymentConfig $deploymentConfig)
  208. {
  209. if ($options[ConfigOptionsListConstants::INPUT_KEY_DB_NAME] === null) {
  210. $options[ConfigOptionsListConstants::INPUT_KEY_DB_NAME] =
  211. $deploymentConfig->get(
  212. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  213. '/' . ConfigOptionsListConstants::KEY_NAME
  214. );
  215. }
  216. if ($options[ConfigOptionsListConstants::INPUT_KEY_DB_HOST] === null) {
  217. $options[ConfigOptionsListConstants::INPUT_KEY_DB_HOST] =
  218. $deploymentConfig->get(
  219. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  220. '/' . ConfigOptionsListConstants::KEY_HOST
  221. );
  222. }
  223. if ($options[ConfigOptionsListConstants::INPUT_KEY_DB_USER] === null) {
  224. $options[ConfigOptionsListConstants::INPUT_KEY_DB_USER] =
  225. $deploymentConfig->get(
  226. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  227. '/' . ConfigOptionsListConstants::KEY_USER
  228. );
  229. }
  230. if ($options[ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD] === null) {
  231. $options[ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD] =
  232. $deploymentConfig->get(
  233. ConfigOptionsListConstants::CONFIG_PATH_DB_CONNECTION_DEFAULT .
  234. '/' . ConfigOptionsListConstants::KEY_PASSWORD
  235. );
  236. }
  237. return $options;
  238. }
  239. /**
  240. * Validates session save param
  241. *
  242. * @param array $options
  243. * @return string[]
  244. */
  245. private function validateSessionSave(array $options)
  246. {
  247. $errors = [];
  248. if (isset($options[ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE])
  249. && !in_array($options[ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE], $this->validSaveHandlers)
  250. ) {
  251. $errors[] = "Invalid session handler '{$options[ConfigOptionsListConstants::INPUT_KEY_SESSION_SAVE]}'";
  252. }
  253. return $errors;
  254. }
  255. /**
  256. * Validates encryption key param
  257. *
  258. * @param array $options
  259. * @return string[]
  260. */
  261. private function validateEncryptionKey(array $options)
  262. {
  263. $errors = [];
  264. if (isset($options[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY])
  265. && !$options[ConfigOptionsListConstants::INPUT_KEY_ENCRYPTION_KEY]) {
  266. $errors[] = 'Invalid encryption key';
  267. }
  268. return $errors;
  269. }
  270. /**
  271. * Validate http cache hosts
  272. *
  273. * @param string $option
  274. * @return string[]
  275. */
  276. private function validateHttpCacheHosts($option)
  277. {
  278. $errors = [];
  279. if (!preg_match('/^[\-\w:,.]+$/', $option)
  280. ) {
  281. $errors[] = "Invalid http cache hosts '{$option}'";
  282. }
  283. return $errors;
  284. }
  285. /**
  286. * Validate Db table prefix
  287. *
  288. * @param string $option
  289. * @return string[]
  290. */
  291. private function validateDbPrefix($option)
  292. {
  293. $errors = [];
  294. try {
  295. $this->dbValidator->checkDatabaseTablePrefix($option);
  296. } catch (\InvalidArgumentException $exception) {
  297. $errors[] = $exception->getMessage();
  298. }
  299. return $errors;
  300. }
  301. /**
  302. * Validate Db settings
  303. *
  304. * @param array $options
  305. * @param DeploymentConfig $deploymentConfig
  306. * @return string[]
  307. */
  308. private function validateDbSettings(array $options, DeploymentConfig $deploymentConfig)
  309. {
  310. $errors = [];
  311. if ($options[ConfigOptionsListConstants::INPUT_KEY_DB_NAME] !== null
  312. || $options[ConfigOptionsListConstants::INPUT_KEY_DB_HOST] !== null
  313. || $options[ConfigOptionsListConstants::INPUT_KEY_DB_USER] !== null
  314. || $options[ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD] !== null
  315. ) {
  316. try {
  317. $options = $this->getDbSettings($options, $deploymentConfig);
  318. $this->dbValidator->checkDatabaseConnection(
  319. $options[ConfigOptionsListConstants::INPUT_KEY_DB_NAME],
  320. $options[ConfigOptionsListConstants::INPUT_KEY_DB_HOST],
  321. $options[ConfigOptionsListConstants::INPUT_KEY_DB_USER],
  322. $options[ConfigOptionsListConstants::INPUT_KEY_DB_PASSWORD]
  323. );
  324. } catch (\Exception $exception) {
  325. $errors[] = $exception->getMessage();
  326. }
  327. }
  328. return $errors;
  329. }
  330. }