forked from magento/magento2
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magento#10045 Sitemap add item refactoring
Add injectable item resolver to load sitemap items
- Loading branch information
1 parent
ce5dbd5
commit 9867aca
Showing
14 changed files
with
1,145 additions
and
75 deletions.
There are no files selected for viewing
109 changes: 109 additions & 0 deletions
109
app/code/Magento/Sitemap/Model/CategorySitemapItemResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Sitemap\Model; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Sitemap\Model\ResourceModel\Catalog\CategoryFactory; | ||
use Magento\Store\Model\ScopeInterface; | ||
|
||
class CategorySitemapItemResolver implements SitemapItemResolverInterface | ||
{ | ||
/**#@+ | ||
* Xpath config settings | ||
*/ | ||
const XML_PATH_CATEGORY_CHANGEFREQ = 'sitemap/category/changefreq'; | ||
const XML_PATH_CATEGORY_PRIORITY = 'sitemap/category/priority'; | ||
|
||
/**#@-*/ | ||
|
||
/** | ||
* Category factory | ||
* | ||
* @var CategoryFactory | ||
*/ | ||
private $categoryFactory; | ||
|
||
/** | ||
* Sitemap item factory | ||
* | ||
* @var SitemapItemInterfaceFactory | ||
*/ | ||
private $itemFactory; | ||
|
||
/** | ||
* Scope config | ||
* | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* CategorySitemapItemResolver constructor. | ||
* | ||
* @param ScopeConfigInterface $scopeConfig | ||
* @param CategoryFactory $categoryFactory | ||
* @param SitemapItemInterfaceFactory $itemFactory | ||
*/ | ||
public function __construct( | ||
ScopeConfigInterface $scopeConfig, | ||
CategoryFactory $categoryFactory, | ||
SitemapItemInterfaceFactory $itemFactory | ||
) { | ||
$this->categoryFactory = $categoryFactory; | ||
$this->itemFactory = $itemFactory; | ||
$this->scopeConfig = $scopeConfig; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems($storeId) | ||
{ | ||
$collection = $this->categoryFactory->create()->getCollection($storeId); | ||
$items = array_map(function($item) use ($storeId) { | ||
return $this->itemFactory->create([ | ||
'url' => $item->getUrl(), | ||
'updatedAt' => $item->getUpdatedAt(), | ||
'images' => $item->getImages(), | ||
'priority' => $this->getCategoryPriority($storeId), | ||
'changeFrequency' => $this->getCategoryChangeFrequency($storeId), | ||
]); | ||
}, $collection); | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Get page priority | ||
* | ||
* @param int $storeId | ||
* @return string | ||
*/ | ||
private function getCategoryPriority($storeId) | ||
{ | ||
return (string)$this->scopeConfig->getValue( | ||
self::XML_PATH_CATEGORY_PRIORITY, | ||
ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
|
||
/** | ||
* Get page change frequency | ||
* | ||
* @param int $storeId | ||
* @return string | ||
*/ | ||
private function getCategoryChangeFrequency($storeId) | ||
{ | ||
return (string)$this->scopeConfig->getValue( | ||
self::XML_PATH_CATEGORY_CHANGEFREQ, | ||
ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
} |
113 changes: 113 additions & 0 deletions
113
app/code/Magento/Sitemap/Model/CmsPageSitemapItemResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Sitemap\Model; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Store\Model\ScopeInterface; | ||
use Magento\Sitemap\Model\ResourceModel\Cms\PageFactory; | ||
|
||
class CmsPageSitemapItemResolver implements SitemapItemResolverInterface | ||
{ | ||
|
||
/**#@+ | ||
* Xpath config settings | ||
*/ | ||
const XML_PATH_PAGE_CHANGEFREQ = 'sitemap/page/changefreq'; | ||
const XML_PATH_PAGE_PRIORITY = 'sitemap/page/priority'; | ||
|
||
/**#@-*/ | ||
|
||
/** | ||
* Cms page factory | ||
* | ||
* @var PageFactory | ||
*/ | ||
private $cmsPageFactory; | ||
|
||
/** | ||
* Sitemap item factory | ||
* | ||
* @var SitemapItemInterfaceFactory | ||
*/ | ||
private $itemFactory; | ||
|
||
/** | ||
* Scope config | ||
* | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* CmsPageSitemapItemResolver constructor. | ||
* | ||
* @param ScopeConfigInterface $scopeConfig | ||
* @param PageFactory $cmsPageFactory | ||
* @param SitemapItemInterfaceFactory $itemFactory | ||
*/ | ||
public function __construct( | ||
ScopeConfigInterface $scopeConfig, | ||
PageFactory $cmsPageFactory, | ||
SitemapItemInterfaceFactory $itemFactory | ||
) { | ||
$this->scopeConfig = $scopeConfig; | ||
$this->cmsPageFactory = $cmsPageFactory; | ||
$this->itemFactory = $itemFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems($storeId) | ||
{ | ||
$collection = $this->cmsPageFactory->create()->getCollection($storeId); | ||
var_dump($collection); | ||
$items = array_map(function($item) use ($storeId) { | ||
return $this->itemFactory->create([ | ||
'url' => $item->getUrl(), | ||
'updatedAt' => $item->getUpdatedAt(), | ||
'images' => $item->getImages(), | ||
'priority' => $this->getPagePriority($storeId), | ||
'changeFrequency' => $this->getPageChangeFrequency($storeId), | ||
]); | ||
}, $collection); | ||
|
||
var_dump($items); | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Get page priority | ||
* | ||
* @param int $storeId | ||
* @return string | ||
*/ | ||
private function getPagePriority($storeId) | ||
{ | ||
return (string)$this->scopeConfig->getValue( | ||
self::XML_PATH_PAGE_PRIORITY, | ||
ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
|
||
/** | ||
* Get page change frequency | ||
* | ||
* @param int $storeId | ||
* @return string | ||
*/ | ||
private function getPageChangeFrequency($storeId) | ||
{ | ||
return (string)$this->scopeConfig->getValue( | ||
self::XML_PATH_PAGE_CHANGEFREQ, | ||
ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
app/code/Magento/Sitemap/Model/CompositeSitemapItemResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Sitemap\Model; | ||
|
||
|
||
class CompositeSitemapItemResolver implements SitemapItemResolverInterface | ||
{ | ||
/** | ||
* Item resolvers | ||
* | ||
* @var SitemapItemResolverInterface[] | ||
*/ | ||
private $itemResolvers; | ||
|
||
/** | ||
* CompositeSitemapItemResolver constructor. | ||
* | ||
* @param SitemapItemResolverInterface[] $itemResolvers | ||
*/ | ||
public function __construct($itemResolvers = []) | ||
{ | ||
$this->itemResolvers = $itemResolvers; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems($storeId) | ||
{ | ||
$items = []; | ||
|
||
foreach ($this->itemResolvers as $resolver) { | ||
foreach ($resolver->getItems($storeId) as $item) { | ||
$items[] = $item; | ||
} | ||
} | ||
|
||
return $items; | ||
} | ||
} |
108 changes: 108 additions & 0 deletions
108
app/code/Magento/Sitemap/Model/ProductSitemapItemResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
/** | ||
* Copyright © Magento, Inc. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
|
||
namespace Magento\Sitemap\Model; | ||
|
||
use Magento\Framework\App\Config\ScopeConfigInterface; | ||
use Magento\Sitemap\Model\ResourceModel\Catalog\ProductFactory; | ||
use Magento\Store\Model\ScopeInterface; | ||
|
||
class ProductSitemapItemResolver implements SitemapItemResolverInterface | ||
{ | ||
/**#@+ | ||
* Xpath config settings | ||
*/ | ||
const XML_PATH_PRODUCT_CHANGEFREQ = 'sitemap/product/changefreq'; | ||
const XML_PATH_PRODUCT_PRIORITY = 'sitemap/product/priority'; | ||
/**#@-*/ | ||
|
||
/** | ||
* Product factory | ||
* | ||
* @var ProductFactory | ||
*/ | ||
private $productFactory; | ||
|
||
/** | ||
* Sitemap item factory | ||
* | ||
* @var SitemapItemInterfaceFactory | ||
*/ | ||
private $itemFactory; | ||
|
||
/** | ||
* Scope config | ||
* | ||
* @var ScopeConfigInterface | ||
*/ | ||
private $scopeConfig; | ||
|
||
/** | ||
* ProductSitemapItemResolver constructor. | ||
* | ||
* @param ScopeConfigInterface $scopeConfig | ||
* @param ProductFactory $productFactory | ||
* @param SitemapItemInterfaceFactory $itemFactory | ||
*/ | ||
public function __construct( | ||
ScopeConfigInterface $scopeConfig, | ||
ProductFactory $productFactory, | ||
SitemapItemInterfaceFactory $itemFactory | ||
) { | ||
$this->scopeConfig = $scopeConfig; | ||
$this->productFactory = $productFactory; | ||
$this->itemFactory = $itemFactory; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getItems($storeId) | ||
{ | ||
$collection = $this->productFactory->create()->getCollection($storeId); | ||
$items = array_map(function($item) use ($storeId) { | ||
return $this->itemFactory->create([ | ||
'url' => $item->getUrl(), | ||
'updatedAt' => $item->getUpdatedAt(), | ||
'images' => $item->getImages(), | ||
'priority' => $this->getProductPriority($storeId), | ||
'changeFrequency' => $this->getProductChangeFrequency($storeId), | ||
]); | ||
}, $collection); | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Get page priority | ||
* | ||
* @param int $storeId | ||
* @return string | ||
*/ | ||
private function getProductPriority($storeId) | ||
{ | ||
return (string)$this->scopeConfig->getValue( | ||
self::XML_PATH_PRODUCT_PRIORITY, | ||
ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
|
||
/** | ||
* Get page change frequency | ||
* | ||
* @param int $storeId | ||
* @return string | ||
*/ | ||
private function getProductChangeFrequency($storeId) | ||
{ | ||
return (string)$this->scopeConfig->getValue( | ||
self::XML_PATH_PRODUCT_CHANGEFREQ, | ||
ScopeInterface::SCOPE_STORE, | ||
$storeId | ||
); | ||
} | ||
} |
Oops, something went wrong.