8
8
namespace Magento \MediaStorage \Console \Command ;
9
9
10
10
use Magento \Framework \App \Area ;
11
- use Magento \Framework \App \ObjectManager ;
12
11
use Magento \Framework \App \State ;
13
- use Magento \Framework \ObjectManagerInterface ;
12
+ use Magento \Framework \Console \ Cli ;
14
13
use Magento \MediaStorage \Service \ImageResize ;
14
+ use Magento \MediaStorage \Service \ImageResizeScheduler ;
15
15
use Symfony \Component \Console \Helper \ProgressBar ;
16
16
use Symfony \Component \Console \Helper \ProgressBarFactory ;
17
17
use Symfony \Component \Console \Input \InputInterface ;
18
18
use Symfony \Component \Console \Output \OutputInterface ;
19
+ use Symfony \Component \Console \Input \InputOption ;
20
+ use Symfony \Component \Console \Command \Command ;
21
+ use Magento \Catalog \Model \ResourceModel \Product \Image as ProductImage ;
19
22
20
23
/**
21
24
* Resizes product images according to theme view definitions.
22
25
*
23
26
* @package Magento\MediaStorage\Console\Command
24
27
*/
25
- class ImagesResizeCommand extends \ Symfony \ Component \ Console \ Command \ Command
28
+ class ImagesResizeCommand extends Command
26
29
{
30
+ /**
31
+ * Asynchronous image resize mode
32
+ */
33
+ const ASYNC_RESIZE = 'async ' ;
34
+
35
+ /**
36
+ * @var ImageResizeScheduler
37
+ */
38
+ private $ imageResizeScheduler ;
39
+
27
40
/**
28
41
* @var ImageResize
29
42
*/
30
- private $ resize ;
43
+ private $ imageResize ;
31
44
32
45
/**
33
46
* @var State
@@ -39,24 +52,32 @@ class ImagesResizeCommand extends \Symfony\Component\Console\Command\Command
39
52
*/
40
53
private $ progressBarFactory ;
41
54
55
+ /**
56
+ * @var ProductImage
57
+ */
58
+ private $ productImage ;
59
+
42
60
/**
43
61
* @param State $appState
44
- * @param ImageResize $resize
45
- * @param ObjectManagerInterface $objectManager
62
+ * @param ImageResize $imageResize
63
+ * @param ImageResizeScheduler $imageResizeScheduler
46
64
* @param ProgressBarFactory $progressBarFactory
65
+ * @param ProductImage $productImage
47
66
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
48
67
*/
49
68
public function __construct (
50
69
State $ appState ,
51
- ImageResize $ resize ,
52
- ObjectManagerInterface $ objectManager ,
53
- ProgressBarFactory $ progressBarFactory = null
70
+ ImageResize $ imageResize ,
71
+ ImageResizeScheduler $ imageResizeScheduler ,
72
+ ProgressBarFactory $ progressBarFactory ,
73
+ ProductImage $ productImage
54
74
) {
55
75
parent ::__construct ();
56
- $ this ->resize = $ resize ;
57
76
$ this ->appState = $ appState ;
58
- $ this ->progressBarFactory = $ progressBarFactory
59
- ?: ObjectManager::getInstance ()->get (ProgressBarFactory::class);
77
+ $ this ->imageResize = $ imageResize ;
78
+ $ this ->imageResizeScheduler = $ imageResizeScheduler ;
79
+ $ this ->progressBarFactory = $ progressBarFactory ;
80
+ $ this ->productImage = $ productImage ;
60
81
}
61
82
62
83
/**
@@ -65,7 +86,25 @@ public function __construct(
65
86
protected function configure ()
66
87
{
67
88
$ this ->setName ('catalog:images:resize ' )
68
- ->setDescription ('Creates resized product images ' );
89
+ ->setDescription ('Creates resized product images ' )
90
+ ->setDefinition ($ this ->getOptionsList ());
91
+ }
92
+
93
+ /**
94
+ * Image resize command options list
95
+ *
96
+ * @return array
97
+ */
98
+ private function getOptionsList () : array
99
+ {
100
+ return [
101
+ new InputOption (
102
+ self ::ASYNC_RESIZE ,
103
+ 'a ' ,
104
+ InputOption::VALUE_NONE ,
105
+ 'Resize image in asynchronous mode '
106
+ ),
107
+ ];
69
108
}
70
109
71
110
/**
@@ -74,11 +113,25 @@ protected function configure()
74
113
* @param OutputInterface $output
75
114
*/
76
115
protected function execute (InputInterface $ input , OutputInterface $ output )
116
+ {
117
+ $ result = $ input ->getOption (self ::ASYNC_RESIZE ) ?
118
+ $ this ->executeAsync ($ output ) : $ this ->executeSync ($ output );
119
+
120
+ return $ result ;
121
+ }
122
+
123
+ /**
124
+ * Run resize in synchronous mode
125
+ *
126
+ * @param OutputInterface $output
127
+ * @return int
128
+ */
129
+ private function executeSync (OutputInterface $ output ): int
77
130
{
78
131
try {
79
132
$ errors = [];
80
133
$ this ->appState ->setAreaCode (Area::AREA_GLOBAL );
81
- $ generator = $ this ->resize ->resizeFromThemes ();
134
+ $ generator = $ this ->imageResize ->resizeFromThemes ();
82
135
83
136
/** @var ProgressBar $progress */
84
137
$ progress = $ this ->progressBarFactory ->create (
@@ -111,7 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
111
164
} catch (\Exception $ e ) {
112
165
$ output ->writeln ("<error> {$ e ->getMessage ()}</error> " );
113
166
// we must have an exit code higher than zero to indicate something was wrong
114
- return \ Magento \ Framework \ Console \ Cli::RETURN_FAILURE ;
167
+ return Cli::RETURN_FAILURE ;
115
168
}
116
169
117
170
$ output ->write (PHP_EOL );
@@ -124,6 +177,62 @@ protected function execute(InputInterface $input, OutputInterface $output)
124
177
$ output ->writeln ("<info>Product images resized successfully</info> " );
125
178
}
126
179
127
- return \Magento \Framework \Console \Cli::RETURN_SUCCESS ;
180
+ return Cli::RETURN_SUCCESS ;
181
+ }
182
+
183
+ /**
184
+ * Schedule asynchronous image resizing
185
+ *
186
+ * @param OutputInterface $output
187
+ * @return int
188
+ */
189
+ private function executeAsync (OutputInterface $ output ): int
190
+ {
191
+ try {
192
+ $ errors = [];
193
+ $ this ->appState ->setAreaCode (Area::AREA_GLOBAL );
194
+
195
+ /** @var ProgressBar $progress */
196
+ $ progress = $ this ->progressBarFactory ->create (
197
+ [
198
+ 'output ' => $ output ,
199
+ 'max ' => $ this ->productImage ->getCountUsedProductImages ()
200
+ ]
201
+ );
202
+ $ progress ->setFormat (
203
+ "%current%/%max% [%bar%] %percent:3s%% %elapsed% %memory:6s% \t| <info>%message%</info> "
204
+ );
205
+
206
+ if ($ output ->getVerbosity () !== OutputInterface::VERBOSITY_NORMAL ) {
207
+ $ progress ->setOverwrite (false );
208
+ }
209
+
210
+ $ productImages = $ this ->productImage ->getUsedProductImages ();
211
+ foreach ($ productImages as $ image ) {
212
+ $ result = $ this ->imageResizeScheduler ->schedule ($ image ['filepath ' ]);
213
+
214
+ if (!$ result ) {
215
+ $ errors [$ image ['filepath ' ]] = 'Error image scheduling: ' . $ image ['filepath ' ];
216
+ }
217
+ $ progress ->setMessage ($ image ['filepath ' ]);
218
+ $ progress ->advance ();
219
+ }
220
+ } catch (\Exception $ e ) {
221
+ $ output ->writeln ("<error> {$ e ->getMessage ()}</error> " );
222
+ // we must have an exit code higher than zero to indicate something was wrong
223
+ return Cli::RETURN_FAILURE ;
224
+ }
225
+
226
+ $ output ->write (PHP_EOL );
227
+ if (count ($ errors )) {
228
+ $ output ->writeln ("<info>Product images resized with errors:</info> " );
229
+ foreach ($ errors as $ error ) {
230
+ $ output ->writeln ("<error> {$ error }</error> " );
231
+ }
232
+ } else {
233
+ $ output ->writeln ("<info>Product images scheduled successfully</info> " );
234
+ }
235
+
236
+ return Cli::RETURN_SUCCESS ;
128
237
}
129
238
}
0 commit comments