ModuleResource.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Module;
  7. use \Magento\Framework\Model\ResourceModel\Db\AbstractDb;
  8. /**
  9. * Resource Model
  10. *
  11. * @deprecated 102.0.0 Declarative schema and data patches replace old functionality and setup_module table
  12. * So all resources related to this table, will be deprecated since 2.3.0
  13. */
  14. class ModuleResource extends AbstractDb implements ResourceInterface
  15. {
  16. /**
  17. * Database versions
  18. *
  19. * @var array
  20. */
  21. protected static $schemaVersions = null;
  22. /**
  23. * Resource data versions cache array
  24. *
  25. * @var array
  26. */
  27. protected static $dataVersions = null;
  28. /**
  29. * Define main table
  30. *
  31. * @return void
  32. */
  33. protected function _construct()
  34. {
  35. $this->_init('setup_module', 'module');
  36. }
  37. /**
  38. * Fill static versions arrays.
  39. * This routine fetches Db and Data versions of at once to optimize sql requests. However, when upgrading, it's
  40. * possible that 'data' column will be created only after all Db installs are passed. So $neededType contains
  41. * information on main purpose of calling this routine, and even when 'data' column is absent - it won't require
  42. * reissuing new sql just to get 'db' version of module.
  43. *
  44. * @param string $needType Can be 'db' or 'data'
  45. * @return $this
  46. */
  47. protected function _loadVersion($needType)
  48. {
  49. if ($needType == 'db' && self::$schemaVersions === null ||
  50. $needType == 'data' && self::$dataVersions === null
  51. ) {
  52. self::$schemaVersions = [];
  53. // Db version column always exists
  54. self::$dataVersions = null;
  55. // Data version array will be filled only if Data column exist
  56. if ($this->getConnection()->isTableExists($this->getMainTable())) {
  57. $select = $this->getConnection()->select()->from($this->getMainTable(), '*');
  58. $rowset = $this->getConnection()->fetchAll($select);
  59. foreach ($rowset as $row) {
  60. self::$schemaVersions[$row['module']] = $row['schema_version'];
  61. if (array_key_exists('data_version', $row)) {
  62. if (self::$dataVersions === null) {
  63. self::$dataVersions = [];
  64. }
  65. self::$dataVersions[$row['module']] = $row['data_version'];
  66. }
  67. }
  68. }
  69. }
  70. return $this;
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getDbVersion($moduleName)
  76. {
  77. if (!$this->getConnection()) {
  78. return false;
  79. }
  80. $this->_loadVersion('db');
  81. return self::$schemaVersions[$moduleName] ?? false;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function setDbVersion($moduleName, $version)
  87. {
  88. $dbModuleInfo = ['module' => $moduleName, 'schema_version' => $version];
  89. if ($this->getDbVersion($moduleName)) {
  90. self::$schemaVersions[$moduleName] = $version;
  91. return $this->getConnection()->update(
  92. $this->getMainTable(),
  93. $dbModuleInfo,
  94. ['module = ?' => $moduleName]
  95. );
  96. } else {
  97. self::$schemaVersions[$moduleName] = $version;
  98. return $this->getConnection()->insert($this->getMainTable(), $dbModuleInfo);
  99. }
  100. }
  101. /**
  102. * {@inheritdoc}
  103. */
  104. public function getDataVersion($moduleName)
  105. {
  106. if (!$this->getConnection()) {
  107. return false;
  108. }
  109. $this->_loadVersion('data');
  110. return self::$dataVersions[$moduleName] ?? false;
  111. }
  112. /**
  113. * {@inheritdoc}
  114. */
  115. public function setDataVersion($moduleName, $version)
  116. {
  117. $data = ['module' => $moduleName, 'data_version' => $version];
  118. if ($this->getDbVersion($moduleName) || $this->getDataVersion($moduleName)) {
  119. self::$dataVersions[$moduleName] = $version;
  120. $this->getConnection()->update($this->getMainTable(), $data, ['module = ?' => $moduleName]);
  121. } else {
  122. self::$dataVersions[$moduleName] = $version;
  123. $this->getConnection()->insert($this->getMainTable(), $data);
  124. }
  125. }
  126. /**
  127. * Flush all class cache
  128. *
  129. * @deprecated 102.0.0 This method was added as temporary solution, to increase modularity:
  130. * Because before new modules appears in resource only on next bootstrap
  131. * @return void
  132. */
  133. public static function flush()
  134. {
  135. self::$dataVersions = null;
  136. self::$schemaVersions = [];
  137. }
  138. }