protected function getProgressBar($nbIteration, $message) { $bar = new ProgressBar($this->output, $nbIteration); ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) { static $i = 0; $mem = memory_get_usage(); $colors = $i++ ? '41;37' : '44;37'; return "[" . $colors . 'm ' . Helper::formatMemory($mem) . " [0m"; }); $bar->setFormat(" [44;37m %title:-38s% [0m\n %current%/%max% %bar% %percent:3s%%\n 🏁 %remaining:-10s% %memory:37s%\n"); $bar->setBarCharacter("[32m●[0m"); $bar->setEmptyBarCharacter("[31m●[0m"); $bar->setMessage($message, 'title'); $bar->start(); return $bar; }
private static function initPlaceholderFormatters() { return array('indicator' => function (ProgressIndicator $indicator) { return $indicator->indicatorValues[$indicator->indicatorCurrent % count($indicator->indicatorValues)]; }, 'message' => function (ProgressIndicator $indicator) { return $indicator->message; }, 'elapsed' => function (ProgressIndicator $indicator) { return Helper::formatTime(time() - $indicator->startTime); }, 'memory' => function () { return Helper::formatMemory(memory_get_usage(true)); }); }
private static function initPlaceholderFormatters() { return array('indicator' => function (ProgressIndicator $indicator) { return $indicator->getCurrentValue(); }, 'message' => function (ProgressIndicator $indicator) { return $indicator->getMessage(); }, 'elapsed' => function (ProgressIndicator $indicator) { return Helper::formatTime(time() - $indicator->getStartTime()); }, 'memory' => function () { return Helper::formatMemory(memory_get_usage(true)); }); }
private static function initPlaceholderFormatters() { return array('bar' => function (ProgressBar $bar, OutputInterface $output) { $completeBars = floor($bar->getMaxSteps() > 0 ? $bar->getProgressPercent() * $bar->getBarWidth() : $bar->getProgress() % $bar->getBarWidth()); $display = str_repeat($bar->getBarCharacter(), $completeBars); if ($completeBars < $bar->getBarWidth()) { $emptyBars = $bar->getBarWidth() - $completeBars - Helper::strlenWithoutDecoration($output->getFormatter(), $bar->getProgressCharacter()); $display .= $bar->getProgressCharacter() . str_repeat($bar->getEmptyBarCharacter(), $emptyBars); } return $display; }, 'elapsed' => function (ProgressBar $bar) { return Helper::formatTime(time() - $bar->getStartTime()); }, 'remaining' => function (ProgressBar $bar) { if (!$bar->getMaxSteps()) { throw new LogicException('Unable to display the remaining time if the maximum number of steps is not set.'); } if (!$bar->getProgress()) { $remaining = 0; } else { $remaining = round((time() - $bar->getStartTime()) / $bar->getProgress() * ($bar->getMaxSteps() - $bar->getProgress())); } return Helper::formatTime($remaining); }, 'estimated' => function (ProgressBar $bar) { if (!$bar->getMaxSteps()) { throw new LogicException('Unable to display the estimated time if the maximum number of steps is not set.'); } if (!$bar->getProgress()) { $estimated = 0; } else { $estimated = round((time() - $bar->getStartTime()) / $bar->getProgress() * $bar->getMaxSteps()); } return Helper::formatTime($estimated); }, 'memory' => function (ProgressBar $bar) { return Helper::formatMemory(memory_get_usage(true)); }, 'current' => function (ProgressBar $bar) { return str_pad($bar->getProgress(), $bar->getStepWidth(), ' ', STR_PAD_LEFT); }, 'max' => function (ProgressBar $bar) { return $bar->getMaxSteps(); }, 'percent' => function (ProgressBar $bar) { return floor($bar->getProgressPercent() * 100); }); }
public function testAnsiColorsAndEmojis() { $bar = new ProgressBar($output = $this->getOutputStream(), 15); ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) { static $i = 0; $mem = 100000 * $i; $colors = $i++ ? '41;37' : '44;37'; return "[" . $colors . 'm ' . Helper::formatMemory($mem) . " [0m"; }); $bar->setFormat(" [44;37m %title:-37s% [0m\n %current%/%max% %bar% %percent:3s%%\n 🏁 %remaining:-10s% %memory:37s%"); $bar->setBarCharacter($done = "[32m●[0m"); $bar->setEmptyBarCharacter($empty = "[31m●[0m"); $bar->setProgressCharacter($progress = "[32m➤ [0m"); $bar->setMessage('Starting the demo... fingers crossed', 'title'); $bar->start(); $bar->setMessage('Looks good to me...', 'title'); $bar->advance(4); $bar->setMessage('Thanks, bye', 'title'); $bar->finish(); rewind($output->getStream()); $this->assertEquals($this->generateOutput(" [44;37m Starting the demo... fingers crossed [0m\n" . ' 0/15 ' . $progress . str_repeat($empty, 26) . " 0%\n" . " 🏁 1 sec [44;37m 0 B [0m") . $this->generateOutput(" [44;37m Looks good to me... [0m\n" . ' 4/15 ' . str_repeat($done, 7) . $progress . str_repeat($empty, 19) . " 26%\n" . " 🏁 1 sec [41;37m 97 KiB [0m") . $this->generateOutput(" [44;37m Thanks, bye [0m\n" . ' 15/15 ' . str_repeat($done, 28) . " 100%\n" . " 🏁 1 sec [41;37m 195 KiB [0m"), stream_get_contents($output->getStream())); }
/** * Executes the current command. * * This method is not abstract because you can use this class * as a concrete class. In this case, instead of defining the * execute() method, you set the code to execute by passing * a Closure to the setCode() method. * * @param InputInterface $input An InputInterface instance * @param OutputInterface $output An OutputInterface instance * * @throws LogicException When this abstract method is not implemented * * @return null|int null or 0 if everything went fine, or an error code * * @see setCode() */ protected function execute(InputInterface $input, OutputInterface $output) { $startTime = microtime(true); $startMemUsage = memory_get_usage(true); $output->writeln($this->getApplication()->getLongVersion() . " by overtrue and contributors.\n"); $options = $this->mergeOptions(); $verbosity = $output->getVerbosity(); if ($verbosity >= OutputInterface::VERBOSITY_DEBUG) { $output->writeln('Options: ' . json_encode($options)); } $linter = new Linter($options['path'], $options['exclude'], $options['extensions']); $linter->setProcessLimit($options['jobs']); if (!$input->getOption('no-cache') && Cache::isCached()) { $output->writeln('Using cache.'); $linter->setCache(Cache::get()); } $fileCount = count($linter->getFiles()); if ($fileCount <= 0) { $output->writeln('<info>Could not find files to lint</info>'); return 0; } $errors = $this->executeLint($linter, $output, $fileCount, !$input->getOption('no-cache')); $timeUsage = Helper::formatTime(microtime(true) - $startTime); $memUsage = Helper::formatMemory(memory_get_usage(true) - $startMemUsage); $code = 0; $errCount = count($errors); $output->writeln("\n\nTime: {$timeUsage}, Memory: {$memUsage}MB\n"); if ($errCount > 0) { $output->writeln('<error>FAILURES!</error>'); $output->writeln("<error>Files: {$fileCount}, Failures: {$errCount}</error>"); $this->showErrors($errors); $code = 1; } else { $output->writeln("<info>OK! (Files: {$fileCount}, Success: {$fileCount})</info>"); } return $code; }
protected function displayAvancement($options, $done, $size, $showProgress, $output, $progress) { //advance $done += $options['slice']; if ($done > $size) { $done = $size; } //showing where we're at. if ($showProgress) { if ($output->isDecorated()) { $progress->advance(); } else { $output->writeln("did " . $done . " over (" . $size . ") memory: " . Helper::formatMemory(memory_get_usage(true))); } } return $done; }
/** * Chooses the best compressed file format to download (ZIP or TGZ) depending upon the * available operating system uncompressing commands and the enabled PHP extensions * and it downloads the file. * * @return $this * * @throws \RuntimeException If the Symfony archive could not be downloaded */ protected function download() { $this->output->writeln(sprintf("\n Downloading %s...\n", $this->getDownloadedApplicationType())); // decide which is the best compressed version to download $distill = new Distill(); $symfonyArchiveFile = $distill->getChooser()->setStrategy(new MinimumSize())->addFilesWithDifferentExtensions($this->getRemoteFileUrl(), ['tgz', 'zip'])->getPreferredFile(); /** @var ProgressBar|null $progressBar */ $progressBar = null; $downloadCallback = function (ProgressEvent $event) use(&$progressBar) { $downloadSize = $event->downloadSize; $downloaded = $event->downloaded; // progress bar is only displayed for files larger than 1MB if ($downloadSize < 1 * 1024 * 1024) { return; } if (null === $progressBar) { ProgressBar::setPlaceholderFormatterDefinition('max', function (ProgressBar $bar) { return Helper::formatMemory($bar->getMaxSteps()); }); ProgressBar::setPlaceholderFormatterDefinition('current', function (ProgressBar $bar) { return str_pad(Helper::formatMemory($bar->getProgress()), 11, ' ', STR_PAD_LEFT); }); $progressBar = new ProgressBar($this->output, $downloadSize); $progressBar->setFormat('%current%/%max% %bar% %percent:3s%%'); $progressBar->setRedrawFrequency(max(1, floor($downloadSize / 1000))); $progressBar->setBarWidth(60); if (!defined('PHP_WINDOWS_VERSION_BUILD')) { $progressBar->setEmptyBarCharacter('░'); // light shade character \u2591 $progressBar->setProgressCharacter(''); $progressBar->setBarCharacter('▓'); // dark shade character \u2593 } $progressBar->start(); } $progressBar->setProgress($downloaded); }; $client = $this->getGuzzleClient(); // store the file in a temporary hidden directory with a random name $this->downloadedFilePath = rtrim(getcwd(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . '.' . uniqid(time()) . DIRECTORY_SEPARATOR . 'symfony.' . pathinfo($symfonyArchiveFile, PATHINFO_EXTENSION); try { $request = $client->createRequest('GET', $symfonyArchiveFile); $request->getEmitter()->on('progress', $downloadCallback); $response = $client->send($request); } catch (ClientException $e) { if ('new' === $this->getName() && ($e->getCode() === 403 || $e->getCode() === 404)) { throw new \RuntimeException(sprintf("The selected version (%s) cannot be installed because it does not exist.\n" . "Execute the following command to install the latest stable Symfony release:\n" . '%s new %s', $this->version, $_SERVER['PHP_SELF'], str_replace(getcwd() . DIRECTORY_SEPARATOR, '', $this->projectDir))); } else { throw new \RuntimeException(sprintf("There was an error downloading %s from symfony.com server:\n%s", $this->getDownloadedApplicationType(), $e->getMessage()), null, $e); } } $this->fs->dumpFile($this->downloadedFilePath, $response->getBody()); if (null !== $progressBar) { $progressBar->finish(); $this->output->writeln("\n"); } return $this; }
/** * @param StopwatchEvent $event * * @return string */ private function formatStopwatchEvent(StopwatchEvent $event) { return sprintf('Time: %s, Memory: %s.', Helper::formatTime($event->getDuration() / 1000), Helper::formatMemory($event->getMemory())); }