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

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