33
33
use Magento \UrlRewrite \Model \UrlPersistInterface as UrlPersist ;
34
34
use Magento \UrlRewrite \Service \V1 \Data \UrlRewrite ;
35
35
use Magento \Store \Model \StoreManagerInterface ;
36
+ use Magento \Framework \App \Config \ScopeConfigInterface ;
36
37
37
38
abstract class RegenerateUrlRewritesAbstract extends Command
38
39
{
39
- const INPUT_KEY_STOREID = 'store-id ' ;
40
- const INPUT_KEY_REGENERATE_ENTITY_TYPE = 'entity-type ' ;
41
- const INPUT_KEY_SAVE_REWRITES_HISTORY = 'save-old-urls ' ;
42
- const INPUT_KEY_NO_REINDEX = 'no-reindex ' ;
43
- const INPUT_KEY_NO_PROGRESS = 'no-progress ' ;
44
- const INPUT_KEY_NO_CACHE_FLUSH = 'no-cache-flush ' ;
45
- const INPUT_KEY_NO_CACHE_CLEAN = 'no-cache-clean ' ;
46
- const INPUT_KEY_CATEGORIES_RANGE = 'categories-range ' ;
47
- const INPUT_KEY_PRODUCTS_RANGE = 'products-range ' ;
48
- const INPUT_KEY_CATEGORY_ID = 'category-id ' ;
49
- const INPUT_KEY_PRODUCT_ID = 'product-id ' ;
50
-
51
- const CONSOLE_LOG_MAX_DOTS_IN_LINE = 70 ;
52
- const INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT = 'product ' ;
53
- const INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY = 'category ' ;
40
+ const INPUT_KEY_STOREID = 'store-id ' ;
41
+ const INPUT_KEY_REGENERATE_ENTITY_TYPE = 'entity-type ' ;
42
+ const INPUT_KEY_SAVE_REWRITES_HISTORY = 'save-old-urls ' ;
43
+ const INPUT_KEY_NO_REINDEX = 'no-reindex ' ;
44
+ const INPUT_KEY_NO_PROGRESS = 'no-progress ' ;
45
+ const INPUT_KEY_NO_CACHE_FLUSH = 'no-cache-flush ' ;
46
+ const INPUT_KEY_NO_CACHE_CLEAN = 'no-cache-clean ' ;
47
+ const INPUT_KEY_CATEGORIES_RANGE = 'categories-range ' ;
48
+ const INPUT_KEY_PRODUCTS_RANGE = 'products-range ' ;
49
+ const INPUT_KEY_CATEGORY_ID = 'category-id ' ;
50
+ const INPUT_KEY_PRODUCT_ID = 'product-id ' ;
51
+ const INPUT_KEY_CHECK_USE_CATEGORIES_FOR_PRODUCT_URL = 'check-use-category-in-product-url ' ;
52
+
53
+ const CONSOLE_LOG_MAX_DOTS_IN_LINE = 70 ;
54
+ const INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT = 'product ' ;
55
+ const INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY = 'category ' ;
54
56
55
57
/**
56
58
* @var \Magento\Framework\App\ResourceConnection
@@ -147,6 +149,11 @@ abstract class RegenerateUrlRewritesAbstract extends Command
147
149
*/
148
150
protected $ _productAction ;
149
151
152
+ /**
153
+ * @var \Magento\Framework\App\Config\ScopeConfigInterface
154
+ */
155
+ protected $ _scopeConfig ;
156
+
150
157
/**
151
158
* @var array
152
159
*/
@@ -172,6 +179,11 @@ abstract class RegenerateUrlRewritesAbstract extends Command
172
179
*/
173
180
protected $ _errors = [];
174
181
182
+ /**
183
+ * @var array
184
+ */
185
+ protected $ _consoleMsg = [];
186
+
175
187
/**
176
188
* Constructor
177
189
* @param ResourceConnection $resource
@@ -189,6 +201,7 @@ abstract class RegenerateUrlRewritesAbstract extends Command
189
201
* @param DatabaseMapPool\Proxy $databaseMapPool
190
202
* @param UrlPersist\Proxy $urlPersist
191
203
* @param StoreManagerInterface $storeManager
204
+ * @param ScopeConfigInterface $scopeConfig
192
205
*/
193
206
public function __construct (
194
207
ResourceConnection $ resource ,
@@ -205,7 +218,8 @@ public function __construct(
205
218
UrlRewriteHandlerFactory \Proxy $ urlRewriteHandlerFactory ,
206
219
DatabaseMapPool \Proxy $ databaseMapPool ,
207
220
UrlPersist \Proxy $ urlPersist ,
208
- StoreManagerInterface $ storeManager
221
+ StoreManagerInterface $ storeManager ,
222
+ ScopeConfigInterface $ scopeConfig
209
223
) {
210
224
$ this ->_resource = $ resource ;
211
225
$ this ->_appState = $ appState ;
@@ -222,6 +236,7 @@ public function __construct(
222
236
$ this ->_databaseMapPool = $ databaseMapPool ;
223
237
$ this ->_urlPersist = $ urlPersist ;
224
238
$ this ->_storeManager = $ storeManager ;
239
+ $ this ->_scopeConfig = $ scopeConfig ;
225
240
226
241
$ this ->_dataUrlRewriteClassNames = [
227
242
DataCategoryUrlRewriteDatabaseMap::class,
@@ -242,6 +257,7 @@ public function __construct(
242
257
$ this ->_commandOptions ['productsFilter ' ] = [];
243
258
$ this ->_commandOptions ['categoryId ' ] = null ;
244
259
$ this ->_commandOptions ['productId ' ] = null ;
260
+ $ this ->_commandOptions ['checkUseCategoryInProductUrl ' ] = false ;
245
261
}
246
262
247
263
/**
@@ -455,17 +471,34 @@ protected function _displayProgressBar($size = 70)
455
471
}
456
472
457
473
/**
458
- * Display message in console
459
- * @param string $msg
474
+ * Collect console messages
475
+ * @param mixed $msg
460
476
* @return void
461
477
*/
462
- protected function _displayConsoleMsg ($ msg )
478
+ protected function _addConsoleMsg ($ msg )
463
479
{
464
480
if ($ msg instanceof \Magento \Framework \Phrase) {
465
481
$ msg = $ msg ->render ();
466
482
}
467
- $ this ->_output ->writeln ('' );
468
- $ this ->_output ->writeln ($ msg );
483
+
484
+ $ this ->_consoleMsg [] = (string )$ msg ;
485
+ }
486
+
487
+ /**
488
+ * Display all console messages
489
+ * @return void
490
+ */
491
+ protected function _displayConsoleMsg ()
492
+ {
493
+ if (count ($ this ->_consoleMsg ) > 0 ) {
494
+ $ this ->_output ->writeln ('[CONSOLE MESSAGES] ' );
495
+ foreach ($ this ->_consoleMsg as $ msg ) {
496
+ $ this ->_output ->writeln ($ msg );
497
+ }
498
+ $ this ->_output ->writeln ('[END OF CONSOLE MESSAGES] ' );
499
+ $ this ->_output ->writeln ('' );
500
+ $ this ->_output ->writeln ('' );
501
+ }
469
502
}
470
503
471
504
/**
@@ -489,7 +522,7 @@ protected function _doBunchReplaceUrlRewrites($urlRewrites = array(), $type = 'C
489
522
} catch (\Exception $ y ) {
490
523
// debugging
491
524
$ data = $ singleUrlRewrite ->toArray ();
492
- $ this ->_displayConsoleMsg ($ y ->getMessage () .' ' . $ type .' ID: ' . $ data ['entity_id ' ] .'. Request path: ' . $ data ['request_path ' ]);
525
+ $ this ->_addConsoleMsg ($ y ->getMessage () .' ' . $ type .' ID: ' . $ data ['entity_id ' ] .'. Request path: ' . $ data ['request_path ' ]);
493
526
}
494
527
}
495
528
}
@@ -535,6 +568,20 @@ protected function _runClearCache()
535
568
*/
536
569
protected function _clearRequestPath ($ requestPath )
537
570
{
538
- return ltrim ($ requestPath , '/ ' );
571
+ return str_replace (['// ' , './ ' ], ['/ ' , '/ ' ], ltrim (ltrim ($ requestPath , '/ ' ), '. ' ));
572
+ }
573
+
574
+ /**
575
+ * Get "Use Categories Path for Product URLs" config option value
576
+ * @param mixed $storeId
577
+ * @return boolean
578
+ */
579
+ protected function _getUseCategoriesPathForProductUrlsConfig ($ storeId = null )
580
+ {
581
+ return (bool ) $ this ->_scopeConfig ->getValue (
582
+ 'catalog/seo/product_use_categories ' ,
583
+ \Magento \Store \Model \ScopeInterface::SCOPE_STORES ,
584
+ $ storeId
585
+ );
539
586
}
540
587
}
0 commit comments