/** * Get progress bar instance * * @param integer $total * * @return ProgressBar */ public function getProgress($total = null) { if (!$this->progress || $this->progress->getMaxSteps() === $this->progress->getProgress()) { $this->progress = new ProgressBar($this->output, $total); } return $this->progress; }
/** * @param RunnerEvent $runnerEvent */ public function finishProgress(RunnerEvent $runnerEvent) { if ($this->progressBar->getProgress() != $this->progressBar->getMaxSteps()) { $this->progressBar->setFormat('<fg=red>%message%</fg=red>'); $this->progressBar->setMessage('Aborted ...'); } $this->progressBar->finish(); $this->output->writeln(''); }
/** * {@inheritdoc} */ public function increment($increment = 1) { if ($this->bar === null) { return; } $this->bar->advance($increment); if ($this->bar->getProgress() === $this->bar->getMaxSteps()) { $this->consoleIO->getOutput()->writeln(' - Finished!'); } }
/** * 绑定事件 * @param Runner $runner * @param OutputInterface $output */ protected function bindEventsForUi(Runner $runner, OutputInterface $output) { $chain = $runner->getExaminationChain(); $progressBar = new ProgressBar($output, count($chain)); $dispatcher = $runner->getDispatcher(); $dispatcher->bind(Runner::EVENT_RUN, function () use($output, $progressBar) { $output->writeln("Hi {$this->author}!"); $output->writeln("Runner started and will be performed {$progressBar->getMaxSteps()} tasks"); $output->write(PHP_EOL); $progressBar->start(); }); //执行新的测试任务 $dispatcher->bind(Runner::EVENT_EXAMINATION_EXECUTE, function (Event $event) use($output, $progressBar) { $examination = $event->getArgument('examination'); $examination->getReport()->write('_beginTime', microtime(true)); }); //测试任务执行完毕 $dispatcher->bind(Runner::EVENT_EXAMINATION_EXECUTE, function (Event $event) use($output, $progressBar) { $examination = $event->getArgument('examination'); $examination->getReport()->write('_endTime', microtime(true)); $consume = microtime(true) - $examination->getReport()->read('_beginTime'); $examination->getReport()->write('consume', $consume); $progressBar->advance(1); }); $dispatcher->bind(Runner::EVENT_FINISH, function () use($output, $progressBar) { $progressBar->finish(); $output->writeln(PHP_EOL); $output->writeln("Runner stop,Generating test report"); }); $dispatcher->bind(Runner::EVENT_FINISH, function () use($runner) { $this->makeReport($runner); }); }
/** * 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 * * @return null|int null or 0 if everything went fine, or an error code * * @see setCode() */ protected function execute(InputInterface $input, OutputInterface $output) { $entity = $input->getArgument('entity'); /** @var EntityManager $entityManager */ $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); if ($entity === 'article') { $output->writeln('<comment>This might take a while.</comment>'); $journals = $entityManager->getRepository('OjsJournalBundle:Journal')->findAll(); $totalProgress = new ProgressBar($output, count($journals)); $totalProgress->setFormat('%current%/%max% [%bar%] %message%'); if ($totalProgress->getMaxSteps() > 0) { $totalProgress->setMessage('Numerating...'); $totalProgress->start(); } /** @var Journal $journal */ foreach ($journals as $journal) { $articles = $entityManager->getRepository('OjsJournalBundle:Article')->findBy(['journal' => $journal]); $totalProgress->setMessage('Numerating articles of "' . $journal->getTitle() . '"'); foreach ($articles as $article) { NumeratorHelper::numerateArticle($article, $entityManager); } $totalProgress->advance(); } $totalProgress->finish(); $output->writeln(''); // Necessary, unfortunately. $output->writeln('<info>Done.</info>'); } else { if ($entity === 'issue') { $output->writeln('<comment>This might take a while.</comment>'); $journals = $entityManager->getRepository('OjsJournalBundle:Journal')->findAll(); $totalProgress = new ProgressBar($output, count($journals)); $totalProgress->setFormat('%current%/%max% [%bar%] %message%'); if ($totalProgress->getMaxSteps() > 0) { $totalProgress->setMessage('Numerating...'); $totalProgress->start(); } /** @var Journal $journal */ foreach ($journals as $journal) { $issues = $entityManager->getRepository('OjsJournalBundle:Issue')->findBy(['journal' => $journal]); $totalProgress->setMessage('Numerating issues of "' . $journal->getTitle() . '"'); foreach ($issues as $issue) { NumeratorHelper::numerateIssue($issue, $entityManager); } $totalProgress->advance(); } $totalProgress->finish(); $output->writeln(''); // Necessary, unfortunately. $output->writeln('<info>Done.</info>'); } else { $output->writeln('<error>This entity is not yet supported.</error>'); } } }
function it_finishes_progress_early(OutputInterface $output, ProgressBar $progressBar, RunnerEvent $event) { $progressBar->getProgress()->willReturn(1); $progressBar->getMaxSteps()->willReturn(2); $progressBar->setFormat(Argument::type('string'))->shouldBeCalled(); $progressBar->setMessage(Argument::type('string'))->shouldBeCalled(); $progressBar->setOverwrite(false)->shouldBeCalled(); $progressBar->finish()->shouldBeCalled(); $output->writeln('')->shouldBeCalled(); $this->finishProgress($event); }
/** * @param \FlickrDownloadr\Photo\Photo $photo * @param string $filename * @param string $dirname * @return int Number of bytes that were written to the file, or FALSE on failure */ public function download(Photo $photo, $filename, $dirname) { $url = $photo->getUrl(); if ($this->dryRun) { return 0; } \Nette\Utils\FileSystem::createDir($dirname . '/' . dirname($filename)); $this->setupProgressBar(); $ctx = stream_context_create(); stream_context_set_params($ctx, array("notification" => $this->getNotificationCallback($filename))); $bytes = file_put_contents($dirname . '/' . $filename, fopen($url, 'r', FALSE, $ctx)); if ($bytes === FALSE) { $this->progress->setMessage('<error>Error!</error>', 'final_report'); } else { list($time, $size, $speed) = $this->getFinalStats($this->progress->getMaxSteps(), $this->progress->getStartTime()); $this->progress->setMessage('<comment>[' . $size . ' in ' . $time . ' (' . $speed . ')]</comment>', 'final_report'); $this->progress->setFormat('%message% %final_report%' . "\n"); } $this->progress->finish(); $this->output->writeln(''); return $bytes; }
public function testProgressBar() { $progressBar = new ProgressBar(new NullOutput()); $output = new InstallOutput(null, $progressBar); $output->start('Start', 5); $this->assertEquals(5, $progressBar->getMaxSteps()); $this->assertEquals('Start', $progressBar->getMessage('message')); $output->step('Step 1'); $this->assertEquals(1, $progressBar->getProgress()); $this->assertEquals('Step 1', $progressBar->getMessage('message')); $output->end('End'); $this->assertEquals(5, $progressBar->getProgress()); $this->assertEquals('End', $progressBar->getMessage('message')); }
/** * 绑定ui * @param MagicHand $magicHand * @param OutputInterface $output */ protected function bindEventsForUi(MagicHand $magicHand, OutputInterface $output) { $magicHand->getDispatcher()->bind(MagicHand::EVENT_BEGIN, function (Event $event) use($output) { $images = $event->getArgument('images'); $progressBar = new ProgressBar($output, count($images)); $output->writeln("Magic Hand started and will be performed {$progressBar->getMaxSteps()} images"); $output->write(PHP_EOL); $progressBar->start(); $this->progressBar = $progressBar; }); $magicHand->getDispatcher()->bind(MagicHand::EVENT_PROCESS, function (Event $event) use($output) { $this->progressBar->advance(1); }); $magicHand->getDispatcher()->bind(MagicHand::EVENT_END, function (Event $event) use($output) { $this->progressBar->finish(); $output->writeln(PHP_EOL); $output->writeln("Work ok"); }); }
public function reDrawProgressBar() { if ($this->progress !== null && $this->progress->getStartTime() !== null && $this->progress->getProgress() !== $this->progress->getMaxSteps() && $this->progresseBar === true) { $this->progress->display(); } }