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