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
|
|
|
public static function get(bool $real = false, ?string $unit = null, ?int $decimals = null): string |
22
|
|
|
{ |
23
|
|
|
return |
24
|
|
|
Pretty::bytes(memory_get_usage($real), $unit, $decimals); |
25
|
|
|
} |
26
|
|
|
|
27
|
2 |
|
/** |
28
|
|
|
* @param bool $real |
29
|
|
|
* @param null|string $unit |
30
|
2 |
|
* @param int|null $decimals |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
|
|
public static function getPeak(bool $real = false, ?string $unit = null, ?int $decimals = null): string |
34
|
|
|
{ |
35
|
|
|
return |
36
|
|
|
Pretty::bytes(memory_get_peak_usage($real), $unit, $decimals); |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
/** |
40
|
|
|
* @return MemoryUsageReport |
41
|
|
|
*/ |
42
|
2 |
|
public static function reportStatic(): MemoryUsageReport |
43
|
|
|
{ |
44
|
|
|
return |
45
|
|
|
new MemoryUsageReport(); |
46
|
|
|
} |
47
|
|
|
|
48
|
4 |
|
/** |
49
|
|
|
* @return MemoryUsageReportFormatter |
50
|
|
|
*/ |
51
|
4 |
|
public static function getFormatter(): MemoryUsageReportFormatter |
52
|
4 |
|
{ |
53
|
4 |
|
if (null === static::$formatter) { |
54
|
4 |
|
static::$formatter = new MemoryUsageReportFormatter(); |
55
|
4 |
|
} |
56
|
|
|
return static::$formatter; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param null|MemoryUsageReportFormatter $formatter |
61
|
|
|
*/ |
62
|
7 |
|
public static function setFormatter(?MemoryUsageReportFormatter $formatter): void |
63
|
|
|
{ |
64
|
7 |
|
self::$formatter = $formatter; |
65
|
1 |
|
} |
66
|
|
|
|
67
|
7 |
|
protected function createEmptyReport(): ReportInterface |
68
|
|
|
{ |
69
|
|
|
return |
|
|
|
|
70
|
|
|
static::reportStatic(); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|