Data.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Robots\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\Robots\Model\Robots;
  12. use Magento\Store\Model\StoreResolver;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. /**
  15. * Robots Block Class.
  16. *
  17. * Prepares base content for robots.txt and implements Page Cache functionality.
  18. *
  19. * @api
  20. * @since 100.1.0
  21. */
  22. class Data extends AbstractBlock implements IdentityInterface
  23. {
  24. /**
  25. * @var Robots
  26. */
  27. private $robots;
  28. /**
  29. * @var StoreManagerInterface
  30. */
  31. private $storeManager;
  32. /**
  33. * @param Context $context
  34. * @param Robots $robots
  35. * @param StoreResolver $storeResolver
  36. * @param StoreManagerInterface|null $storeManager
  37. * @param array $data
  38. *
  39. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  40. */
  41. public function __construct(
  42. Context $context,
  43. Robots $robots,
  44. StoreResolver $storeResolver,
  45. StoreManagerInterface $storeManager = null,
  46. array $data = []
  47. ) {
  48. $this->robots = $robots;
  49. $this->storeManager = $storeManager ?: \Magento\Framework\App\ObjectManager::getInstance()
  50. ->get(StoreManagerInterface::class);
  51. parent::__construct($context, $data);
  52. }
  53. /**
  54. * Retrieve base content for robots.txt file
  55. *
  56. * @return string
  57. * @since 100.1.0
  58. */
  59. protected function _toHtml()
  60. {
  61. return $this->robots->getData() . PHP_EOL;
  62. }
  63. /**
  64. * Get unique page cache identities
  65. *
  66. * @return array
  67. * @since 100.1.0
  68. */
  69. public function getIdentities()
  70. {
  71. return [
  72. Value::CACHE_TAG . '_' . $this->storeManager->getStore()->getId(),
  73. ];
  74. }
  75. }