1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
6
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
7
|
|
|
|
8
|
|
|
class BenchmarkSymfonyProgressBar extends Benchmark |
9
|
|
|
{ |
10
|
|
|
public const DEFAULT_PROGRESSBAR_FORMAT = '[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%'; |
11
|
|
|
public const PROGRESS_BAR_WIDTH = 80; |
12
|
|
|
|
13
|
|
|
/** @var ConsoleOutput */ |
14
|
|
|
protected $output; |
15
|
|
|
|
16
|
|
|
/** @var ProgressBar */ |
17
|
|
|
protected $progressBar; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
private $progressBarWidth; |
21
|
|
|
|
22
|
1 |
|
public function __construct( |
23
|
|
|
int $iterations = 1000, |
24
|
|
|
?int $progressBarMax = null, |
25
|
|
|
?int $progressBarWidth = null, |
26
|
|
|
?ConsoleOutput $output = null |
27
|
|
|
) { |
28
|
1 |
|
parent::__construct($iterations); |
29
|
1 |
|
$this->output = $output ?? new ConsoleOutput(); |
30
|
1 |
|
$this->advanceSteps = $progressBarMax ?? $this->advanceSteps; |
31
|
1 |
|
$this->progressBarWidth = $progressBarWidth ?? self::PROGRESS_BAR_WIDTH; |
32
|
|
|
|
33
|
1 |
|
$this->progressBar = new ProgressBar($this->output, $this->advanceSteps); |
34
|
1 |
|
$this->progressBar->setBarWidth($this->progressBarWidth); |
35
|
|
|
$progressStart = |
36
|
|
|
function (): void { |
37
|
1 |
|
$this->progressBar->start(); |
38
|
1 |
|
}; |
39
|
|
|
|
40
|
|
|
$progressAdvance = |
41
|
|
|
function (): void { |
42
|
1 |
|
$this->progressBar->advance(); |
43
|
1 |
|
}; |
44
|
|
|
|
45
|
|
|
$progressFinish = |
46
|
|
|
function (): void { |
47
|
1 |
|
$this->progressBar->finish(); |
48
|
1 |
|
$this->progressBar->clear(); |
49
|
1 |
|
}; |
50
|
|
|
|
51
|
1 |
|
$this->progressBar->setFormat(static::DEFAULT_PROGRESSBAR_FORMAT); |
52
|
1 |
|
$this->showProgressBy($progressStart, $progressAdvance, $progressFinish); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return ConsoleOutput |
57
|
|
|
*/ |
58
|
1 |
|
public function getOutput(): ConsoleOutput |
59
|
|
|
{ |
60
|
1 |
|
return $this->output; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return ProgressBar |
65
|
|
|
*/ |
66
|
1 |
|
public function getProgressBar(): ProgressBar |
67
|
|
|
{ |
68
|
1 |
|
return $this->progressBar; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return int |
73
|
|
|
*/ |
74
|
1 |
|
public function getProgressBarWidth(): int |
75
|
|
|
{ |
76
|
1 |
|
return $this->progressBarWidth; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|