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