Last active
October 21, 2022 15:55
-
-
Save vertexvaar/f0121ab9ed5ae3e16da49e40c3e8e411 to your computer and use it in GitHub Desktop.
Symfony Console mulitple subprocess status bar with overall progress and message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Helper\ProgressBar; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Output\ConsoleOutput; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use TYPO3\CMS\Core\Database\ConnectionPool; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
class SomeCommand extends Command | |
{ | |
private const CHUNK_SIZE = 100; | |
private const TABLE = 'tt_content'; | |
public function execute(InputInterface $input, OutputInterface $output) | |
{ | |
if (!($output instanceof ConsoleOutput)) { | |
throw new \LogicException(sprintf('Output must be instance of ConsoleOutput, "%s" given', get_class($output))); | |
} | |
$section1 = $output->section(); | |
$section2 = $output->section(); | |
$section3 = $output->section(); | |
$section4 = $output->section(); | |
ProgressBar::setFormatDefinition('normal', ' %current:3s%/%max:3s% [%bar%] %percent:3s%%'); | |
$section1->clear(); | |
$progress1 = new ProgressBar($section1, 10); | |
$progress1->start(); | |
for ($i = 0; $i < 10; $i++) { | |
$overallCount = rand(600, 900); | |
$section2->overwrite('Processing ' . uniqid() . ' (' . $i . '/10)'); | |
$section3->clear(); | |
$progress2 = new ProgressBar($section3, (int)ceil($overallCount / self::CHUNK_SIZE)); | |
$progress2->start(); | |
for ($j = 0; $j < $overallCount; $j += self::CHUNK_SIZE) { | |
$rows = range(0, self::CHUNK_SIZE - 1); | |
$section4->clear(); | |
$progress3 = new ProgressBar($section4, count($rows)); | |
$progress3->start(); | |
foreach ($rows as $row) { | |
$progress3->advance(); | |
// TODO: Actually do something with that row | |
usleep(5000); | |
} | |
$progress2->advance(); | |
} | |
$progress1->advance(); | |
} | |
$output->writeln('Done'); | |
return 0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx for that.
Here a very simple example within execute():