Skip to content

Commit 67e7a93

Browse files
committed
Add ProgressScanner to manage progress of scan jobs
1 parent 73f0192 commit 67e7a93

File tree

2 files changed

+103
-17
lines changed

2 files changed

+103
-17
lines changed

src/PHPSemVerChecker/Console/Command/CompareCommand.php

+5-17
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
use PHPSemVerChecker\Finder\Finder;
1010
use PHPSemVerChecker\Reporter\JsonReporter;
1111
use PHPSemVerChecker\Reporter\Reporter;
12+
use PHPSemVerChecker\Scanner\ProgressScanner;
1213
use PHPSemVerChecker\Scanner\Scanner;
1314
use Symfony\Component\Console\Command\Command;
14-
use Symfony\Component\Console\Helper\ProgressBar;
1515
use Symfony\Component\Console\Input\InputArgument;
1616
use Symfony\Component\Console\Input\InputInterface;
1717
use Symfony\Component\Console\Input\InputOption;
@@ -64,22 +64,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
6464
$sourceFilter = new SourceFilter();
6565
$identicalCount = $sourceFilter->filter($sourceBefore, $sourceAfter);
6666

67-
$progress = new ProgressBar($output, count($sourceBefore) + count($sourceAfter));
68-
$progress->setFormat("%message%\n%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%");
69-
$output->writeln('');
70-
$progress->setMessage('Scanning before files');
71-
foreach ($sourceBefore as $file) {
72-
$scannerBefore->scan($file);
73-
$progress->advance();
74-
}
75-
76-
$progress->setMessage('Scanning after files');
77-
foreach ($sourceAfter as $file) {
78-
$scannerAfter->scan($file);
79-
$progress->advance();
80-
}
81-
82-
$progress->clear();
67+
$progress = new ProgressScanner($output);
68+
$progress->addJob($input->getArgument('source-before'), $sourceBefore, $scannerBefore);
69+
$progress->addJob($input->getArgument('source-after'), $sourceAfter, $scannerAfter);
70+
$progress->runJobs();
8371

8472
$registryBefore = $scannerBefore->getRegistry();
8573
$registryAfter = $scannerAfter->getRegistry();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
namespace PHPSemVerChecker\Scanner;
4+
5+
use Symfony\Component\Console\Helper\ProgressBar;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
/**
9+
* ProgressScanner helps run and display the progress of a scan job.
10+
*/
11+
class ProgressScanner
12+
{
13+
/**
14+
* @var string[][]
15+
*/
16+
private $fileLists = [];
17+
/**
18+
* @var \PHPSemVerChecker\Scanner\Scanner[]
19+
*/
20+
private $scanners = [];
21+
/**
22+
* @var \Symfony\Component\Console\Output\OutputInterface
23+
*/
24+
private $output;
25+
/**
26+
* @var \Symfony\Component\Console\Helper\ProgressBar
27+
*/
28+
private $progressBar;
29+
30+
/**
31+
* @param \Symfony\Component\Console\Output\OutputInterface $output
32+
*/
33+
public function __construct(OutputInterface $output)
34+
{
35+
$this->output = $output;
36+
}
37+
38+
/**
39+
* @param string $name
40+
* @param string[] $fileList
41+
* @param \PHPSemVerChecker\Scanner\Scanner $scanner
42+
*/
43+
public function addJob($name, $fileList, $scanner)
44+
{
45+
$this->fileLists[$name] = $fileList;
46+
$this->scanners[$name] = $scanner;
47+
}
48+
49+
/**
50+
* Run all registered jobs.
51+
*/
52+
public function runJobs()
53+
{
54+
foreach (array_keys($this->scanners) as $jobName) {
55+
$this->runJob($jobName);
56+
}
57+
}
58+
59+
/**
60+
* Run a single job.
61+
*
62+
* @param string $jobName
63+
*/
64+
public function runJob($jobName)
65+
{
66+
$progress = $this->getProgressBar();
67+
$progress->setMessage('Scanning ' . $jobName);
68+
$scanner = $this->scanners[$jobName];
69+
foreach ($this->fileLists[$jobName] as $filePath) {
70+
$scanner->scan($filePath);
71+
$progress->advance();
72+
}
73+
if ($progress->getProgress() === $progress->getMaxSteps()) {
74+
$progress->clear();
75+
}
76+
}
77+
78+
/**
79+
* @return int
80+
*/
81+
private function getFileCount()
82+
{
83+
return array_sum(array_map('count', $this->fileLists));
84+
}
85+
86+
/**
87+
* @return \Symfony\Component\Console\Helper\ProgressBar
88+
*/
89+
private function getProgressBar()
90+
{
91+
if ($this->progressBar === null) {
92+
$this->progressBar = new ProgressBar($this->output, $this->getFileCount());
93+
$this->progressBar->setFormat("%message%\n%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%");
94+
$this->output->writeln('');
95+
}
96+
return $this->progressBar;
97+
}
98+
}

0 commit comments

Comments
 (0)