vendor/shopware/core/Framework/DataAbstractionLayer/Command/ConsoleProgressTrait.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\DataAbstractionLayer\Command;
  3. use Shopware\Core\Framework\Event\ProgressAdvancedEvent;
  4. use Shopware\Core\Framework\Event\ProgressFinishedEvent;
  5. use Shopware\Core\Framework\Event\ProgressStartedEvent;
  6. use Shopware\Core\Framework\Log\Package;
  7. use Symfony\Component\Console\Helper\ProgressBar;
  8. use Symfony\Component\Console\Style\SymfonyStyle;
  9. #[Package('core')]
  10. trait ConsoleProgressTrait
  11. {
  12. /**
  13. * @var SymfonyStyle|null
  14. */
  15. protected $io;
  16. /**
  17. * @var ProgressBar|null
  18. */
  19. protected $progress;
  20. /**
  21. * @return array<string, string>
  22. */
  23. public static function getSubscribedEvents(): array
  24. {
  25. return [
  26. ProgressStartedEvent::NAME => 'startProgress',
  27. ProgressAdvancedEvent::NAME => 'advanceProgress',
  28. ProgressFinishedEvent::NAME => 'finishProgress',
  29. ];
  30. }
  31. public function startProgress(ProgressStartedEvent $event): void
  32. {
  33. if (!$this->io) {
  34. return;
  35. }
  36. $this->progress = $this->io->createProgressBar($event->getTotal());
  37. $this->progress->setFormat("<info>[%message%]</info>\n%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%");
  38. $this->progress->setMessage($event->getMessage());
  39. }
  40. public function advanceProgress(ProgressAdvancedEvent $event): void
  41. {
  42. if (!$this->progress) {
  43. return;
  44. }
  45. $this->progress->advance($event->getStep());
  46. }
  47. public function finishProgress(ProgressFinishedEvent $event): void
  48. {
  49. if (!$this->io) {
  50. return;
  51. }
  52. if (!$this->progress) {
  53. return;
  54. }
  55. if (!$this->progress->getMaxSteps()) {
  56. return;
  57. }
  58. $this->progress->setMessage($event->getMessage());
  59. $this->progress->finish();
  60. $this->io->newLine(2);
  61. }
  62. }