Skip to content

Commit c4885c9

Browse files
#10045 backwards compatibility
1 parent b251c7e commit c4885c9

File tree

1 file changed

+71
-6
lines changed

1 file changed

+71
-6
lines changed

app/code/Magento/Sitemap/Model/Sitemap.php

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
1212
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\DataObject;
1314
use Magento\Framework\Exception\LocalizedException;
1415
use Magento\Framework\UrlInterface;
1516
use Magento\Robots\Model\Config\Value;
@@ -174,6 +175,13 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
174175
*/
175176
private $configReader;
176177

178+
/**
179+
* Sitemap Item Factory
180+
*
181+
* @var SitemapItemInterfaceFactory|null
182+
*/
183+
private $sitemapItemFactory;
184+
177185
/**
178186
* Initialize dependencies.
179187
*
@@ -195,6 +203,7 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
195203
* @param DocumentRoot|null $documentRoot
196204
* @param ItemResolverInterface|null $itemResolver
197205
* @param SitemapConfigReaderInterface|null $configReader
206+
* @param SitemapItemInterfaceFactory|null $sitemapItemFactory
198207
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
199208
*/
200209
public function __construct(
@@ -215,11 +224,13 @@ public function __construct(
215224
array $data = [],
216225
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null,
217226
ItemResolverInterface $itemResolver = null,
218-
SitemapConfigReaderInterface $configReader = null
227+
SitemapConfigReaderInterface $configReader = null,
228+
SitemapItemInterfaceFactory $sitemapItemFactory = null
219229
) {
230+
$objectManager = ObjectManager::getInstance();
220231
$this->_escaper = $escaper;
221232
$this->_sitemapData = $sitemapData;
222-
$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
233+
$documentRoot = $documentRoot ?: $objectManager->get(DocumentRoot::class);
223234
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
224235
$this->_categoryFactory = $categoryFactory;
225236
$this->_productFactory = $productFactory;
@@ -228,10 +239,11 @@ public function __construct(
228239
$this->_storeManager = $storeManager;
229240
$this->_request = $request;
230241
$this->dateTime = $dateTime;
231-
$this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
232-
$this->configReader = $configReader ?: ObjectManager::getInstance()->get(SitemapConfigReaderInterface::class);
233-
242+
$this->itemResolver = $itemResolver ?: $objectManager->get(ItemResolverInterface::class);
243+
$this->configReader = $configReader ?: $objectManager->get(SitemapConfigReaderInterface::class);
244+
$this->sitemapItemFactory = $sitemapItemFactory ?: $objectManager->get(SitemapItemInterfaceFactory::class);
234245
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
246+
235247
}
236248

237249
/**
@@ -259,14 +271,43 @@ protected function _getStream()
259271
}
260272
}
261273

274+
/**
275+
* Add a sitemap item to the array of sitemap items
276+
*
277+
* @param DataObject $sitemapItem
278+
* @return $this
279+
* @deprecated
280+
*/
281+
public function addSitemapItem(DataObject $sitemapItem)
282+
{
283+
$this->_sitemapItems[] = $sitemapItem;
284+
285+
return $this;
286+
}
287+
288+
/**
289+
* Collect all sitemap items
290+
*
291+
* @return void
292+
* @deprecated
293+
*/
294+
public function collectSitemapItems()
295+
{
296+
297+
}
298+
262299
/**
263300
* Initialize sitemap
264301
*
265302
* @return void
266303
*/
267304
protected function _initSitemapItems()
268305
{
269-
$this->_sitemapItems = $this->itemResolver->getItems($this->getStoreId());
306+
$sitemapItems = $this->itemResolver->getItems($this->getStoreId());
307+
$this->collectSitemapItems();
308+
$mappedItems = $this->mapToSitemapItem();
309+
$this->_sitemapItems = array_merge($sitemapItems, $mappedItems);
310+
270311

271312
$this->_tags = [
272313
self::TYPE_INDEX => [
@@ -740,6 +781,30 @@ private function _findNewLinesDelimiter($text)
740781
return PHP_EOL;
741782
}
742783

784+
/**
785+
* Sitemap item mapper for backwards compatibility
786+
*
787+
* @return array
788+
*/
789+
private function mapToSitemapItem()
790+
{
791+
$items = [];
792+
793+
foreach ($this->_sitemapItems as $data) {
794+
foreach($data->getCollection() as $item) {
795+
$items[] = $this->sitemapItemFactory->create([
796+
'url' => $item->getUrl(),
797+
'updatedAt' => $item->getUpdatedAt(),
798+
'images' => $item->getImages(),
799+
'priority' => $data->getPriority(),
800+
'changeFrequency' => $data->getChangeFrequency(),
801+
]);
802+
}
803+
}
804+
805+
return $items;
806+
}
807+
743808
/**
744809
* Get unique page cache identities
745810
*

0 commit comments

Comments
 (0)