Skip to content

Commit

Permalink
magento#10045 backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrkwiecinski committed Jul 26, 2017
1 parent b251c7e commit c4885c9
Showing 1 changed file with 71 additions and 6 deletions.
77 changes: 71 additions & 6 deletions app/code/Magento/Sitemap/Model/Sitemap.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\DataObject;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\UrlInterface;
use Magento\Robots\Model\Config\Value;
Expand Down Expand Up @@ -174,6 +175,13 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
*/
private $configReader;

/**
* Sitemap Item Factory
*
* @var SitemapItemInterfaceFactory|null
*/
private $sitemapItemFactory;

/**
* Initialize dependencies.
*
Expand All @@ -195,6 +203,7 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
* @param DocumentRoot|null $documentRoot
* @param ItemResolverInterface|null $itemResolver
* @param SitemapConfigReaderInterface|null $configReader
* @param SitemapItemInterfaceFactory|null $sitemapItemFactory
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
*/
public function __construct(
Expand All @@ -215,11 +224,13 @@ public function __construct(
array $data = [],
\Magento\Config\Model\Config\Reader\Source\Deployed\DocumentRoot $documentRoot = null,
ItemResolverInterface $itemResolver = null,
SitemapConfigReaderInterface $configReader = null
SitemapConfigReaderInterface $configReader = null,
SitemapItemInterfaceFactory $sitemapItemFactory = null
) {
$objectManager = ObjectManager::getInstance();
$this->_escaper = $escaper;
$this->_sitemapData = $sitemapData;
$documentRoot = $documentRoot ?: ObjectManager::getInstance()->get(DocumentRoot::class);
$documentRoot = $documentRoot ?: $objectManager->get(DocumentRoot::class);
$this->_directory = $filesystem->getDirectoryWrite($documentRoot->getPath());
$this->_categoryFactory = $categoryFactory;
$this->_productFactory = $productFactory;
Expand All @@ -228,10 +239,11 @@ public function __construct(
$this->_storeManager = $storeManager;
$this->_request = $request;
$this->dateTime = $dateTime;
$this->itemResolver = $itemResolver ?: ObjectManager::getInstance()->get(ItemResolverInterface::class);
$this->configReader = $configReader ?: ObjectManager::getInstance()->get(SitemapConfigReaderInterface::class);

$this->itemResolver = $itemResolver ?: $objectManager->get(ItemResolverInterface::class);
$this->configReader = $configReader ?: $objectManager->get(SitemapConfigReaderInterface::class);
$this->sitemapItemFactory = $sitemapItemFactory ?: $objectManager->get(SitemapItemInterfaceFactory::class);
parent::__construct($context, $registry, $resource, $resourceCollection, $data);

}

/**
Expand Down Expand Up @@ -259,14 +271,43 @@ protected function _getStream()
}
}

/**
* Add a sitemap item to the array of sitemap items
*
* @param DataObject $sitemapItem
* @return $this
* @deprecated
*/
public function addSitemapItem(DataObject $sitemapItem)
{
$this->_sitemapItems[] = $sitemapItem;

return $this;
}

/**
* Collect all sitemap items
*
* @return void
* @deprecated
*/
public function collectSitemapItems()
{

}

/**
* Initialize sitemap
*
* @return void
*/
protected function _initSitemapItems()
{
$this->_sitemapItems = $this->itemResolver->getItems($this->getStoreId());
$sitemapItems = $this->itemResolver->getItems($this->getStoreId());
$this->collectSitemapItems();
$mappedItems = $this->mapToSitemapItem();
$this->_sitemapItems = array_merge($sitemapItems, $mappedItems);


$this->_tags = [
self::TYPE_INDEX => [
Expand Down Expand Up @@ -740,6 +781,30 @@ private function _findNewLinesDelimiter($text)
return PHP_EOL;
}

/**
* Sitemap item mapper for backwards compatibility
*
* @return array
*/
private function mapToSitemapItem()
{
$items = [];

foreach ($this->_sitemapItems as $data) {
foreach($data->getCollection() as $item) {
$items[] = $this->sitemapItemFactory->create([
'url' => $item->getUrl(),
'updatedAt' => $item->getUpdatedAt(),
'images' => $item->getImages(),
'priority' => $data->getPriority(),
'changeFrequency' => $data->getChangeFrequency(),
]);
}
}

return $items;
}

/**
* Get unique page cache identities
*
Expand Down

0 comments on commit c4885c9

Please sign in to comment.