7
7
8
8
namespace Magento \UrlRewrite \Model ;
9
9
10
- use Magento \Catalog \Model \Category ;
11
- use Magento \Catalog \Model \Product ;
12
- use Magento \Cms \Model \Page ;
13
10
use Magento \Framework \App \ObjectManager ;
14
11
use Magento \Framework \Data \Collection \AbstractDb ;
15
12
use Magento \Framework \EntityManager \EventManager ;
@@ -61,6 +58,11 @@ class UrlRewrite extends AbstractModel
61
58
*/
62
59
private $ entityToCacheTagMap ;
63
60
61
+ /**
62
+ * @var UrlFinderInterface
63
+ */
64
+ private $ urlFinder ;
65
+
64
66
/**
65
67
* UrlRewrite constructor.
66
68
*
@@ -72,6 +74,7 @@ class UrlRewrite extends AbstractModel
72
74
* @param Json|null $serializer
73
75
* @param CacheContext|null $cacheContext
74
76
* @param EventManager|null $eventManager
77
+ * @param UrlFinderInterface|null $urlFinder
75
78
* @param array $entityToCacheTagMap
76
79
*/
77
80
public function __construct (
@@ -83,12 +86,14 @@ public function __construct(
83
86
Json $ serializer = null ,
84
87
CacheContext $ cacheContext = null ,
85
88
EventManager $ eventManager = null ,
89
+ UrlFinderInterface $ urlFinder = null ,
86
90
array $ entityToCacheTagMap = []
87
91
)
88
92
{
89
93
$ this ->serializer = $ serializer ?: ObjectManager::getInstance ()->get (Json::class);
90
94
$ this ->cacheContext = $ cacheContext ?: ObjectManager::getInstance ()->get (CacheContext::class);
91
95
$ this ->eventManager = $ eventManager ?: ObjectManager::getInstance ()->get (EventManager::class);
96
+ $ this ->urlFinder = $ urlFinder ?: ObjectManager::getInstance ()->get (UrlFinderInterface::class);
92
97
$ this ->entityToCacheTagMap = $ entityToCacheTagMap ;
93
98
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
94
99
}
@@ -131,30 +136,91 @@ public function setMetadata($metadata)
131
136
}
132
137
133
138
/**
134
- * Clean cache for the entity which was affected by updating UrlRewrite
139
+ * Gets final target UrlRewrite for custom rewrite record
135
140
*
136
- * @param $entityType
137
- * @param $entityId
141
+ * @param string $path
142
+ * @param int $storeId
143
+ * @return UrlRewrite|null
144
+ */
145
+ private function getFinalTargetUrlRewrite (string $ path , int $ storeId ) {
146
+ $ urlRewriteTarget = $ this ->urlFinder ->findOneByData (
147
+ [
148
+ 'request_path ' => $ path ,
149
+ 'store_id ' => $ storeId
150
+ ]
151
+ );
152
+
153
+ while ($ urlRewriteTarget && $ urlRewriteTarget ->getRedirectType () > 0 ) {
154
+ $ urlRewriteTarget = $ this ->urlFinder ->findOneByData (
155
+ [
156
+ 'request_path ' => $ urlRewriteTarget ->getTargetPath (),
157
+ 'store_id ' => $ urlRewriteTarget ->getStoreId ()
158
+ ]
159
+ );
160
+ }
161
+
162
+ return $ urlRewriteTarget ;
163
+ }
164
+
165
+ /**
166
+ * Clean the cache for entities affected by current rewrite
138
167
*/
139
- private function cleanCacheForEntity ($ entityType , $ entityId )
168
+ private function cleanEntitiesCache () {
169
+ if ($ this ->getEntityType () === Rewrite::ENTITY_TYPE_CUSTOM ) {
170
+ $ urlRewrite = $ this ->getFinalTargetUrlRewrite (
171
+ $ this ->getTargetPath (),
172
+ (int )$ this ->getStoreId ()
173
+ );
174
+
175
+ if ($ urlRewrite ) {
176
+ $ this ->cleanCacheForEntity ($ urlRewrite ->getEntityType (), (int ) $ urlRewrite ->getEntityId ());
177
+ }
178
+
179
+ if ($ this ->getOrigData () && $ this ->getOrigData ('target_path ' ) !== $ this ->getTargetPath ()) {
180
+ $ origUrlRewrite = $ this ->getFinalTargetUrlRewrite (
181
+ $ this ->getOrigData ('target_path ' ),
182
+ (int )$ this ->getOrigData ('store_id ' )
183
+ );
184
+
185
+ if ($ origUrlRewrite ) {
186
+ $ this ->cleanCacheForEntity ($ origUrlRewrite ->getEntityType (), (int ) $ origUrlRewrite ->getEntityId ());
187
+ }
188
+ }
189
+ } else {
190
+ $ this ->cleanCacheForEntity ($ this ->getEntityType (), (int ) $ this ->getEntityId ());
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Clean cache for specified entity type by id
196
+ *
197
+ * @param string $entityType
198
+ * @param int $entityId
199
+ */
200
+ private function cleanCacheForEntity (string $ entityType , int $ entityId )
140
201
{
141
- if ($ entityType !== Rewrite:: ENTITY_TYPE_CUSTOM && array_key_exists ($ entityType , $ this ->entityToCacheTagMap )) {
202
+ if (array_key_exists ($ entityType , $ this ->entityToCacheTagMap )) {
142
203
$ cacheKey = $ this ->entityToCacheTagMap [$ entityType ];
143
-
144
204
$ this ->cacheContext ->registerEntities ($ cacheKey , [$ entityId ]);
145
205
$ this ->eventManager ->dispatch ('clean_cache_by_tags ' , ['object ' => $ this ->cacheContext ]);
146
206
}
147
207
}
148
208
209
+ /**
210
+ * @inheritdoc
211
+ */
149
212
public function afterDelete ()
150
213
{
151
- $ this ->cleanCacheForEntity ( $ this -> getEntityType (), $ this -> getEntityId () );
214
+ $ this ->cleanEntitiesCache ( );
152
215
return parent ::afterDelete ();
153
216
}
154
217
218
+ /**
219
+ * @inheritdoc
220
+ */
155
221
public function afterSave ()
156
222
{
157
- $ this ->cleanCacheForEntity ( $ this -> getEntityType (), $ this -> getEntityId () );
223
+ $ this ->cleanEntitiesCache ( );
158
224
return parent ::afterSave ();
159
225
}
160
226
}
0 commit comments