10
10
11
11
use Magento \Config \Model \Config \Reader \Source \Deployed \DocumentRoot ;
12
12
use Magento \Framework \App \ObjectManager ;
13
+ use Magento \Framework \DataObject ;
13
14
use Magento \Framework \Exception \LocalizedException ;
14
15
use Magento \Framework \UrlInterface ;
15
16
use Magento \Robots \Model \Config \Value ;
@@ -174,6 +175,13 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
174
175
*/
175
176
private $ configReader ;
176
177
178
+ /**
179
+ * Sitemap Item Factory
180
+ *
181
+ * @var SitemapItemInterfaceFactory|null
182
+ */
183
+ private $ sitemapItemFactory ;
184
+
177
185
/**
178
186
* Initialize dependencies.
179
187
*
@@ -195,6 +203,7 @@ class Sitemap extends \Magento\Framework\Model\AbstractModel implements \Magento
195
203
* @param DocumentRoot|null $documentRoot
196
204
* @param ItemResolverInterface|null $itemResolver
197
205
* @param SitemapConfigReaderInterface|null $configReader
206
+ * @param SitemapItemInterfaceFactory|null $sitemapItemFactory
198
207
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
199
208
*/
200
209
public function __construct (
@@ -215,11 +224,13 @@ public function __construct(
215
224
array $ data = [],
216
225
\Magento \Config \Model \Config \Reader \Source \Deployed \DocumentRoot $ documentRoot = null ,
217
226
ItemResolverInterface $ itemResolver = null ,
218
- SitemapConfigReaderInterface $ configReader = null
227
+ SitemapConfigReaderInterface $ configReader = null ,
228
+ SitemapItemInterfaceFactory $ sitemapItemFactory = null
219
229
) {
230
+ $ objectManager = ObjectManager::getInstance ();
220
231
$ this ->_escaper = $ escaper ;
221
232
$ this ->_sitemapData = $ sitemapData ;
222
- $ documentRoot = $ documentRoot ?: ObjectManager:: getInstance () ->get (DocumentRoot::class);
233
+ $ documentRoot = $ documentRoot ?: $ objectManager ->get (DocumentRoot::class);
223
234
$ this ->_directory = $ filesystem ->getDirectoryWrite ($ documentRoot ->getPath ());
224
235
$ this ->_categoryFactory = $ categoryFactory ;
225
236
$ this ->_productFactory = $ productFactory ;
@@ -228,10 +239,11 @@ public function __construct(
228
239
$ this ->_storeManager = $ storeManager ;
229
240
$ this ->_request = $ request ;
230
241
$ 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);
234
245
parent ::__construct ($ context , $ registry , $ resource , $ resourceCollection , $ data );
246
+
235
247
}
236
248
237
249
/**
@@ -259,14 +271,43 @@ protected function _getStream()
259
271
}
260
272
}
261
273
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
+
262
299
/**
263
300
* Initialize sitemap
264
301
*
265
302
* @return void
266
303
*/
267
304
protected function _initSitemapItems ()
268
305
{
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
+
270
311
271
312
$ this ->_tags = [
272
313
self ::TYPE_INDEX => [
@@ -740,6 +781,30 @@ private function _findNewLinesDelimiter($text)
740
781
return PHP_EOL ;
741
782
}
742
783
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
+
743
808
/**
744
809
* Get unique page cache identities
745
810
*
0 commit comments