InitializeStoresAndWebsites.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Store\Setup\Patch\Schema;
  7. use Magento\Catalog\Helper\DefaultCategory;
  8. use Magento\Framework\Setup\SchemaSetupInterface;
  9. use Magento\Framework\Setup\Patch\PatchVersionInterface;
  10. use Magento\Framework\Setup\Patch\SchemaPatchInterface;
  11. use Magento\Store\Api\Data\WebsiteInterface;
  12. /**
  13. * Create stores and websites. Actually stores and websites are part of schema as
  14. * other modules schema relies on store and website presence.
  15. * @package Magento\Store\Setup\Patch\Schema
  16. */
  17. class InitializeStoresAndWebsites implements SchemaPatchInterface, PatchVersionInterface
  18. {
  19. /**
  20. * @var SchemaSetupInterface
  21. */
  22. private $schemaSetup;
  23. /**
  24. * @var DefaultCategory
  25. */
  26. private $defaultCategory;
  27. /**
  28. * @var \Magento\Catalog\Helper\DefaultCategoryFactory
  29. */
  30. private $defaultCategoryFactory;
  31. /**
  32. * PatchInitial constructor.
  33. * @param SchemaSetupInterface $schemaSetup
  34. */
  35. public function __construct(
  36. SchemaSetupInterface $schemaSetup,
  37. \Magento\Catalog\Helper\DefaultCategoryFactory $defaultCategoryFactory
  38. ) {
  39. $this->schemaSetup = $schemaSetup;
  40. $this->defaultCategoryFactory = $defaultCategoryFactory;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function apply()
  46. {
  47. $this->schemaSetup->startSetup();
  48. $connection = $this->schemaSetup->getConnection();
  49. $select = $connection->select()
  50. ->from($this->schemaSetup->getTable('store_website'))
  51. ->where('website_id = ?', 0);
  52. if ($connection->fetchOne($select) === false) {
  53. /**
  54. * Insert websites
  55. */
  56. $connection->insertForce(
  57. $this->schemaSetup->getTable('store_website'),
  58. [
  59. 'website_id' => 0,
  60. 'code' => WebsiteInterface::ADMIN_CODE,
  61. 'name' => 'Admin',
  62. 'sort_order' => 0,
  63. 'default_group_id' => 0,
  64. 'is_default' => 0
  65. ]
  66. );
  67. $connection->insertForce(
  68. $this->schemaSetup->getTable('store_website'),
  69. [
  70. 'website_id' => 1,
  71. 'code' => 'base',
  72. 'name' => 'Main Website',
  73. 'sort_order' => 0,
  74. 'default_group_id' => 1,
  75. 'is_default' => 1
  76. ]
  77. );
  78. /**
  79. * Insert store groups
  80. */
  81. $connection->insertForce(
  82. $this->schemaSetup->getTable('store_group'),
  83. [
  84. 'group_id' => 0,
  85. 'website_id' => 0,
  86. 'name' => 'Default',
  87. 'root_category_id' => 0,
  88. 'default_store_id' => 0
  89. ]
  90. );
  91. $connection->insertForce(
  92. $this->schemaSetup->getTable('store_group'),
  93. [
  94. 'group_id' => 1,
  95. 'website_id' => 1,
  96. 'name' => 'Main Website Store',
  97. 'root_category_id' => $this->getDefaultCategory()->getId(),
  98. 'default_store_id' => 1
  99. ]
  100. );
  101. /**
  102. * Insert stores
  103. */
  104. $connection->insertForce(
  105. $this->schemaSetup->getTable('store'),
  106. [
  107. 'store_id' => 0,
  108. 'code' => 'admin',
  109. 'website_id' => 0,
  110. 'group_id' => 0,
  111. 'name' => 'Admin',
  112. 'sort_order' => 0,
  113. 'is_active' => 1
  114. ]
  115. );
  116. $connection->insertForce(
  117. $this->schemaSetup->getTable('store'),
  118. [
  119. 'store_id' => 1,
  120. 'code' => 'default',
  121. 'website_id' => 1,
  122. 'group_id' => 1,
  123. 'name' => 'Default Store View',
  124. 'sort_order' => 0,
  125. 'is_active' => 1
  126. ]
  127. );
  128. $this->schemaSetup->endSetup();
  129. }
  130. }
  131. /**
  132. * Get default category.
  133. *
  134. * @deprecated 101.0.0
  135. * @return DefaultCategory
  136. */
  137. private function getDefaultCategory()
  138. {
  139. if ($this->defaultCategory === null) {
  140. $this->defaultCategory = $this->defaultCategoryFactory->create();
  141. }
  142. return $this->defaultCategory;
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public static function getDependencies()
  148. {
  149. return [];
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public static function getVersion()
  155. {
  156. return '2.0.0';
  157. }
  158. /**
  159. * {@inheritdoc}
  160. */
  161. public function getAliases()
  162. {
  163. return [];
  164. }
  165. }