1
1
<?php
2
2
/**
3
- * Copyright © Magento, Inc. All rights reserved.
4
- * See COPYING.txt for license details .
3
+ * Copyright 2011 Adobe
4
+ * All Rights Reserved .
5
5
*/
6
-
7
6
namespace Magento \Sitemap \Model ;
8
7
9
8
use Magento \Config \Model \Config \Reader \Source \Deployed \DocumentRoot ;
12
11
use Magento \Framework \DataObject ;
13
12
use Magento \Framework \Exception \LocalizedException ;
14
13
use Magento \Framework \Filesystem ;
14
+ use Magento \Framework \Filesystem \Directory \WriteInterface ;
15
15
use Magento \Framework \UrlInterface ;
16
16
use Magento \Robots \Model \Config \Value ;
17
17
use Magento \Sitemap \Model \ItemProvider \ItemProviderInterface ;
18
18
use Magento \Sitemap \Model \ResourceModel \Sitemap as SitemapResource ;
19
19
20
20
/**
21
- * Sitemap model.
22
- *
23
21
* @method string getSitemapType()
24
22
* @method \Magento\Sitemap\Model\Sitemap setSitemapType(string $value)
25
23
* @method string getSitemapFilename()
@@ -199,13 +197,11 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
199
197
private $ filesystem ;
200
198
201
199
/**
202
- * @var DocumentRoot
200
+ * @var WriteInterface
203
201
*/
204
- private $ documentRoot ;
202
+ private $ tmpDirectory ;
205
203
206
204
/**
207
- * Initialize dependencies.
208
- *
209
205
* @param \Magento\Framework\Model\Context $context
210
206
* @param \Magento\Framework\Registry $registry
211
207
* @param \Magento\Framework\Escaper $escaper
@@ -252,6 +248,7 @@ public function __construct(
252
248
$ this ->_sitemapData = $ sitemapData ;
253
249
$ this ->filesystem = $ filesystem ;
254
250
$ this ->_directory = $ filesystem ->getDirectoryWrite (DirectoryList::PUB );
251
+ $ this ->tmpDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::SYS_TMP );
255
252
$ this ->_categoryFactory = $ categoryFactory ;
256
253
$ this ->_productFactory = $ productFactory ;
257
254
$ this ->_cmsFactory = $ cmsFactory ;
@@ -480,13 +477,11 @@ public function generateXml()
480
477
481
478
$ this ->_finalizeSitemap ();
482
479
483
- if ($ this ->_sitemapIncrement == 1 ) {
480
+ if ($ this ->_sitemapIncrement == - 1 ) {
484
481
// 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 );
490
485
} else {
491
486
// Otherwise create index file with list of generated sitemaps
492
487
$ this ->_createSitemapIndex ();
@@ -507,10 +502,15 @@ protected function _createSitemapIndex()
507
502
{
508
503
$ this ->_createSitemap ($ this ->getSitemapFilename (), self ::TYPE_INDEX );
509
504
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 ());
511
509
$ this ->_writeSitemapRow ($ xml );
512
510
}
513
511
$ this ->_finalizeSitemap (self ::TYPE_INDEX );
512
+ $ path = $ this ->getFilePath ($ this ->getSitemapFilename ());
513
+ $ this ->tmpDirectory ->renameFile ($ path , $ path , $ this ->_directory );
514
514
}
515
515
516
516
/**
@@ -638,9 +638,8 @@ protected function _createSitemap($fileName = null, $type = self::TYPE_URL)
638
638
$ this ->_sitemapIncrement ++;
639
639
$ fileName = $ this ->_getCurrentSitemapFilename ($ this ->_sitemapIncrement );
640
640
}
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 );
644
643
645
644
$ fileHeader = sprintf ($ this ->_tags [$ type ][self ::OPEN_TAG_KEY ], $ type );
646
645
$ this ->_stream ->write ($ fileHeader );
@@ -688,6 +687,20 @@ protected function _getCurrentSitemapFilename($index)
688
687
. '- ' . $ this ->getStoreId () . '- ' . $ index . '.xml ' ;
689
688
}
690
689
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
+
691
704
/**
692
705
* Get base dir
693
706
*
0 commit comments