|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* User: alec |
|
4
|
|
|
* Date: 10.12.18 |
|
5
|
|
|
* Time: 14:22 |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
|
10
|
|
|
|
|
11
|
|
|
use AlecRabbit\Tools\Reports\TimerReport; |
|
12
|
|
|
use function AlecRabbit\format_time_auto; |
|
13
|
|
|
use const AlecRabbit\Constants\Accessories\DEFAULT_NAME; |
|
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
class TimerReportFormatter extends Formatter |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var TimerReport */ |
|
18
|
|
|
protected $report; |
|
19
|
|
|
|
|
20
|
|
|
public function setStyles(): void |
|
21
|
|
|
{ |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function getString(): string |
|
25
|
|
|
{ |
|
26
|
|
|
if (DEFAULT_NAME === $name = $this->report->getName()) { |
|
27
|
|
|
return $this->elapsed(); |
|
28
|
|
|
} |
|
29
|
|
|
return $this->full($name); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @return string |
|
34
|
|
|
* @throws \Throwable |
|
35
|
|
|
*/ |
|
36
|
|
|
public function elapsed(): string |
|
37
|
|
|
{ |
|
38
|
|
|
return |
|
39
|
|
|
sprintf( |
|
40
|
|
|
'Elapsed: %s %s', |
|
41
|
|
|
$this->theme->comment(format_time_auto($this->report->getElapsed())), |
|
|
|
|
|
|
42
|
|
|
PHP_EOL |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
* @return string |
|
49
|
|
|
* @throws \Throwable |
|
50
|
|
|
*/ |
|
51
|
|
|
public function full(string $name): string |
|
52
|
|
|
{ |
|
53
|
|
|
try { |
|
54
|
|
|
$str = sprintf( |
|
55
|
|
|
'Timer:[%s] Average: %s, Last: %s, Min(%s): %s, Max(%s): %s, Count: %s' . PHP_EOL, |
|
56
|
|
|
$this->theme->info($name), |
|
|
|
|
|
|
57
|
|
|
$this->theme->comment(format_time_auto($this->report->getAverageValue())), |
|
58
|
|
|
format_time_auto($this->report->getLastValue()), |
|
59
|
|
|
$this->report->getMinValueIteration(), |
|
60
|
|
|
format_time_auto($this->report->getMinValue()), |
|
61
|
|
|
$this->report->getMaxValueIteration(), |
|
62
|
|
|
format_time_auto($this->report->getMaxValue()), |
|
63
|
|
|
$this->report->getCount() |
|
64
|
|
|
); |
|
65
|
|
|
} catch (\Throwable $e) { |
|
66
|
|
|
$str = |
|
67
|
|
|
sprintf( |
|
68
|
|
|
'Timer:[%s] %s' . PHP_EOL, |
|
69
|
|
|
$this->theme->info($name), |
|
70
|
|
|
$this->theme->red('Exception encountered') |
|
|
|
|
|
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
return $str; |
|
74
|
|
|
} |
|
75
|
|
|
} |