1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Accessories\MemoryUsage; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Contracts\AbstractFormatter; |
6
|
|
|
use AlecRabbit\Accessories\MemoryUsage\Contracts\MemoryUsageReportFormatterInterface; |
7
|
|
|
use AlecRabbit\Accessories\Pretty; |
8
|
|
|
use function AlecRabbit\Helpers\bounds; |
9
|
|
|
use const AlecRabbit\Helpers\Strings\Constants\BYTES_UNITS; |
10
|
|
|
|
11
|
|
|
class MemoryUsageReportFormatter extends AbstractFormatter implements MemoryUsageReportFormatterInterface |
12
|
|
|
{ |
13
|
|
|
public const STRING_FORMAT = 'Memory: %s(%s) Real: %s(%s)'; |
14
|
|
|
public const MAX_DECIMALS = 3; |
15
|
|
|
|
16
|
|
|
/** @var null|array */ |
17
|
|
|
private static $unitsArray; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
protected $units = 'MB'; |
21
|
|
|
|
22
|
|
|
/** @var int */ |
23
|
|
|
protected $decimals = 2; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $units |
27
|
|
|
*/ |
28
|
2 |
|
public function setUnits(string $units): void |
29
|
|
|
{ |
30
|
2 |
|
$this->units = $this->refineUnits($units); |
31
|
1 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $units |
35
|
|
|
*/ |
36
|
3 |
|
protected function assertUnits(string $units): void |
37
|
|
|
{ |
38
|
3 |
|
if (null === static::$unitsArray) { |
|
|
|
|
39
|
1 |
|
static::$unitsArray = array_keys(BYTES_UNITS); |
40
|
|
|
} |
41
|
3 |
|
if (false === in_array(strtoupper($units), static::$unitsArray, true)) { |
42
|
1 |
|
throw new \RuntimeException( |
43
|
1 |
|
'Unsupported units: "' . $units . '"' |
44
|
|
|
); |
45
|
|
|
} |
46
|
2 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $decimals |
50
|
|
|
*/ |
51
|
1 |
|
public function setDecimals(int $decimals): void |
52
|
|
|
{ |
53
|
1 |
|
$this->decimals = $this->refineDecimals($decimals); |
54
|
1 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param null|int $decimals |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
2 |
|
private function refineDecimals(?int $decimals): int |
61
|
|
|
{ |
62
|
2 |
|
return (int)bounds($decimals ?? $this->decimals, 0, self::MAX_DECIMALS); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
3 |
|
public function process(MemoryUsageReport $report): string |
69
|
|
|
{ |
70
|
|
|
return |
71
|
3 |
|
sprintf( |
72
|
3 |
|
self::STRING_FORMAT, |
73
|
3 |
|
Pretty::bytes($report->getUsage(), $this->units, $this->decimals), |
74
|
3 |
|
Pretty::bytes($report->getPeakUsage(), $this->units, $this->decimals), |
75
|
3 |
|
Pretty::bytes($report->getUsageReal(), $this->units, $this->decimals), |
76
|
3 |
|
Pretty::bytes($report->getPeakUsageReal(), $this->units, $this->decimals) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
1 |
|
public function getUsageString( |
84
|
|
|
MemoryUsageReport $report, |
85
|
|
|
?string $units = null, |
86
|
|
|
?int $decimals = null |
87
|
|
|
): string { |
88
|
|
|
return |
89
|
1 |
|
Pretty::bytes($report->getUsage(), $this->refineUnits($units), $this->refineDecimals($decimals)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param null|string $units |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
3 |
|
private function refineUnits(?string $units): string |
97
|
|
|
{ |
98
|
3 |
|
$units = $units ?? $this->units; |
99
|
3 |
|
$this->assertUnits($units); |
100
|
2 |
|
return $units; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
|
107
|
1 |
|
public function getPeakUsageString( |
108
|
|
|
MemoryUsageReport $report, |
109
|
|
|
?string $units = null, |
110
|
|
|
?int $decimals = null |
111
|
|
|
): string { |
112
|
|
|
return |
113
|
1 |
|
Pretty::bytes($report->getPeakUsage(), $this->refineUnits($units), $this->refineDecimals($decimals)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* {@inheritdoc} |
118
|
|
|
*/ |
119
|
1 |
|
public function getUsageRealString( |
120
|
|
|
MemoryUsageReport $report, |
121
|
|
|
?string $units = null, |
122
|
|
|
?int $decimals = null |
123
|
|
|
): string { |
124
|
|
|
return |
125
|
1 |
|
Pretty::bytes($report->getUsageReal(), $this->refineUnits($units), $this->refineDecimals($decimals)); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* {@inheritdoc} |
130
|
|
|
*/ |
131
|
1 |
|
public function getPeakUsageRealString( |
132
|
|
|
MemoryUsageReport $report, |
133
|
|
|
?string $units = null, |
134
|
|
|
?int $decimals = null |
135
|
|
|
): string { |
136
|
|
|
return |
137
|
1 |
|
Pretty::bytes( |
138
|
1 |
|
$report->getPeakUsageReal(), |
139
|
1 |
|
$this->refineUnits($units), |
140
|
1 |
|
$this->refineDecimals($decimals) |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|