Setup.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Base Resource Setup Model
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\Module;
  9. use Magento\Framework\App\ResourceConnection;
  10. use Magento\Framework\Setup\ModuleDataSetupInterface;
  11. use Magento\Framework\Setup\SetupInterface;
  12. class Setup implements SetupInterface
  13. {
  14. /**
  15. * Setup Connection
  16. *
  17. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  18. */
  19. private $connection = null;
  20. /**
  21. * Tables cache array
  22. *
  23. * @var array
  24. */
  25. private $tables = [];
  26. /**
  27. * Modules configuration
  28. *
  29. * @var \Magento\Framework\App\ResourceConnection
  30. */
  31. private $resourceModel;
  32. /**
  33. * Connection instance name
  34. *
  35. * @var string
  36. */
  37. private $connectionName;
  38. /**
  39. * Init
  40. *
  41. * @param \Magento\Framework\App\ResourceConnection $resource
  42. * @param string $connectionName
  43. */
  44. public function __construct(
  45. \Magento\Framework\App\ResourceConnection $resource,
  46. $connectionName = ModuleDataSetupInterface::DEFAULT_SETUP_CONNECTION
  47. ) {
  48. $this->resourceModel = $resource;
  49. $this->connectionName = $connectionName;
  50. }
  51. /**
  52. * Get connection object
  53. *
  54. * @param string|null $connectionName
  55. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  56. */
  57. public function getConnection($connectionName = null)
  58. {
  59. if ($connectionName !== null) {
  60. try {
  61. return $this->resourceModel->getConnectionByName($connectionName);
  62. } catch (\DomainException $exception) {
  63. //Fallback to default connection
  64. }
  65. }
  66. return $this->getDefaultConnection();
  67. }
  68. /**
  69. * Returns default setup connection instance
  70. *
  71. * @return \Magento\Framework\DB\Adapter\AdapterInterface
  72. */
  73. private function getDefaultConnection()
  74. {
  75. if (null === $this->connection) {
  76. $this->connection = $this->resourceModel->getConnection($this->connectionName);
  77. }
  78. return $this->connection;
  79. }
  80. /**
  81. * Add table placeholder/table name relation
  82. *
  83. * @param string $tableName
  84. * @param string $realTableName
  85. * @return $this
  86. */
  87. public function setTable($tableName, $realTableName)
  88. {
  89. $this->tables[$tableName] = $realTableName;
  90. return $this;
  91. }
  92. /**
  93. * Gets table placeholder by table name
  94. *
  95. * @param string $tableName
  96. * @return string
  97. */
  98. public function getTablePlaceholder($tableName)
  99. {
  100. return $this->resourceModel->getTablePlaceholder($tableName);
  101. }
  102. /**
  103. * Get table name (validated by db adapter) by table placeholder
  104. *
  105. * @param string|array $tableName
  106. * @param string $connectionName
  107. * @return string
  108. */
  109. public function getTable($tableName, $connectionName = ResourceConnection::DEFAULT_CONNECTION)
  110. {
  111. $cacheKey = $this->_getTableCacheName($tableName);
  112. if (!isset($this->tables[$cacheKey])) {
  113. $this->tables[$cacheKey] = $this->resourceModel->getTableName($tableName, $connectionName);
  114. }
  115. return $this->tables[$cacheKey];
  116. }
  117. /**
  118. * Retrieve table name for cache
  119. *
  120. * @param string|array $tableName
  121. * @return string
  122. */
  123. private function _getTableCacheName($tableName)
  124. {
  125. if (is_array($tableName)) {
  126. return join('_', $tableName);
  127. }
  128. return $tableName;
  129. }
  130. /**
  131. * Check is table exists
  132. *
  133. * @param string $table
  134. * @param string $connectionName
  135. * @return bool
  136. */
  137. public function tableExists($table, $connectionName = ResourceConnection::DEFAULT_CONNECTION)
  138. {
  139. $table = $this->getTable($table, $connectionName);
  140. return $this->getConnection($connectionName)->isTableExists($table);
  141. }
  142. /**
  143. * Run plain SQL query(ies)
  144. *
  145. * @param string $sql
  146. * @return $this
  147. */
  148. public function run($sql)
  149. {
  150. $this->getConnection()->query($sql);
  151. return $this;
  152. }
  153. /**
  154. * Prepare database before install/upgrade
  155. *
  156. * @return $this
  157. */
  158. public function startSetup()
  159. {
  160. $this->getConnection()->startSetup();
  161. return $this;
  162. }
  163. /**
  164. * Prepare database after install/upgrade
  165. *
  166. * @return $this
  167. */
  168. public function endSetup()
  169. {
  170. $this->getConnection()->endSetup();
  171. return $this;
  172. }
  173. }