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
|
|
|
use Symfony\Component\Console\Terminal; |
8
|
|
|
|
9
|
|
|
class BenchmarkSymfonyProgressBar extends Benchmark |
10
|
|
|
{ |
11
|
|
|
public const DEFAULT_PROGRESSBAR_FORMAT = '[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%'; |
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
|
|
|
|
32
|
1 |
|
$this->progressBarWidth = $this->refineProgressBarWidth($progressBarWidth); |
33
|
|
|
|
34
|
1 |
|
$this->progressBar = new ProgressBar($this->output, $this->advanceSteps); |
35
|
1 |
|
$this->progressBar->setBarWidth($this->progressBarWidth); |
36
|
|
|
$progressStart = |
37
|
|
|
function (): void { |
38
|
1 |
|
$this->progressBar->start(); |
39
|
1 |
|
}; |
40
|
|
|
|
41
|
|
|
$progressAdvance = |
42
|
|
|
function (): void { |
43
|
1 |
|
$this->progressBar->advance(); |
44
|
1 |
|
}; |
45
|
|
|
|
46
|
|
|
$progressFinish = |
47
|
|
|
function (): void { |
48
|
1 |
|
$this->progressBar->finish(); |
49
|
1 |
|
$this->progressBar->clear(); |
50
|
1 |
|
}; |
51
|
|
|
|
52
|
1 |
|
$this->progressBar->setFormat(static::DEFAULT_PROGRESSBAR_FORMAT); |
53
|
1 |
|
$this->showProgressBy($progressStart, $progressAdvance, $progressFinish); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param null|int $progressBarWidth |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
1 |
|
protected function refineProgressBarWidth(?int $progressBarWidth): int |
61
|
|
|
{ |
62
|
|
|
return |
63
|
1 |
|
$progressBarWidth ?? (int)((new Terminal())->getWidth() * 0.8); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return ConsoleOutput |
68
|
|
|
*/ |
69
|
1 |
|
public function getOutput(): ConsoleOutput |
70
|
|
|
{ |
71
|
1 |
|
return $this->output; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return ProgressBar |
76
|
|
|
*/ |
77
|
1 |
|
public function getProgressBar(): ProgressBar |
78
|
|
|
{ |
79
|
1 |
|
return $this->progressBar; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return int |
84
|
|
|
*/ |
85
|
1 |
|
public function getProgressBarWidth(): int |
86
|
|
|
{ |
87
|
1 |
|
return $this->progressBarWidth; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
protected function showComment(string $comment = ''): void |
91
|
|
|
{ |
92
|
|
|
// $outputStyle = new OutputFormatterStyle('red', 'yellow', ['bold', 'blink']); |
93
|
|
|
// $this->output->getFormatter()->setStyle('fire', $outputStyle); |
94
|
|
|
// |
95
|
|
|
$this->output->writeln('<comment>' . $comment . '</>'); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|