Skip to content

Commit 7fe8d97

Browse files
committed
Release new version 1.4.0
1 parent ede08a0 commit 7fe8d97

8 files changed

+708
-493
lines changed

CHANGELOG.md

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
44

55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [1.4.0] - 2019-02-11
8+
### Added
9+
- new option "--entity-type"
10+
- new option "--products-range"
11+
- new option "--product-id"
12+
- new option "--category-range"
13+
- new option "--category-id"
14+
15+
### Changed
16+
- revised and restructured code
17+
- modified logic of url rewrites regeneration
18+
- removed "--clean-url-key"
19+
720
## [1.3.1] - 2018-11-14
821
### Changed
922
- fixed issue of empty product URL keys

Console/Command/RegenerateUrlRewrites.php

+182-111
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,13 @@
1010

1111
namespace OlegKoval\RegenerateUrlRewrites\Console\Command;
1212

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-
1913
use Symfony\Component\Console\Command\Command;
2014
use Symfony\Component\Console\Input\InputInterface;
2115
use Symfony\Component\Console\Input\InputArgument;
2216
use Symfony\Component\Console\Input\InputOption;
2317
use Symfony\Component\Console\Output\OutputInterface;
2418

25-
class RegenerateUrlRewrites extends RegenerateUrlRewritesLayer
19+
class RegenerateUrlRewrites extends RegenerateUrlRewritesCategoryAbstract
2620
{
2721
/**
2822
* @var null|Symfony\Component\Console\Input\InputInterface
@@ -34,6 +28,83 @@ class RegenerateUrlRewrites extends RegenerateUrlRewritesLayer
3428
*/
3529
protected $_output = null;
3630

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+
37108
/**
38109
* Regenerate Url Rewrites
39110
* @param InputInterface $input
@@ -65,112 +136,72 @@ protected function execute(InputInterface $input, OutputInterface $output)
65136
$this->_appState->setAreaCode('adminhtml');
66137
}
67138

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-
73139
foreach ($this->_commandOptions['storesList'] as $storeId => $storeCode) {
140+
$this->_step = 0;
74141
$this->_output->writeln('');
75142
$this->_output->writeln("[Store ID: {$storeId}, Store View code: {$storeCode}]:");
76143

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+
}
99172
}
100173
}
101174

102175
$this->_output->writeln('');
103176
$this->_output->writeln('');
104177

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();
122180

123181
$this->_showSupportMe();
124182
$this->_output->writeln('Finished');
125183
}
126184

127185
/**
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
153188
*/
154189
public function getCommandOptions()
155190
{
156191
$options = $this->_input->getOptions();
157192
$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;
172193
$distinctOptionsUsed = 0;
173194

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+
174205
if (isset($options[self::INPUT_KEY_SAVE_REWRITES_HISTORY]) && $options[self::INPUT_KEY_SAVE_REWRITES_HISTORY] === true) {
175206
$this->_commandOptions['saveOldUrls'] = true;
176207
}
@@ -191,8 +222,22 @@ public function getCommandOptions()
191222
$this->_commandOptions['runCacheFlush'] = false;
192223
}
193224

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+
}
196241
}
197242

198243
if (isset($options[self::INPUT_KEY_CATEGORIES_RANGE])) {
@@ -203,14 +248,6 @@ public function getCommandOptions()
203248
$distinctOptionsUsed++;
204249
}
205250

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-
214251
if (isset($options[self::INPUT_KEY_CATEGORY_ID])) {
215252
$this->_commandOptions['categoryId'] = (int)$options[self::INPUT_KEY_CATEGORY_ID];
216253

@@ -221,14 +258,32 @@ public function getCommandOptions()
221258
}
222259
}
223260

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+
}
226274

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+
);
232287
}
233288

234289
if ($distinctOptionsUsed > 1) {
@@ -244,10 +299,7 @@ public function getCommandOptions()
244299
}
245300

246301
// 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);
251303

252304
// if store ID is not specified the re-generate for all stores
253305
if (is_null($storeId)) {
@@ -268,4 +320,23 @@ public function getCommandOptions()
268320
$this->_errors[] = __('ERROR: store ID should have a integer value.');
269321
}
270322
}
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+
}
271342
}

0 commit comments

Comments
 (0)