|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace AlecRabbit\Accessories; |
|
4
|
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReport; |
|
6
|
|
|
use AlecRabbit\Accessories\MemoryUsage\MemoryUsageReportFormatter; |
|
7
|
|
|
use AlecRabbit\Reports\Contracts\ReportInterface; |
|
8
|
|
|
use AlecRabbit\Reports\Core\Reportable; |
|
9
|
|
|
|
|
10
|
|
|
class MemoryUsage extends Reportable |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var null|MemoryUsageReportFormatter */ |
|
13
|
|
|
protected static $formatter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param bool $real |
|
17
|
|
|
* @param null|string $unit |
|
18
|
|
|
* @param int|null $decimals |
|
19
|
|
|
* @return string |
|
20
|
|
|
*/ |
|
21
|
2 |
|
public static function get(bool $real = false, ?string $unit = null, ?int $decimals = null): string |
|
22
|
|
|
{ |
|
23
|
|
|
return |
|
24
|
2 |
|
Pretty::bytes(memory_get_usage($real), $unit, $decimals); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param bool $real |
|
29
|
|
|
* @param null|string $unit |
|
30
|
|
|
* @param int|null $decimals |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
2 |
|
public static function getPeak(bool $real = false, ?string $unit = null, ?int $decimals = null): string |
|
34
|
|
|
{ |
|
35
|
|
|
return |
|
36
|
2 |
|
Pretty::bytes(memory_get_peak_usage($real), $unit, $decimals); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return MemoryUsageReport |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public static function reportStatic(): MemoryUsageReport |
|
43
|
|
|
{ |
|
44
|
|
|
return |
|
45
|
5 |
|
new MemoryUsageReport(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return MemoryUsageReportFormatter |
|
50
|
|
|
*/ |
|
51
|
9 |
|
public static function getFormatter(): MemoryUsageReportFormatter |
|
52
|
|
|
{ |
|
53
|
9 |
|
if (null === static::$formatter) { |
|
54
|
1 |
|
static::$formatter = new MemoryUsageReportFormatter(); |
|
55
|
|
|
} |
|
56
|
9 |
|
return static::$formatter; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param null|MemoryUsageReportFormatter $formatter |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public static function setFormatter(?MemoryUsageReportFormatter $formatter): void |
|
63
|
|
|
{ |
|
64
|
2 |
|
self::$formatter = $formatter; |
|
65
|
2 |
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
protected function createEmptyReport(): ReportInterface |
|
68
|
|
|
{ |
|
69
|
|
|
return |
|
70
|
1 |
|
static::reportStatic(); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|