Robots.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Config backend model for robots.txt
  8. */
  9. namespace Magento\Config\Model\Config\Backend\Admin;
  10. use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
  11. use Magento\Framework\App\ObjectManager;
  12. /**
  13. * @deprecated 100.1.7 robots.txt file is no longer stored in filesystem. It generates as response on request.
  14. */
  15. class Robots extends \Magento\Framework\App\Config\Value
  16. {
  17. /**
  18. * @var \Magento\Framework\Filesystem\Directory\Write
  19. */
  20. protected $_directory;
  21. /**
  22. * @var string
  23. */
  24. protected $_file;
  25. /**
  26. * @param \Magento\Framework\Model\Context $context
  27. * @param \Magento\Framework\Registry $registry
  28. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  29. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  30. * @param \Magento\Framework\Filesystem $filesystem
  31. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  32. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  33. * @param array $data
  34. * @param DocumentRoot $documentRoot
  35. */
  36. public function __construct(
  37. \Magento\Framework\Model\Context $context,
  38. \Magento\Framework\Registry $registry,
  39. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  40. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  41. \Magento\Framework\Filesystem $filesystem,
  42. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  43. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  44. array $data = [],
  45. \Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null
  46. ) {
  47. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  48. $documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
  49. $this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
  50. $this->_file = 'robots.txt';
  51. }
  52. /**
  53. * Return content of default robot.txt
  54. *
  55. * @return bool|string
  56. */
  57. protected function _getDefaultValue()
  58. {
  59. if ($this->_directory->isFile($this->_file)) {
  60. return $this->_directory->readFile($this->_file);
  61. }
  62. return false;
  63. }
  64. /**
  65. * Load default content from robots.txt if customer does not define own
  66. *
  67. * @return $this
  68. */
  69. protected function _afterLoad()
  70. {
  71. if (!(string)$this->getValue()) {
  72. $this->setValue($this->_getDefaultValue());
  73. }
  74. return parent::_afterLoad();
  75. }
  76. /**
  77. * Check and process robots file
  78. *
  79. * @return $this
  80. */
  81. public function afterSave()
  82. {
  83. if ($this->getValue()) {
  84. $this->_directory->writeFile($this->_file, $this->getValue());
  85. }
  86. return parent::afterSave();
  87. }
  88. }