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

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.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             ProgressStartedEvent::NAME => 'startProgress',
  22.             ProgressAdvancedEvent::NAME => 'advanceProgress',
  23.             ProgressFinishedEvent::NAME => 'finishProgress',
  24.         ];
  25.     }
  26.     public function startProgress(ProgressStartedEvent $event): void
  27.     {
  28.         if (!$this->io) {
  29.             return;
  30.         }
  31.         $this->progress $this->io->createProgressBar($event->getTotal());
  32.         $this->progress->setFormat("<info>[%message%]</info>\n%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%");
  33.         $this->progress->setMessage($event->getMessage());
  34.     }
  35.     public function advanceProgress(ProgressAdvancedEvent $event): void
  36.     {
  37.         if (!$this->progress) {
  38.             return;
  39.         }
  40.         $this->progress->advance($event->getStep());
  41.     }
  42.     public function finishProgress(ProgressFinishedEvent $event): void
  43.     {
  44.         if (!$this->progress) {
  45.             return;
  46.         }
  47.         if (!$this->progress->getMaxSteps()) {
  48.             return;
  49.         }
  50.         $this->progress->setMessage($event->getMessage());
  51.         $this->progress->finish();
  52.         $this->io->newLine(2);
  53.     }
  54. }