/app/code/Magento/Install/Model/Installer/Db.php

https://github.com/creaminternet/magento2-opensource · PHP · 195 lines · 97 code · 19 blank · 79 comment · 15 complexity · fe0bec2fac7fb61bcd99881869c0ca3d MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Magento
  22. * @package Magento_Install
  23. * @copyright Copyright (c) 2014 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * DB Installer
  28. */
  29. namespace Magento\Install\Model\Installer;
  30. class Db extends \Magento\Install\Model\Installer\AbstractInstaller
  31. {
  32. /**
  33. * Database resource
  34. *
  35. * @var \Magento\Install\Model\Installer\Db\AbstractDb
  36. */
  37. protected $_dbResource;
  38. /**
  39. * @var \Magento\Logger
  40. */
  41. protected $_logger;
  42. /**
  43. * Database model factory
  44. *
  45. * @var \Magento\Install\Model\Installer\Db\Factory
  46. */
  47. protected $_dbFactory;
  48. /**
  49. * Databases configuration
  50. *
  51. * @var array
  52. */
  53. protected $_dbConfig;
  54. /**
  55. * @param \Magento\Install\Model\Installer $installer
  56. * @param \Magento\Logger $logger
  57. * @param \Magento\Install\Model\Installer\Db\Factory $dbFactory
  58. * @param array $dbConfig
  59. */
  60. public function __construct(
  61. \Magento\Install\Model\Installer $installer,
  62. \Magento\Logger $logger,
  63. \Magento\Install\Model\Installer\Db\Factory $dbFactory,
  64. array $dbConfig
  65. ) {
  66. parent::__construct($installer);
  67. $this->_logger = $logger;
  68. $this->_dbConfig = $dbConfig;
  69. $this->_dbFactory = $dbFactory;
  70. }
  71. /**
  72. * Check database connection
  73. * and return checked connection data
  74. *
  75. * @param array $data
  76. * @return array
  77. * @throws \Magento\Core\Exception
  78. */
  79. public function checkDbConnectionData($data)
  80. {
  81. $data = $this->_getCheckedData($data);
  82. try {
  83. /** @var \Magento\Install\Model\Installer\Db\Mysql4 $resource */
  84. $resource = $this->_getDbResource();
  85. $resource->setConfig($data);
  86. // check required extensions
  87. $absenteeExtensions = array();
  88. $extensions = $resource->getRequiredExtensions();
  89. foreach ($extensions as $extName) {
  90. if (!extension_loaded($extName)) {
  91. $absenteeExtensions[] = $extName;
  92. }
  93. }
  94. if (!empty($absenteeExtensions)) {
  95. throw new \Magento\Core\Exception(
  96. __('PHP Extensions "%1" must be loaded.', implode(',', $absenteeExtensions))
  97. );
  98. }
  99. $version = $resource->getVersion();
  100. $requiredVersion = isset($this->_dbConfig['mysql4']['min_version'])
  101. ? $this->_dbConfig['mysql4']['min_version']
  102. : 0;
  103. // check DB server version
  104. if (version_compare($version, $requiredVersion) == -1) {
  105. throw new \Magento\Core\Exception(
  106. __('The database server version doesn\'t match system requirements (required: %1, actual: %2).', $requiredVersion, $version)
  107. );
  108. }
  109. // check InnoDB support
  110. if (!$resource->supportEngine()) {
  111. throw new \Magento\Core\Exception(
  112. __('Database server does not support the InnoDB storage engine.')
  113. );
  114. }
  115. // TODO: check user roles
  116. } catch (\Magento\Core\Exception $e) {
  117. $this->_logger->logException($e);
  118. throw new \Magento\Core\Exception(__($e->getMessage()));
  119. } catch (\Exception $e) {
  120. $this->_logger->logException($e);
  121. throw new \Magento\Core\Exception(__('Something went wrong while connecting to the database.'));
  122. }
  123. return $data;
  124. }
  125. /**
  126. * Check database connection data
  127. *
  128. * @param array $data
  129. * @return array
  130. */
  131. protected function _getCheckedData($data)
  132. {
  133. if (!isset($data['db_name']) || empty($data['db_name'])) {
  134. throw new \Magento\Core\Exception(__('The Database Name field cannot be empty.'));
  135. }
  136. //make all table prefix to lower letter
  137. if ($data['db_prefix'] != '') {
  138. $data['db_prefix'] = strtolower($data['db_prefix']);
  139. }
  140. //check table prefix
  141. if ($data['db_prefix'] != '') {
  142. if (!preg_match('/^[a-z]+[a-z0-9_]*$/', $data['db_prefix'])) {
  143. throw new \Magento\Core\Exception(
  144. __('The table prefix should contain only letters (a-z), numbers (0-9) or underscores (_); the first character should be a letter.')
  145. );
  146. }
  147. }
  148. //set db type according the db model
  149. if (!isset($data['db_type'])) {
  150. $data['db_type'] = isset($this->_dbConfig[(string)$data['db_model']]['type'])
  151. ? $this->_dbConfig[(string)$data['db_model']]['type']
  152. : null;
  153. }
  154. $dbResource = $this->_getDbResource();
  155. $data['db_pdo_type'] = $dbResource->getPdoType();
  156. if (!isset($data['db_init_statements'])) {
  157. $data['db_init_statements'] = isset($this->_dbConfig[(string)$data['db_model']]['initStatements'])
  158. ? $this->_dbConfig[(string)$data['db_model']]['initStatements']
  159. : null;
  160. }
  161. return $data;
  162. }
  163. /**
  164. * Retrieve the database resource
  165. *
  166. * @return \Magento\Install\Model\Installer\Db\AbstractDb
  167. * @throws \Magento\Core\Exception
  168. */
  169. protected function _getDbResource()
  170. {
  171. if (!isset($this->_dbResource)) {
  172. $this->_dbResource = $this->_dbFactory->get('mysql4');
  173. }
  174. return $this->_dbResource;
  175. }
  176. }