Skip to content

Commit 7b15429

Browse files
[4.x] Remove Symfony
1 parent aec9d00 commit 7b15429

3 files changed

Lines changed: 172 additions & 0 deletions

File tree

src/View/AbstractConsoleView.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\View;
6+
7+
abstract class AbstractConsoleView
8+
{
9+
protected static $stream;
10+
11+
protected function writeLine(string $line): void
12+
{
13+
fwrite($this->stream(), $line . PHP_EOL);
14+
}
15+
16+
protected function write(string $line): void
17+
{
18+
fwrite($this->stream(), $line);
19+
}
20+
21+
/**
22+
* @return resource
23+
*/
24+
abstract protected function stream();
25+
}

src/View/ConsoleProgressBar.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\View;
6+
7+
use function floor;
8+
use function max;
9+
use function str_repeat;
10+
11+
class ConsoleProgressBar extends AbstractConsoleView
12+
{
13+
protected int $current = 0;
14+
15+
protected int $barWidth = 28;
16+
17+
public function __construct(
18+
protected int $total
19+
) {
20+
$this->total = max(1, $this->total);
21+
22+
$this->display();
23+
}
24+
25+
public function advance(int $step = 1): void
26+
{
27+
$this->current += $step;
28+
29+
$this->display();
30+
}
31+
32+
public function finish(): void
33+
{
34+
$this->current = $this->total;
35+
36+
$this->display();
37+
38+
$this->write(PHP_EOL);
39+
}
40+
41+
protected function display(): void
42+
{
43+
$percent = $this->current / $this->total;
44+
$filled = (int) floor($percent * $this->barWidth);
45+
$empty = $this->barWidth - $filled;
46+
$percText = (int) floor($percent * 100);
47+
48+
$bar = str_repeat('', $filled) . str_repeat('', $empty);
49+
50+
$line = " {$this->current}/{$this->total} [{$bar}] {$percText}%";
51+
52+
$this->write("\r" . $line);
53+
}
54+
55+
/**
56+
* @return resource
57+
*/
58+
protected function stream()
59+
{
60+
if (static::$stream === null) {
61+
static::$stream = fopen('php://stderr', 'w');
62+
}
63+
64+
return static::$stream;
65+
}
66+
}

src/View/ConsoleTable.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace DragonCode\Benchmark\View;
6+
7+
use function array_keys;
8+
use function array_map;
9+
use function array_values;
10+
use function implode;
11+
use function max;
12+
use function mb_str_pad;
13+
use function mb_strlen;
14+
15+
class ConsoleTable extends AbstractConsoleView
16+
{
17+
18+
public function show(array $data): void
19+
{
20+
$headers = $this->headers($data);
21+
$widths = $this->columnWidths($headers, $data);
22+
23+
$this->writeLine($this->separator($widths));
24+
$this->writeLine($this->formatRow($headers, $widths));
25+
$this->writeLine($this->separator($widths));
26+
27+
foreach ($data as $row) {
28+
$this->writeLine($this->formatRow(array_values($row), $widths));
29+
}
30+
31+
$this->writeLine($this->separator($widths));
32+
}
33+
34+
protected function headers(array $data): array
35+
{
36+
return array_keys(array_values($data)[0]);
37+
}
38+
39+
protected function columnWidths(array $headers, array $data): array
40+
{
41+
$widths = array_map(fn ($header) => mb_strlen((string) $header), $headers);
42+
43+
foreach ($data as $row) {
44+
foreach (array_values($row) as $i => $value) {
45+
$widths[$i] = max($widths[$i], mb_strlen((string) $value));
46+
}
47+
}
48+
49+
return $widths;
50+
}
51+
52+
protected function separator(array $widths): string
53+
{
54+
$parts = array_map(fn (int $w) => str_repeat('-', $w + 2), $widths);
55+
56+
return '+' . implode('+', $parts) . '+';
57+
}
58+
59+
protected function formatRow(array $values, array $widths): string
60+
{
61+
$cells = [];
62+
63+
foreach ($values as $i => $value) {
64+
$cells[] = ' ' . mb_str_pad((string) $value, $widths[$i]) . ' ';
65+
}
66+
67+
return '|' . implode('|', $cells) . '|';
68+
}
69+
70+
/**
71+
* @return resource
72+
*/
73+
protected function stream()
74+
{
75+
if (static::$stream === null) {
76+
static::$stream = fopen('php://stdout', 'w');
77+
}
78+
79+
return static::$stream;
80+
}
81+
}

0 commit comments

Comments
 (0)