10
10
11
11
namespace OlegKoval \RegenerateUrlRewrites \Console \Command ;
12
12
13
- if (class_exists ('\OlegKoval\RegenerateUrlRewrites\Console\Command\RegenerateUrlRewritesPro ' )) {
14
- abstract class RegenerateUrlRewritesLayer extends RegenerateUrlRewritesPro {}
15
- } else {
16
- abstract class RegenerateUrlRewritesLayer extends RegenerateUrlRewritesAbstract {}
17
- }
18
-
19
13
use Symfony \Component \Console \Command \Command ;
20
14
use Symfony \Component \Console \Input \InputInterface ;
21
15
use Symfony \Component \Console \Input \InputArgument ;
22
16
use Symfony \Component \Console \Input \InputOption ;
23
17
use Symfony \Component \Console \Output \OutputInterface ;
24
18
25
- class RegenerateUrlRewrites extends RegenerateUrlRewritesLayer
19
+ class RegenerateUrlRewrites extends RegenerateUrlRewritesCategoryAbstract
26
20
{
27
21
/**
28
22
* @var null|Symfony\Component\Console\Input\InputInterface
@@ -34,6 +28,83 @@ class RegenerateUrlRewrites extends RegenerateUrlRewritesLayer
34
28
*/
35
29
protected $ _output = null ;
36
30
31
+ /**
32
+ * {@inheritdoc}
33
+ */
34
+ protected function configure ()
35
+ {
36
+ $ this ->setName ('ok:urlrewrites:regenerate ' )
37
+ ->setDescription ('Regenerate Url rewrites of products and categories ' )
38
+ ->setDefinition ([
39
+ new InputOption (
40
+ self ::INPUT_KEY_STOREID ,
41
+ null ,
42
+ InputArgument::OPTIONAL ,
43
+ 'Specific store id '
44
+ ),
45
+ new InputOption (
46
+ self ::INPUT_KEY_REGENERATE_ENTITY_TYPE ,
47
+ null ,
48
+ InputArgument::OPTIONAL ,
49
+ 'Entity type which URLs regenerate: product or category. Default is "product". '
50
+ ),
51
+ new InputOption (
52
+ self ::INPUT_KEY_SAVE_REWRITES_HISTORY ,
53
+ null ,
54
+ InputOption::VALUE_NONE ,
55
+ 'Save current URL Rewrites '
56
+ ),
57
+ new InputOption (
58
+ self ::INPUT_KEY_NO_REINDEX ,
59
+ null ,
60
+ InputOption::VALUE_NONE ,
61
+ 'Do not run reindex when URL rewrites are generated. '
62
+ ),
63
+ new InputOption (
64
+ self ::INPUT_KEY_NO_PROGRESS ,
65
+ null ,
66
+ InputOption::VALUE_NONE ,
67
+ 'Do not show progress indicator. '
68
+ ),
69
+ new InputOption (
70
+ self ::INPUT_KEY_NO_CACHE_FLUSH ,
71
+ null ,
72
+ InputOption::VALUE_NONE ,
73
+ 'Do not run cache:flush when URL rewrites are generated. '
74
+ ),
75
+ new InputOption (
76
+ self ::INPUT_KEY_NO_CACHE_CLEAN ,
77
+ null ,
78
+ InputOption::VALUE_NONE ,
79
+ 'Do not run cache:clean when URL rewrites are generated. '
80
+ ),
81
+ new InputOption (
82
+ self ::INPUT_KEY_CATEGORIES_RANGE ,
83
+ null ,
84
+ InputArgument::OPTIONAL ,
85
+ 'Categories ID range, e.g.: 15-40 (Pro version only) '
86
+ ),
87
+ new InputOption (
88
+ self ::INPUT_KEY_PRODUCTS_RANGE ,
89
+ null ,
90
+ InputArgument::OPTIONAL ,
91
+ 'Products ID range, e.g.: 101-152 (Pro version only) '
92
+ ),
93
+ new InputOption (
94
+ self ::INPUT_KEY_CATEGORY_ID ,
95
+ null ,
96
+ InputArgument::OPTIONAL ,
97
+ 'Specific category ID, e.g.: 123 (Pro version only) '
98
+ ),
99
+ new InputOption (
100
+ self ::INPUT_KEY_PRODUCT_ID ,
101
+ null ,
102
+ InputArgument::OPTIONAL ,
103
+ 'Specific product ID, e.g.: 107 (Pro version only) '
104
+ )
105
+ ]);
106
+ }
107
+
37
108
/**
38
109
* Regenerate Url Rewrites
39
110
* @param InputInterface $input
@@ -65,112 +136,72 @@ protected function execute(InputInterface $input, OutputInterface $output)
65
136
$ this ->_appState ->setAreaCode ('adminhtml ' );
66
137
}
67
138
68
- // remove current url rewrites
69
- if (count ($ this ->_commandOptions ['storesList ' ]) > 0 && !$ this ->_commandOptions ['saveOldUrls ' ]) {
70
- $ this ->_removeAllUrlRewrites ($ this ->_commandOptions ['storesList ' ], $ this ->_commandOptions ['productsFilter ' ]);
71
- }
72
-
73
139
foreach ($ this ->_commandOptions ['storesList ' ] as $ storeId => $ storeCode ) {
140
+ $ this ->_step = 0 ;
74
141
$ this ->_output ->writeln ('' );
75
142
$ this ->_output ->writeln ("[Store ID: {$ storeId }, Store View code: {$ storeCode }]: " );
76
143
77
- if (count ($ this ->_commandOptions ['categoriesFilter ' ]) > 0 ) {
78
- $ this ->regenerateCategoriesRangeUrlRewrites (
79
- $ this ->_commandOptions ['categoriesFilter ' ],
80
- $ storeId
81
- );
82
- } elseif (count ($ this ->_commandOptions ['productsFilter ' ]) > 0 ) {
83
- $ this ->regenerateProductsRangeUrlRewrites (
84
- $ this ->_commandOptions ['productsFilter ' ],
85
- $ storeId
86
- );
87
- } elseif (!empty ($ this ->_commandOptions ['categoryId ' ])) {
88
- $ this ->regenerateSpecificCategoryUrlRewrites (
89
- $ this ->_commandOptions ['categoryId ' ],
90
- $ storeId
91
- );
92
- } elseif (!empty ($ this ->_commandOptions ['productId ' ])) {
93
- $ this ->regenerateSpecificProductUrlRewrites (
94
- $ this ->_commandOptions ['productId ' ],
95
- $ storeId
96
- );
97
- } else {
98
- $ this ->regenerateAllUrlRewrites ($ storeId );
144
+ if ($ this ->_commandOptions ['entityType ' ] == self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT ) {
145
+ if (count ($ this ->_commandOptions ['productsFilter ' ]) > 0 ) {
146
+ $ this ->regenerateProductsRangeUrlRewrites (
147
+ $ this ->_commandOptions ['productsFilter ' ],
148
+ $ storeId
149
+ );
150
+ } elseif (!empty ($ this ->_commandOptions ['productId ' ])) {
151
+ $ this ->regenerateSpecificProductUrlRewrites (
152
+ $ this ->_commandOptions ['productId ' ],
153
+ $ storeId
154
+ );
155
+ } else {
156
+ $ this ->regenerateAllProductsUrlRewrites ($ storeId );
157
+ }
158
+ } elseif ($ this ->_commandOptions ['entityType ' ] == self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY ) {
159
+ if (count ($ this ->_commandOptions ['categoriesFilter ' ]) > 0 ) {
160
+ $ this ->regenerateCategoriesRangeUrlRewrites (
161
+ $ this ->_commandOptions ['categoriesFilter ' ],
162
+ $ storeId
163
+ );
164
+ } elseif (!empty ($ this ->_commandOptions ['categoryId ' ])) {
165
+ $ this ->regenerateSpecificCategoryUrlRewrites (
166
+ $ this ->_commandOptions ['categoryId ' ],
167
+ $ storeId
168
+ );
169
+ } else {
170
+ $ this ->regenerateAllCategoriesUrlRewrites ($ storeId );
171
+ }
99
172
}
100
173
}
101
174
102
175
$ this ->_output ->writeln ('' );
103
176
$ this ->_output ->writeln ('' );
104
177
105
- if ($ this ->_commandOptions ['runReindex ' ] == true ) {
106
- $ this ->_output ->write ('Reindexation... ' );
107
- shell_exec ('php bin/magento indexer:reindex ' );
108
- $ this ->_output ->writeln (' Done ' );
109
- }
110
-
111
- if ($ this ->_commandOptions ['runCacheClean ' ] || $ this ->_commandOptions ['runCacheFlush ' ]) {
112
- $ this ->_output ->write ('Cache refreshing... ' );
113
- if ($ this ->_commandOptions ['runCacheClean ' ]) {
114
- shell_exec ('php bin/magento cache:clean ' );
115
- }
116
- if ($ this ->_commandOptions ['runCacheFlush ' ]) {
117
- shell_exec ('php bin/magento cache:flush ' );
118
- }
119
- $ this ->_output ->writeln (' Done ' );
120
- $ this ->_output ->writeln ('If you use some external cache mechanisms (e.g.: Redis, Varnish, etc.) - please, refresh this external cache. ' );
121
- }
178
+ $ this ->_runReindexation ();
179
+ $ this ->_runClearCache ();
122
180
123
181
$ this ->_showSupportMe ();
124
182
$ this ->_output ->writeln ('Finished ' );
125
183
}
126
184
127
185
/**
128
- * @see parent::regenerateAllUrlRewrites()
129
- */
130
- public function regenerateAllUrlRewrites ($ storeId = 0 )
131
- {
132
- $ this ->_step = 0 ;
133
-
134
- // get categories collection
135
- $ categories = $ this ->_getCategoriesCollection ($ storeId );
136
-
137
- $ pageCount = $ categories ->getLastPageNumber ();
138
- $ currentPage = 1 ;
139
- while ($ currentPage <= $ pageCount ) {
140
- $ categories ->setCurPage ($ currentPage );
141
-
142
- foreach ($ categories as $ category ) {
143
- $ this ->_categoryProcess ($ category , $ storeId );
144
- }
145
-
146
- $ categories ->clear ();
147
- $ currentPage ++;
148
- }
149
- }
150
-
151
- /**
152
- * @see parent::getCommandOptions()
186
+ * Get command options
187
+ * @return void
153
188
*/
154
189
public function getCommandOptions ()
155
190
{
156
191
$ options = $ this ->_input ->getOptions ();
157
192
$ allStores = $ this ->_getAllStoreIds ();
158
-
159
- // default values
160
- $ this ->_commandOptions ['saveOldUrls ' ] = false ;
161
- $ this ->_commandOptions ['runReindex ' ] = true ;
162
- $ this ->_commandOptions ['protectOutOfMemory ' ] = false ;
163
- $ this ->_commandOptions ['storesList ' ] = [];
164
- $ this ->_commandOptions ['showProgress ' ] = true ;
165
- $ this ->_commandOptions ['runCacheClean ' ] = true ;
166
- $ this ->_commandOptions ['runCacheFlush ' ] = true ;
167
- $ this ->_commandOptions ['cleanUrlKey ' ] = true ;
168
- $ this ->_commandOptions ['categoriesFilter ' ] = [];
169
- $ this ->_commandOptions ['productsFilter ' ] = [];
170
- $ this ->_commandOptions ['categoryId ' ] = null ;
171
- $ this ->_commandOptions ['productId ' ] = null ;
172
193
$ distinctOptionsUsed = 0 ;
173
194
195
+ if (
196
+ isset ($ options [self ::INPUT_KEY_REGENERATE_ENTITY_TYPE ])
197
+ && in_array (
198
+ $ options [self ::INPUT_KEY_REGENERATE_ENTITY_TYPE ],
199
+ array (self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT , self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY )
200
+ )
201
+ ) {
202
+ $ this ->_commandOptions ['entityType ' ] = $ options [self ::INPUT_KEY_REGENERATE_ENTITY_TYPE ];
203
+ }
204
+
174
205
if (isset ($ options [self ::INPUT_KEY_SAVE_REWRITES_HISTORY ]) && $ options [self ::INPUT_KEY_SAVE_REWRITES_HISTORY ] === true ) {
175
206
$ this ->_commandOptions ['saveOldUrls ' ] = true ;
176
207
}
@@ -191,8 +222,22 @@ public function getCommandOptions()
191
222
$ this ->_commandOptions ['runCacheFlush ' ] = false ;
192
223
}
193
224
194
- if (isset ($ options [self ::INPUT_KEY_NO_CLEAN_URL_KEY ]) && $ options [self ::INPUT_KEY_NO_CLEAN_URL_KEY ] === true ) {
195
- $ this ->_commandOptions ['cleanUrlKey ' ] = false ;
225
+ if (isset ($ options [self ::INPUT_KEY_PRODUCTS_RANGE ])) {
226
+ $ this ->_commandOptions ['productsFilter ' ] = $ this ->_generateIdsRangeArray (
227
+ $ options [self ::INPUT_KEY_PRODUCTS_RANGE ],
228
+ 'product '
229
+ );
230
+ $ distinctOptionsUsed ++;
231
+ }
232
+
233
+ if (isset ($ options [self ::INPUT_KEY_PRODUCT_ID ])) {
234
+ $ this ->_commandOptions ['productId ' ] = (int )$ options [self ::INPUT_KEY_PRODUCT_ID ];
235
+
236
+ if ($ this ->_commandOptions ['productId ' ] == 0 ) {
237
+ $ this ->_errors [] = __ ('ERROR: product ID should be greater than 0. ' );
238
+ } else {
239
+ $ distinctOptionsUsed ++;
240
+ }
196
241
}
197
242
198
243
if (isset ($ options [self ::INPUT_KEY_CATEGORIES_RANGE ])) {
@@ -203,14 +248,6 @@ public function getCommandOptions()
203
248
$ distinctOptionsUsed ++;
204
249
}
205
250
206
- if (isset ($ options [self ::INPUT_KEY_PRODUCTS_RANGE ])) {
207
- $ this ->_commandOptions ['productsFilter ' ] = $ this ->_generateIdsRangeArray (
208
- $ options [self ::INPUT_KEY_PRODUCTS_RANGE ],
209
- 'product '
210
- );
211
- $ distinctOptionsUsed ++;
212
- }
213
-
214
251
if (isset ($ options [self ::INPUT_KEY_CATEGORY_ID ])) {
215
252
$ this ->_commandOptions ['categoryId ' ] = (int )$ options [self ::INPUT_KEY_CATEGORY_ID ];
216
253
@@ -221,14 +258,32 @@ public function getCommandOptions()
221
258
}
222
259
}
223
260
224
- if (isset ($ options [self ::INPUT_KEY_PRODUCT_ID ])) {
225
- $ this ->_commandOptions ['productId ' ] = (int )$ options [self ::INPUT_KEY_PRODUCT_ID ];
261
+ if (
262
+ $ this ->_commandOptions ['entityType ' ] == self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT
263
+ && (
264
+ count ($ this ->_commandOptions ['categoriesFilter ' ]) > 0
265
+ || (int ) $ this ->_commandOptions ['categoryId ' ] > 0
266
+ )
267
+ ) {
268
+ $ this ->_errors [] = $ this ->_getLogicalConflictError (
269
+ self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_PRODUCT ,
270
+ self ::INPUT_KEY_CATEGORIES_RANGE ,
271
+ self ::INPUT_KEY_CATEGORY_ID
272
+ );
273
+ }
226
274
227
- if ($ this ->_commandOptions ['productId ' ] == 0 ) {
228
- $ this ->_errors [] = __ ('ERROR: product ID should be greater than 0. ' );
229
- } else {
230
- $ distinctOptionsUsed ++;
231
- }
275
+ if (
276
+ $ this ->_commandOptions ['entityType ' ] == self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY
277
+ && (
278
+ count ($ this ->_commandOptions ['productsFilter ' ]) > 0
279
+ || (int ) $ this ->_commandOptions ['productId ' ] > 0
280
+ )
281
+ ) {
282
+ $ this ->_errors [] = $ this ->_getLogicalConflictError (
283
+ self ::INPUT_KEY_REGENERATE_ENTITY_TYPE_CATEGORY ,
284
+ self ::INPUT_KEY_PRODUCTS_RANGE ,
285
+ self ::INPUT_KEY_PRODUCT_ID
286
+ );
232
287
}
233
288
234
289
if ($ distinctOptionsUsed > 1 ) {
@@ -244,10 +299,7 @@ public function getCommandOptions()
244
299
}
245
300
246
301
// get store Id (if was set)
247
- $ storeId = $ this ->_input ->getArgument (self ::INPUT_KEY_STOREID );
248
- if (is_null ($ storeId )) {
249
- $ storeId = $ this ->_input ->getOption (self ::INPUT_KEY_STOREID );
250
- }
302
+ $ storeId = $ this ->_input ->getOption (self ::INPUT_KEY_STOREID );
251
303
252
304
// if store ID is not specified the re-generate for all stores
253
305
if (is_null ($ storeId )) {
@@ -268,4 +320,23 @@ public function getCommandOptions()
268
320
$ this ->_errors [] = __ ('ERROR: store ID should have a integer value. ' );
269
321
}
270
322
}
323
+
324
+ /**
325
+ * Generate logical conflict error
326
+ * @param string $option1
327
+ * @param string $option2
328
+ * @param string $option3
329
+ * @return string
330
+ */
331
+ private function _getLogicalConflictError ($ option1 , $ option2 , $ option3 )
332
+ {
333
+ return __ (
334
+ "ERROR: you can not use this options together (logical conflict): \n'--%o1' with '--%o2'/'--%o3' " ,
335
+ [
336
+ 'o1 ' => $ option1 ,
337
+ 'o2 ' => $ option2 ,
338
+ 'o4 ' => $ option3
339
+ ]
340
+ );
341
+ }
271
342
}
0 commit comments