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

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