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