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