Robots.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sitemap\Block;
  7. use Magento\Framework\DataObject\IdentityInterface;
  8. use Magento\Framework\View\Element\AbstractBlock;
  9. use Magento\Framework\View\Element\Context;
  10. use Magento\Robots\Model\Config\Value;
  11. use Magento\Sitemap\Helper\Data as SitemapHelper;
  12. use Magento\Sitemap\Model\ResourceModel\Sitemap\CollectionFactory;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\Store\Model\StoreResolver;
  15. /**
  16. * Prepares sitemap links to add to the robots.txt file
  17. *
  18. * @api
  19. * @since 100.1.5
  20. */
  21. class Robots extends AbstractBlock implements IdentityInterface
  22. {
  23. /**
  24. * @var CollectionFactory
  25. */
  26. private $sitemapCollectionFactory;
  27. /**
  28. * @var SitemapHelper
  29. */
  30. private $sitemapHelper;
  31. /**
  32. * @var StoreManagerInterface
  33. */
  34. private $storeManager;
  35. /**
  36. * @param Context $context
  37. * @param StoreResolver $storeResolver
  38. * @param CollectionFactory $sitemapCollectionFactory
  39. * @param SitemapHelper $sitemapHelper
  40. * @param StoreManagerInterface $storeManager
  41. * @param array $data
  42. *
  43. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  44. */
  45. public function __construct(
  46. Context $context,
  47. StoreResolver $storeResolver,
  48. CollectionFactory $sitemapCollectionFactory,
  49. SitemapHelper $sitemapHelper,
  50. StoreManagerInterface $storeManager,
  51. array $data = []
  52. ) {
  53. $this->sitemapCollectionFactory = $sitemapCollectionFactory;
  54. $this->sitemapHelper = $sitemapHelper;
  55. $this->storeManager = $storeManager;
  56. parent::__construct($context, $data);
  57. }
  58. /**
  59. * Prepare sitemap links to add to the robots.txt file
  60. *
  61. * Collects sitemap links for all stores of given website.
  62. * Detects if sitemap file information is required to be added to robots.txt
  63. * and adds links for this sitemap files into result data.
  64. *
  65. * @return string
  66. * @since 100.1.5
  67. */
  68. protected function _toHtml()
  69. {
  70. $defaultStore = $this->storeManager->getDefaultStoreView();
  71. /** @var \Magento\Store\Model\Website $website */
  72. $website = $this->storeManager->getWebsite($defaultStore->getWebsiteId());
  73. $storeIds = [];
  74. foreach ($website->getStoreIds() as $storeId) {
  75. if ((bool)$this->sitemapHelper->getEnableSubmissionRobots($storeId)) {
  76. $storeIds[] = (int)$storeId;
  77. }
  78. }
  79. $links = [];
  80. if ($storeIds) {
  81. $links = array_merge($links, $this->getSitemapLinks($storeIds));
  82. }
  83. return $links ? implode(PHP_EOL, $links) . PHP_EOL : '';
  84. }
  85. /**
  86. * Retrieve sitemap links for given store
  87. *
  88. * Gets the names of sitemap files that linked with given store,
  89. * and adds links for this sitemap files into result array.
  90. *
  91. * @param int[] $storeIds
  92. * @return array
  93. * @since 100.1.5
  94. */
  95. protected function getSitemapLinks(array $storeIds)
  96. {
  97. $sitemapLinks = [];
  98. /** @var \Magento\Sitemap\Model\ResourceModel\Sitemap\Collection $collection */
  99. $collection = $this->sitemapCollectionFactory->create();
  100. $collection->addStoreFilter($storeIds);
  101. foreach ($collection as $sitemap) {
  102. /** @var \Magento\Sitemap\Model\Sitemap $sitemap */
  103. $sitemapFilename = $sitemap->getSitemapFilename();
  104. $sitemapPath = $sitemap->getSitemapPath();
  105. $sitemapUrl = $sitemap->getSitemapUrl($sitemapPath, $sitemapFilename);
  106. $sitemapLinks[$sitemapUrl] = 'Sitemap: ' . $sitemapUrl;
  107. }
  108. return $sitemapLinks;
  109. }
  110. /**
  111. * Get unique page cache identities
  112. *
  113. * @return array
  114. * @since 100.1.5
  115. */
  116. public function getIdentities()
  117. {
  118. return [
  119. Value::CACHE_TAG . '_' . $this->storeManager->getDefaultStoreView()->getId(),
  120. ];
  121. }
  122. }