PageRenderTime 61ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/Magento/Catalog/Model/System/Config/Backend/Catalog/Url/Rewrite/Suffix.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 185 lines | 115 code | 14 blank | 56 comment | 12 complexity | b976b5b4fdeef54b7233584fe03271b3 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Url rewrite suffix backend
  8. */
  9. namespace Magento\Catalog\Model\System\Config\Backend\Catalog\Url\Rewrite;
  10. use Magento\CatalogUrlRewrite\Model\CategoryUrlPathGenerator;
  11. use Magento\CatalogUrlRewrite\Model\CategoryUrlRewriteGenerator;
  12. use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
  13. use Magento\CatalogUrlRewrite\Model\ProductUrlRewriteGenerator;
  14. use Magento\Framework\App\ResourceConnection;
  15. use Magento\Store\Model\ScopeInterface;
  16. use Magento\UrlRewrite\Model\Storage\DbStorage;
  17. use Magento\UrlRewrite\Service\V1\Data\UrlRewrite;
  18. /**
  19. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  20. */
  21. class Suffix extends \Magento\Framework\App\Config\Value
  22. {
  23. /** @var \Magento\UrlRewrite\Helper\UrlRewrite */
  24. protected $urlRewriteHelper;
  25. /** @var \Magento\Store\Model\StoreManagerInterface */
  26. protected $storeManager;
  27. /** @var \Magento\UrlRewrite\Model\UrlFinderInterface */
  28. protected $urlFinder;
  29. /** @var \Magento\Framework\DB\Adapter\AdapterInterface */
  30. protected $connection;
  31. /**
  32. * @var \Magento\Framework\App\ResourceConnection
  33. */
  34. protected $resource;
  35. /**
  36. * @param \Magento\Framework\Model\Context $context
  37. * @param \Magento\Framework\Registry $registry
  38. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  39. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  40. * @param \Magento\UrlRewrite\Helper\UrlRewrite $urlRewriteHelper
  41. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  42. * @param ResourceConnection $appResource
  43. * @param \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder
  44. * @param \Magento\Framework\Model\ResourceModel\AbstractResource|null $resource
  45. * @param \Magento\Framework\Data\Collection\AbstractDb|null $resourceCollection
  46. * @param array $data
  47. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  48. */
  49. public function __construct(
  50. \Magento\Framework\Model\Context $context,
  51. \Magento\Framework\Registry $registry,
  52. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  53. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  54. \Magento\UrlRewrite\Helper\UrlRewrite $urlRewriteHelper,
  55. \Magento\Store\Model\StoreManagerInterface $storeManager,
  56. \Magento\Framework\App\ResourceConnection $appResource,
  57. \Magento\UrlRewrite\Model\UrlFinderInterface $urlFinder,
  58. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  59. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  60. array $data = []
  61. ) {
  62. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  63. $this->urlRewriteHelper = $urlRewriteHelper;
  64. $this->connection = $appResource->getConnection();
  65. $this->urlFinder = $urlFinder;
  66. $this->storeManager = $storeManager;
  67. $this->resource = $appResource;
  68. }
  69. /**
  70. * Check url rewrite suffix - whether we can support it
  71. *
  72. * @return $this
  73. */
  74. public function beforeSave()
  75. {
  76. $this->urlRewriteHelper->validateSuffix($this->getValue());
  77. return $this;
  78. }
  79. /**
  80. * @return $this
  81. */
  82. public function afterSave()
  83. {
  84. if ($this->isValueChanged()) {
  85. $this->updateSuffixForUrlRewrites();
  86. if ($this->isCategorySuffixChanged()) {
  87. $this->cacheTypeList->invalidate([
  88. \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER,
  89. \Magento\Framework\App\Cache\Type\Collection::TYPE_IDENTIFIER
  90. ]);
  91. }
  92. }
  93. return parent::afterSave();
  94. }
  95. /**
  96. * Check is category suffix changed
  97. *
  98. * @return bool
  99. */
  100. private function isCategorySuffixChanged()
  101. {
  102. return $this->isValueChanged()
  103. && ($this->getPath() == CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX);
  104. }
  105. /**
  106. * Update suffix for url rewrites
  107. *
  108. * @return $this
  109. */
  110. protected function updateSuffixForUrlRewrites()
  111. {
  112. $map = [
  113. ProductUrlPathGenerator::XML_PATH_PRODUCT_URL_SUFFIX => ProductUrlRewriteGenerator::ENTITY_TYPE,
  114. CategoryUrlPathGenerator::XML_PATH_CATEGORY_URL_SUFFIX => CategoryUrlRewriteGenerator::ENTITY_TYPE,
  115. ];
  116. if (!isset($map[$this->getPath()])) {
  117. return $this;
  118. }
  119. $dataFilter = [UrlRewrite::ENTITY_TYPE => $map[$this->getPath()]];
  120. $storesIds = $this->getStoreIds();
  121. if ($storesIds) {
  122. $dataFilter[UrlRewrite::STORE_ID] = $storesIds;
  123. }
  124. $entities = $this->urlFinder->findAllByData($dataFilter);
  125. $oldSuffixPattern = '~' . preg_quote($this->getOldValue()) . '$~';
  126. $suffix = $this->getValue();
  127. foreach ($entities as $urlRewrite) {
  128. $bind = $urlRewrite->getIsAutogenerated()
  129. ? [UrlRewrite::REQUEST_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getRequestPath())]
  130. : [UrlRewrite::TARGET_PATH => preg_replace($oldSuffixPattern, $suffix, $urlRewrite->getTargetPath())];
  131. $this->connection->update(
  132. $this->resource->getTableName(DbStorage::TABLE_NAME),
  133. $bind,
  134. $this->connection->quoteIdentifier(UrlRewrite::URL_REWRITE_ID) . ' = ' . $urlRewrite->getUrlRewriteId()
  135. );
  136. }
  137. return $this;
  138. }
  139. /**
  140. * @return array|null
  141. */
  142. protected function getStoreIds()
  143. {
  144. if ($this->getScope() == 'stores') {
  145. $storeIds = [$this->getScopeId()];
  146. } elseif ($this->getScope() == 'websites') {
  147. $website = $this->storeManager->getWebsite($this->getScopeId());
  148. $storeIds = array_keys($website->getStoreIds());
  149. $storeIds = array_diff($storeIds, $this->getOverrideStoreIds($storeIds));
  150. } else {
  151. $storeIds = array_keys($this->storeManager->getStores());
  152. $storeIds = array_diff($storeIds, $this->getOverrideStoreIds($storeIds));
  153. }
  154. return $storeIds;
  155. }
  156. /**
  157. * @param array $storeIds
  158. * @return array
  159. */
  160. protected function getOverrideStoreIds($storeIds)
  161. {
  162. $excludeIds = [];
  163. foreach ($storeIds as $storeId) {
  164. $suffix = $this->_config->getValue($this->getPath(), ScopeInterface::SCOPE_STORE, $storeId);
  165. if ($suffix != $this->getOldValue()) {
  166. $excludeIds[] = $storeId;
  167. }
  168. }
  169. return $excludeIds;
  170. }
  171. }