diff --git a/.gitignore b/.gitignore index b92b9edba654c..323bb78787b0f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,8 +5,12 @@ /.settings atlassian* /nbproject +/robots.txt +/pub/robots.txt /sitemap /sitemap.xml +/pub/sitemap +/pub/sitemap.xml /.idea /.gitattributes /app/config_sandbox diff --git a/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php b/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php index deb9daf13df86..b8cd9a47476d5 100644 --- a/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php +++ b/app/code/Magento/Config/Model/Config/Backend/Admin/Robots.php @@ -9,7 +9,8 @@ */ namespace Magento\Config\Model\Config\Backend\Admin; -use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot; +use Magento\Framework\App\ObjectManager; class Robots extends \Magento\Framework\App\Config\Value { @@ -32,6 +33,7 @@ class Robots extends \Magento\Framework\App\Config\Value * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection * @param array $data + * @param DocumentRoot $documentRoot */ public function __construct( \Magento\Framework\Model\Context $context, @@ -41,10 +43,13 @@ public function __construct( \Magento\Framework\Filesystem $filesystem, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, - array $data = [] + array $data = [], + \Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null ) { parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data); - $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); + + $documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class); + $this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath()); $this->_file = 'robots.txt'; } diff --git a/app/code/Magento/Config/Model/Config/Reader/Source/Deployed/DocumentRoot.php b/app/code/Magento/Config/Model/Config/Reader/Source/Deployed/DocumentRoot.php new file mode 100644 index 0000000000000..81fb480280868 --- /dev/null +++ b/app/code/Magento/Config/Model/Config/Reader/Source/Deployed/DocumentRoot.php @@ -0,0 +1,55 @@ +config = $config; + } + + /** + * A shortcut to load the document root path from the DirectoryList based on the + * deployment configuration. + * + * @return string + */ + public function getPath() + { + return $this->isPub() ? DirectoryList::PUB : DirectoryList::ROOT; + } + + /** + * Returns whether the deployment configuration specifies that the document root is + * in the pub/ folder. This affects ares such as sitemaps and robots.txt (and will + * likely be extended to control other areas). + * + * @return bool + */ + public function isPub() + { + return (bool)$this->config->get(ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB); + } +} diff --git a/app/code/Magento/Config/Test/Unit/Model/Config/Reader/Source/Deployed/DocumentRootTest.php b/app/code/Magento/Config/Test/Unit/Model/Config/Reader/Source/Deployed/DocumentRootTest.php new file mode 100644 index 0000000000000..4d98d0061d176 --- /dev/null +++ b/app/code/Magento/Config/Test/Unit/Model/Config/Reader/Source/Deployed/DocumentRootTest.php @@ -0,0 +1,74 @@ +configMock = $this->getMockBuilder(DeploymentConfig::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->documentRoot = new Reader\Source\Deployed\DocumentRoot($this->configMock); + } + + /** + * Ensures that the path returned matches the pub/ path. + */ + public function testGetPath() + { + $this->configMockSetForDocumentRootIsPub(); + + $this->assertSame(DirectoryList::PUB, $this->documentRoot->getPath()); + } + + /** + * Ensures that the deployment configuration returns the mocked value for + * the pub/ folder. + */ + public function testIsPub() + { + $this->configMockSetForDocumentRootIsPub(); + + $this->assertSame(true, $this->documentRoot->isPub()); + } + + private function configMockSetForDocumentRootIsPub() + { + $this->configMock->expects($this->any()) + ->method('get') + ->willReturnMap([ + [ + ConfigOptionsListConstants::CONFIG_PATH_DOCUMENT_ROOT_IS_PUB, + null, + true + ], + ]); + } +} diff --git a/app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php b/app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php index 176ff58aa0bfc..ffc2bf5f6d1cf 100644 --- a/app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php +++ b/app/code/Magento/Sitemap/Block/Adminhtml/Grid/Renderer/Link.php @@ -11,6 +11,8 @@ namespace Magento\Sitemap\Block\Adminhtml\Grid\Renderer; use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot; +use Magento\Framework\App\ObjectManager; class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRenderer { @@ -24,20 +26,29 @@ class Link extends \Magento\Backend\Block\Widget\Grid\Column\Renderer\AbstractRe */ protected $_sitemapFactory; + /** + * @var DocumentRoot + */ + protected $documentRoot; + /** * @param \Magento\Backend\Block\Context $context * @param \Magento\Sitemap\Model\SitemapFactory $sitemapFactory * @param \Magento\Framework\Filesystem $filesystem * @param array $data + * @param DocumentRoot $documentRoot */ public function __construct( \Magento\Backend\Block\Context $context, \Magento\Sitemap\Model\SitemapFactory $sitemapFactory, \Magento\Framework\Filesystem $filesystem, - array $data = [] + array $data = [], + DocumentRoot $documentRoot = null ) { $this->_sitemapFactory = $sitemapFactory; $this->_filesystem = $filesystem; + $this->documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class); + parent::__construct($context, $data); } @@ -54,7 +65,8 @@ public function render(\Magento\Framework\DataObject $row) $url = $this->escapeHtml($sitemap->getSitemapUrl($row->getSitemapPath(), $row->getSitemapFilename())); $fileName = preg_replace('/^\//', '', $row->getSitemapPath() . $row->getSitemapFilename()); - $directory = $this->_filesystem->getDirectoryRead(DirectoryList::ROOT); + $documentRootPath = $this->documentRoot->getPath(); + $directory = $this->_filesystem->getDirectoryRead($documentRootPath); if ($directory->isFile($fileName)) { return sprintf('%1$s', $url); } diff --git a/app/code/Magento/Sitemap/Model/Sitemap.php b/app/code/Magento/Sitemap/Model/Sitemap.php index 12e65b7a3490c..6ba2a7146fe0c 100644 --- a/app/code/Magento/Sitemap/Model/Sitemap.php +++ b/app/code/Magento/Sitemap/Model/Sitemap.php @@ -7,8 +7,8 @@ // @codingStandardsIgnoreFile namespace Magento\Sitemap\Model; - -use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot; +use Magento\Framework\App\ObjectManager; /** * Sitemap model @@ -179,11 +179,13 @@ public function __construct( \Magento\Framework\Stdlib\DateTime $dateTime, \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null, \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null, + \Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null, array $data = [] ) { $this->_escaper = $escaper; $this->_sitemapData = $sitemapData; - $this->_directory = $filesystem->getDirectoryWrite(DirectoryList::ROOT); + $documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class); + $this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath()); $this->_categoryFactory = $categoryFactory; $this->_productFactory = $productFactory; $this->_cmsFactory = $cmsFactory; diff --git a/dev/tests/integration/testsuite/Magento/Config/Model/Config/Backend/Admin/RobotsTest.php b/dev/tests/integration/testsuite/Magento/Config/Model/Config/Backend/Admin/RobotsTest.php index cb25bfb3d90ad..6e343033fa0d7 100644 --- a/dev/tests/integration/testsuite/Magento/Config/Model/Config/Backend/Admin/RobotsTest.php +++ b/dev/tests/integration/testsuite/Magento/Config/Model/Config/Backend/Admin/RobotsTest.php @@ -5,7 +5,7 @@ */ namespace Magento\Config\Model\Config\Backend\Admin; -use Magento\Framework\App\Filesystem\DirectoryList; +use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot; /** * @magentoAppArea adminhtml @@ -33,11 +33,11 @@ protected function setUp() $this->model = $objectManager->create(\Magento\Config\Model\Config\Backend\Admin\Robots::class); $this->model->setPath('design/search_engine_robots/custom_instructions'); $this->model->afterLoad(); + + $documentRootPath = $objectManager->get(DocumentRoot::class)->getPath(); $this->rootDirectory = $objectManager->get( \Magento\Framework\Filesystem::class - )->getDirectoryRead( - DirectoryList::ROOT - ); + )->getDirectoryRead($documentRootPath); } /** diff --git a/generated/.htaccess b/generated/.htaccess index 896fbc5a341ea..7d3aaf1a903f7 100644 --- a/generated/.htaccess +++ b/generated/.htaccess @@ -1,2 +1,2 @@ -Order deny,allow +Order allow,deny Deny from all \ No newline at end of file diff --git a/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php b/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php index c53b576ee3b24..01c2e6b26fdfe 100644 --- a/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php +++ b/lib/internal/Magento/Framework/Config/ConfigOptionsListConstants.php @@ -30,6 +30,8 @@ class ConfigOptionsListConstants const CONFIG_PATH_DB = 'db'; const CONFIG_PATH_RESOURCE = 'resource'; const CONFIG_PATH_CACHE_TYPES = 'cache_types'; + const CONFIG_PATH_DOCUMENT_ROOT_IS_PUB = 'directories/document_root_is_pub'; + /**#@-*/ /**#@+ diff --git a/vendor/.htaccess b/vendor/.htaccess index cb24fd7fc0b3a..7d3aaf1a903f7 100644 --- a/vendor/.htaccess +++ b/vendor/.htaccess @@ -1,2 +1,2 @@ Order allow,deny -Deny from all +Deny from all \ No newline at end of file