@@ -68,7 +68,7 @@ protected function configure()
68
68
new InputArgument (
69
69
'storeId ' ,
70
70
InputArgument::OPTIONAL ,
71
- 'Store ID: 5 '
71
+ '5 '
72
72
)
73
73
]);
74
74
}
@@ -83,30 +83,37 @@ protected function execute(InputInterface $input, OutputInterface $output)
83
83
{
84
84
set_time_limit (0 );
85
85
$ allStores = $ this ->getAllStoreIds ();
86
+ $ storesList = [];
86
87
$ output ->writeln ('Regenerating of Url rewrites: ' );
87
88
88
89
// get store Id (if was set)
89
90
$ storeId = $ input ->getArgument ('storeId ' );
90
91
92
+ // if store ID is not specified the re-generate for all stores
93
+ if (is_null ($ storeId )) {
94
+ $ storesList = $ allStores ;
95
+ }
91
96
// we will re-generate URL only in this specific store (if it exists)
92
- if (! empty ($ storeId ) && $ storeId > 0 ) {
97
+ elseif ( strlen ($ storeId ) && ctype_digit ( $ storeId) ) {
93
98
if (isset ($ allStores [$ storeId ])) {
94
99
$ storesList = array (
95
- $ storeId => $ allStores [$ storeId ]
96
- );
100
+ $ storeId => $ allStores [$ storeId ]
101
+ );
97
102
} else {
98
- $ output ->writeln ('ERROR: store with this ID not exists. ' );
99
- $ output ->writeln ('Finished ' );
103
+ $ this ->displayError ($ output , 'ERROR: store with this ID not exists. ' );
100
104
return ;
101
105
}
102
106
}
103
- // otherwise we re-generate for all stores
107
+ // disaply error if user set some incorrect value
104
108
else {
105
- $ storesList = $ allStores ;
109
+ $ this ->displayError ($ output , 'ERROR: store ID should have a integer value. ' , true );
110
+ return ;
106
111
}
107
112
108
113
// remove all current url rewrites
109
- $ this ->removeAllUrlRewrites ($ storesList );
114
+ if (count ($ storesList ) > 0 ) {
115
+ $ this ->removeAllUrlRewrites ($ storesList );
116
+ }
110
117
111
118
// set area code if needed
112
119
try {
@@ -117,6 +124,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
117
124
}
118
125
119
126
foreach ($ storesList as $ storeId => $ storeCode ) {
127
+ $ output ->writeln ('' );
120
128
$ output ->write ("[Store ID: {$ storeId }, Store View code: {$ storeCode }]: " );
121
129
$ step = 0 ;
122
130
@@ -146,11 +154,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
146
154
$ output ->writeln ($ e ->getMessage ());
147
155
}
148
156
}
149
- $ output ->writeln ('' );
150
157
}
151
158
152
159
$ output ->writeln ('' );
153
-
160
+ $ output -> writeln ( '' );
154
161
$ output ->writeln ('Reindexation... ' );
155
162
shell_exec ('php bin/magento indexer:reindex ' );
156
163
@@ -187,18 +194,34 @@ public function getAllStoreIds() {
187
194
$ result = [];
188
195
189
196
$ sql = $ this ->_resource ->getConnection ()->select ()
190
- ->from ($ this ->_resource ->getTableName ('store ' ), array ('store_id ' , 'code ' ))
191
- ->where ('`code` <> ? ' , 'admin ' )
192
- ->order ('store_id ' , 'ASC ' );
197
+ ->from ($ this ->_resource ->getTableName ('store ' ), array ('store_id ' , 'code ' ))
198
+ ->order ('store_id ' , 'ASC ' );
193
199
194
200
$ queryResult = $ this ->_resource ->getConnection ()->fetchAll ($ sql );
195
201
196
202
foreach ($ queryResult as $ row ) {
197
- if (isset ($ row ['store_id ' ]) && (int )$ row ['store_id ' ] > 0 ) {
198
- $ result [(int )$ row ['store_id ' ]] = $ row ['code ' ];
199
- }
203
+ $ result [(int )$ row ['store_id ' ]] = $ row ['code ' ];
200
204
}
201
205
202
206
return $ result ;
203
207
}
208
+
209
+ /**
210
+ * Display error message
211
+ * @param OutputInterface $output
212
+ * @param string $errorMsg
213
+ * @param boolean $displayHint
214
+ * @return void
215
+ */
216
+ private function displayError (&$ output , $ errorMsg , $ displayHint = false )
217
+ {
218
+ $ output ->writeln ('' );
219
+ $ output ->writeln ($ errorMsg );
220
+
221
+ if ($ displayHint ) {
222
+ $ output ->writeln ('Correct command is: bin/magento ok:urlrewrites:regenerate 19 ' );
223
+ }
224
+
225
+ $ output ->writeln ('Finished ' );
226
+ }
204
227
}
0 commit comments