11<?php
22/**
3- * Copyright © Magento, Inc. All rights reserved.
4- * See COPYING.txt for license details .
3+ * Copyright 2011 Adobe
4+ * All Rights Reserved .
55 */
6-
76namespace Magento \Sitemap \Model ;
87
98use Magento \Config \Model \Config \Reader \Source \Deployed \DocumentRoot ;
1211use Magento \Framework \DataObject ;
1312use Magento \Framework \Exception \LocalizedException ;
1413use Magento \Framework \Filesystem ;
14+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
1515use Magento \Framework \UrlInterface ;
1616use Magento \Robots \Model \Config \Value ;
1717use Magento \Sitemap \Model \ItemProvider \ItemProviderInterface ;
1818use Magento \Sitemap \Model \ResourceModel \Sitemap as SitemapResource ;
1919
2020/**
21- * Sitemap model.
22- *
2321 * @method string getSitemapType()
2422 * @method \Magento\Sitemap\Model\Sitemap setSitemapType(string $value)
2523 * @method string getSitemapFilename()
@@ -199,13 +197,11 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
199197 private $ filesystem ;
200198
201199 /**
202- * @var DocumentRoot
200+ * @var WriteInterface
203201 */
204- private $ documentRoot ;
202+ private $ tmpDirectory ;
205203
206204 /**
207- * Initialize dependencies.
208- *
209205 * @param \Magento\Framework\Model\Context $context
210206 * @param \Magento\Framework\Registry $registry
211207 * @param \Magento\Framework\Escaper $escaper
@@ -252,6 +248,7 @@ public function __construct(
252248 $ this ->_sitemapData = $ sitemapData ;
253249 $ this ->filesystem = $ filesystem ;
254250 $ this ->_directory = $ filesystem ->getDirectoryWrite (DirectoryList::PUB );
251+ $ this ->tmpDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::SYS_TMP );
255252 $ this ->_categoryFactory = $ categoryFactory ;
256253 $ this ->_productFactory = $ productFactory ;
257254 $ this ->_cmsFactory = $ cmsFactory ;
@@ -480,13 +477,11 @@ public function generateXml()
480477
481478 $ this ->_finalizeSitemap ();
482479
483- if ($ this ->_sitemapIncrement == 1 ) {
480+ if ($ this ->_sitemapIncrement == - 1 ) {
484481 // In case when only one increment file was created use it as default sitemap
485- $ sitemapPath = $ this ->getSitemapPath () !== null ? rtrim ($ this ->getSitemapPath (), '/ ' ) : '' ;
486- $ path = $ sitemapPath . '/ ' . $ this ->_getCurrentSitemapFilename ($ this ->_sitemapIncrement );
487- $ destination = $ sitemapPath . '/ ' . $ this ->getSitemapFilename ();
488-
489- $ this ->_directory ->renameFile ($ path , $ destination );
482+ $ path = $ this ->getFilePath ($ this ->_getCurrentSitemapFilename ($ this ->_sitemapIncrement ));
483+ $ destination = $ this ->getFilePath ($ this ->getSitemapFilename ());
484+ $ this ->tmpDirectory ->renameFile ($ path , $ destination , $ this ->_directory );
490485 } else {
491486 // Otherwise create index file with list of generated sitemaps
492487 $ this ->_createSitemapIndex ();
@@ -507,10 +502,15 @@ protected function _createSitemapIndex()
507502 {
508503 $ this ->_createSitemap ($ this ->getSitemapFilename (), self ::TYPE_INDEX );
509504 for ($ i = 1 ; $ i <= $ this ->_sitemapIncrement ; $ i ++) {
510- $ xml = $ this ->_getSitemapIndexRow ($ this ->_getCurrentSitemapFilename ($ i ), $ this ->_getCurrentDateTime ());
505+ $ fileName = $ this ->_getCurrentSitemapFilename ($ i );
506+ $ path = $ this ->getFilePath ($ fileName );
507+ $ this ->tmpDirectory ->renameFile ($ path , $ path , $ this ->_directory );
508+ $ xml = $ this ->_getSitemapIndexRow ($ fileName , $ this ->_getCurrentDateTime ());
511509 $ this ->_writeSitemapRow ($ xml );
512510 }
513511 $ this ->_finalizeSitemap (self ::TYPE_INDEX );
512+ $ path = $ this ->getFilePath ($ this ->getSitemapFilename ());
513+ $ this ->tmpDirectory ->renameFile ($ path , $ path , $ this ->_directory );
514514 }
515515
516516 /**
@@ -638,9 +638,8 @@ protected function _createSitemap($fileName = null, $type = self::TYPE_URL)
638638 $ this ->_sitemapIncrement ++;
639639 $ fileName = $ this ->_getCurrentSitemapFilename ($ this ->_sitemapIncrement );
640640 }
641-
642- $ path = ($ this ->getSitemapPath () !== null ? rtrim ($ this ->getSitemapPath (), '/ ' ) : '' ) . '/ ' . $ fileName ;
643- $ this ->_stream = $ this ->_directory ->openFile ($ path );
641+ $ path = $ this ->getFilePath ($ fileName );
642+ $ this ->_stream = $ this ->tmpDirectory ->openFile ($ path );
644643
645644 $ fileHeader = sprintf ($ this ->_tags [$ type ][self ::OPEN_TAG_KEY ], $ type );
646645 $ this ->_stream ->write ($ fileHeader );
@@ -688,6 +687,20 @@ protected function _getCurrentSitemapFilename($index)
688687 . '- ' . $ this ->getStoreId () . '- ' . $ index . '.xml ' ;
689688 }
690689
690+ /**
691+ * Get path to sitemap file
692+ *
693+ * @param string $fileName
694+ * @return string
695+ */
696+ private function getFilePath (string $ fileName ): string
697+ {
698+ $ path = $ this ->getSitemapPath () !== null ? rtrim ($ this ->getSitemapPath (), '/ ' ) : '' ;
699+ $ path .= '/ ' . $ fileName ;
700+
701+ return $ path ;
702+ }
703+
691704 /**
692705 * Get base dir
693706 *
0 commit comments