* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 */ namespace App\Console; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Process\Exception\ProcessTimedOutException; use Symfony\Component\Process\Process; trait ConsoleTools { /** * @var SymfonyStyle */ protected $io; private function cyan($line): void { $this->io->writeln(sprintf('%s', $line)); } private function white($line): void { $this->io->writeln(PHP_EOL.$line); } private function magenta($line): void { $this->io->writeln(sprintf('%s', $line)); } private function green($line): void { $this->io->writeln(sprintf('%s', $line)); } private function red($line): void { $this->io->writeln(sprintf('%s', $line)); } private function blue($line): void { $this->io->writeln(sprintf('%s', $line)); } private function done(): void { $this->green('[Done]'); } private function header($line): void { $this->blue(str_repeat('=', 50)); $this->io->write($line); $this->blue(str_repeat('=', 50)); } private function alertSuccess($line): void { $this->io->writeln(sprintf('[ !! %s !! ]', $line)); } private function alertDanger($line): void { $this->io->writeln(sprintf('[ !! %s !! ]', $line)); } private function alertInfo($line): void { $this->io->writeln(sprintf('[ !! %s !! ]', $line)); } private function alertWarning($line): void { $this->io->writeln(sprintf('[ !! %s !! ]', $line)); } private function commands(array $commands, $silent = false): void { foreach ($commands as $command) { $process = $this->process($command, $silent); if (! $silent) { echo "\n\n"; $this->warn($process->getOutput()); } } } private function process($command, $silent = false): Process { if (! $silent) { $this->cyan($command); $bar = $this->progressStart(); } $process = Process::fromShellCommandline($command); $process->setTimeout(3_600); $process->start(); while ($process->isRunning()) { try { $process->checkTimeout(); } catch (ProcessTimedOutException) { $this->red(sprintf("'%s' timed out.!", $command)); } if (! $silent) { $bar->advance(); } usleep(200_000); } if (! $silent) { $this->progressStop($bar); } $process->stop(); if (! $process->isSuccessful()) { $this->red($process->getErrorOutput()); //die(); } return $process; } protected function progressStart(): ProgressBar { $bar = $this->io->createProgressBar(); $bar->setBarCharacter('='); $bar->setFormat('[%bar%] (%message%)'); $bar->setMessage('Please Wait ...'); //$bar->setRedrawFrequency(20); todo: may be useful for platforms like CentOS $bar->start(); return $bar; } protected function progressStop(ProgressBar $progressBar): void { $progressBar->setMessage('Done!'); $progressBar->finish(); } }