ImagesResizeCommand.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\MediaStorage\Console\Command;
  8. use Magento\Framework\App\Area;
  9. use Magento\Framework\App\State;
  10. use Magento\MediaStorage\Service\ImageResize;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Symfony\Component\Console\Helper\ProgressBar;
  14. use Magento\Framework\ObjectManagerInterface;
  15. class ImagesResizeCommand extends \Symfony\Component\Console\Command\Command
  16. {
  17. /**
  18. * @var ImageResize
  19. */
  20. private $resize;
  21. /**
  22. * @var State
  23. */
  24. private $appState;
  25. /**
  26. * @var ObjectManagerInterface
  27. */
  28. private $objectManager;
  29. /**
  30. * @param State $appState
  31. * @param ImageResize $resize
  32. * @param ObjectManagerInterface $objectManager
  33. */
  34. public function __construct(
  35. State $appState,
  36. ImageResize $resize,
  37. ObjectManagerInterface $objectManager
  38. ) {
  39. parent::__construct();
  40. $this->resize = $resize;
  41. $this->appState = $appState;
  42. $this->objectManager = $objectManager;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function configure()
  48. {
  49. $this->setName('catalog:images:resize')
  50. ->setDescription('Creates resized product images');
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function execute(InputInterface $input, OutputInterface $output)
  56. {
  57. try {
  58. $this->appState->setAreaCode(Area::AREA_GLOBAL);
  59. $generator = $this->resize->resizeFromThemes();
  60. /** @var ProgressBar $progress */
  61. $progress = $this->objectManager->create(ProgressBar::class, [
  62. 'output' => $output,
  63. 'max' => $generator->current()
  64. ]);
  65. $progress->setFormat(
  66. "%current%/%max% [%bar%] %percent:3s%% %elapsed% %memory:6s% \t| <info>%message%</info>"
  67. );
  68. if ($output->getVerbosity() !== OutputInterface::VERBOSITY_NORMAL) {
  69. $progress->setOverwrite(false);
  70. }
  71. for (; $generator->valid(); $generator->next()) {
  72. $progress->setMessage($generator->key());
  73. $progress->advance();
  74. }
  75. } catch (\Exception $e) {
  76. $output->writeln("<error>{$e->getMessage()}</error>");
  77. // we must have an exit code higher than zero to indicate something was wrong
  78. return \Magento\Framework\Console\Cli::RETURN_FAILURE;
  79. }
  80. $output->write(PHP_EOL);
  81. $output->writeln("<info>Product images resized successfully</info>");
  82. }
  83. }