/
CategoryUrlPathGenerator.php
151 lines (137 loc) · 4.45 KB
/
CategoryUrlPathGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\CatalogUrlRewrite\Model;
use Magento\Catalog\Api\CategoryRepositoryInterface;
use Magento\Catalog\Model\Category;
class CategoryUrlPathGenerator
{
/**
* Minimal category level that can be considered for generate path
*/
const MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING = 3;
/**
* XML path for category url suffix
*/
const XML_PATH_CATEGORY_URL_SUFFIX = 'catalog/seo/category_url_suffix';
/**
* Cache for category rewrite suffix
*
* @var array
*/
protected $categoryUrlSuffix = [];
/** @var \Magento\Framework\App\Config\ScopeConfigInterface */
protected $scopeConfig;
/** @var \Magento\Store\Model\StoreManagerInterface */
protected $storeManager;
/**
* @var CategoryRepositoryInterface
*/
protected $categoryRepository;
/**
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param CategoryRepositoryInterface $categoryRepository
*/
public function __construct(
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
CategoryRepositoryInterface $categoryRepository
) {
$this->storeManager = $storeManager;
$this->scopeConfig = $scopeConfig;
$this->categoryRepository = $categoryRepository;
}
/**
* Build category URL path
*
* @param \Magento\Catalog\Api\Data\CategoryInterface|\Magento\Framework\Model\AbstractModel $category
* @return string
*/
public function getUrlPath($category)
{
if (in_array($category->getParentId(), [Category::ROOT_CATEGORY_ID, Category::TREE_ROOT_ID])) {
return '';
}
$path = $category->getUrlPath();
if ($path !== null && !$category->dataHasChangedFor('url_key') && !$category->dataHasChangedFor('parent_id')) {
return $path;
}
$path = $category->getUrlKey();
if ($path === false) {
return $category->getUrlPath();
}
if ($this->isNeedToGenerateUrlPathForParent($category)) {
$parentPath = $this->getUrlPath(
$this->categoryRepository->get($category->getParentId(), $category->getStoreId())
);
$path = $parentPath === '' ? $path : $parentPath . '/' . $path;
}
return $path;
}
/**
* @param \Magento\Catalog\Model\Category $category
* @return bool
*/
protected function isNeedToGenerateUrlPathForParent($category)
{
return $category->isObjectNew() || $category->getLevel() >= self::MINIMAL_CATEGORY_LEVEL_FOR_PROCESSING;
}
/**
* Get category url path
*
* @param \Magento\Catalog\Model\Category $category
* @param int $storeId
* @return string
*/
public function getUrlPathWithSuffix($category, $storeId = null)
{
if ($storeId === null) {
$storeId = $category->getStoreId();
}
return $this->getUrlPath($category) . $this->getCategoryUrlSuffix($storeId);
}
/**
* Retrieve category rewrite suffix for store
*
* @param int $storeId
* @return string
*/
protected function getCategoryUrlSuffix($storeId = null)
{
if ($storeId === null) {
$storeId = $this->storeManager->getStore()->getId();
}
if (!isset($this->categoryUrlSuffix[$storeId])) {
$this->categoryUrlSuffix[$storeId] = $this->scopeConfig->getValue(
self::XML_PATH_CATEGORY_URL_SUFFIX,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$storeId
);
}
return $this->categoryUrlSuffix[$storeId];
}
/**
* Get canonical category url
*
* @param \Magento\Catalog\Model\Category $category
* @return string
*/
public function getCanonicalUrlPath($category)
{
return 'catalog/category/view/id/' . $category->getId();
}
/**
* Generate category url key
*
* @param \Magento\Catalog\Model\Category $category
* @return string
*/
public function getUrlKey($category)
{
$urlKey = $category->getUrlKey();
return $category->formatUrlKey($urlKey === '' || $urlKey === null ? $category->getName() : $urlKey);
}
}