1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace AlecRabbit\Tools\Reports\Formatters; |
5
|
|
|
|
6
|
|
|
use AlecRabbit\Tools\AbstractCounter; |
7
|
|
|
use AlecRabbit\Tools\Reports\AbstractCounterReport; |
8
|
|
|
use AlecRabbit\Tools\Reports\Contracts\ReportInterface; |
9
|
|
|
use AlecRabbit\Tools\Reports\SimpleCounterReport; |
10
|
|
|
use AlecRabbit\Tools\Reports\ProfilerReport; |
11
|
|
|
use AlecRabbit\Tools\Reports\TimerReport; |
12
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
13
|
|
|
use function AlecRabbit\typeOf; |
14
|
|
|
|
15
|
|
|
class ProfilerReportFormatter extends ReportFormatter |
16
|
|
|
{ |
17
|
|
|
/** @var string */ |
18
|
|
|
private $elapsed = ''; |
19
|
|
|
|
20
|
|
|
/** {@inheritdoc} */ |
21
|
3 |
|
public function process(ReportInterface $report): string |
22
|
|
|
{ |
23
|
3 |
|
if ($report instanceof ProfilerReport) { |
24
|
|
|
return |
25
|
2 |
|
sprintf( |
26
|
2 |
|
'%s%s%s', |
27
|
2 |
|
$this->countersStrings($report), |
28
|
2 |
|
$this->timersStrings($report), |
29
|
2 |
|
$this->elapsed |
30
|
|
|
); |
31
|
|
|
} |
32
|
1 |
|
$this->wrongReportType(ProfilerReport::class, $report); |
33
|
|
|
// @codeCoverageIgnoreStart |
34
|
|
|
return ''; |
35
|
|
|
// @codeCoverageIgnoreEnd |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ProfilerReport $report |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
2 |
|
protected function countersStrings(ProfilerReport $report): string |
43
|
|
|
{ |
44
|
2 |
|
$r = ''; |
45
|
2 |
|
foreach ($report->getCountersReports() as $counterReport) { |
46
|
2 |
|
if ($counterReport instanceof AbstractCounterReport && DEFAULT_NAME === $counterReport->getName()) { |
47
|
2 |
|
$r .= $counterReport->isStarted() ? $counterReport : ''; |
48
|
|
|
} else { |
49
|
|
|
$r .= $counterReport; |
50
|
|
|
} |
51
|
|
|
} |
52
|
2 |
|
return $r; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param ProfilerReport $report |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
2 |
|
protected function timersStrings(ProfilerReport $report): string |
60
|
|
|
{ |
61
|
2 |
|
$r = ''; |
62
|
2 |
|
foreach ($report->getTimersReports() as $timerReport) { |
63
|
2 |
|
if ($timerReport instanceof TimerReport && DEFAULT_NAME === $timerReport->getName()) { |
64
|
2 |
|
$this->elapsed = (string)$timerReport; |
65
|
|
|
// $r .= $timerReport; |
66
|
|
|
} else { |
67
|
|
|
$r .= $timerReport; |
68
|
|
|
} |
69
|
|
|
} |
70
|
2 |
|
return $r; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|