1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Tools; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Counters\Core\AbstractCounter; |
6
|
|
|
use AlecRabbit\Counters\ExtendedCounter; |
7
|
|
|
use AlecRabbit\Counters\SimpleCounter; |
8
|
|
|
use AlecRabbit\Reports\Core\AbstractReportable; |
9
|
|
|
use AlecRabbit\Timers\Core\AbstractTimer; |
10
|
|
|
use AlecRabbit\Timers\Timer; |
11
|
|
|
use AlecRabbit\Tools\Contracts\ProfilerInterface; |
12
|
|
|
use AlecRabbit\Tools\Contracts\Strings; |
13
|
|
|
use AlecRabbit\Tools\Formatters\ProfilerReportFormatter; |
14
|
|
|
use AlecRabbit\Tools\Reports\ProfilerReport; |
15
|
|
|
use AlecRabbit\Traits\DefaultableName; |
16
|
|
|
use const AlecRabbit\Traits\Constants\DEFAULT_NAME; |
17
|
|
|
|
18
|
|
|
class Profiler extends AbstractReportable implements ProfilerInterface, Strings |
19
|
|
|
{ |
20
|
|
|
use DefaultableName; |
21
|
|
|
|
22
|
|
|
/** @var Timer[] */ |
23
|
|
|
private $timers = []; |
24
|
|
|
|
25
|
|
|
/** @var AbstractCounter[] */ |
26
|
|
|
private $counters = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Profiler constructor. |
30
|
|
|
* @throws \Exception |
31
|
|
|
*/ |
32
|
6 |
|
public function __construct() |
33
|
|
|
{ |
34
|
6 |
|
parent::__construct(); |
35
|
6 |
|
$this->setBindings( |
36
|
6 |
|
ProfilerReport::class, |
37
|
6 |
|
ProfilerReportFormatter::class |
38
|
|
|
); |
39
|
6 |
|
$this->counter(); // Create default counter |
40
|
6 |
|
$this->timer(); // Create default timer |
41
|
6 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param null|string $name |
45
|
|
|
* @param string ...$suffixes |
46
|
|
|
* @return AbstractCounter |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
6 |
|
public function counter(?string $name = null, string ...$suffixes): AbstractCounter |
50
|
|
|
{ |
51
|
6 |
|
$name = $this->prepareName($name, $suffixes); |
52
|
|
|
return |
53
|
6 |
|
$this->counters[$name] ?? $this->counters[$name] = $this->makeCounter($name); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param null|string $name |
58
|
|
|
* @param array $suffixes |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
6 |
|
protected function prepareName(?string $name, array $suffixes): string |
62
|
|
|
{ |
63
|
6 |
|
$name = $this->defaultName($name); |
64
|
6 |
|
if (!empty($suffixes)) { |
65
|
2 |
|
return $this->formatName($name, $suffixes); |
66
|
|
|
} |
67
|
6 |
|
return $name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $name |
72
|
|
|
* @param array $suffixes |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
2 |
|
protected function formatName(string $name, array $suffixes): string |
76
|
|
|
{ |
77
|
|
|
return |
78
|
2 |
|
sprintf(static::NAME_FORMAT, $name, implode(', ', $suffixes)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param string $name |
83
|
|
|
* @return AbstractCounter |
84
|
|
|
* @throws \Exception |
85
|
|
|
*/ |
86
|
6 |
|
protected function makeCounter(string $name): AbstractCounter |
87
|
|
|
{ |
88
|
6 |
|
if (DEFAULT_NAME === $name) { |
89
|
6 |
|
return new SimpleCounter($name); |
90
|
|
|
} |
91
|
4 |
|
return new ExtendedCounter($name); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param null|string $name |
96
|
|
|
* @param string ...$suffixes |
97
|
|
|
* @return Timer |
98
|
|
|
* @throws \Exception |
99
|
|
|
*/ |
100
|
6 |
|
public function timer(?string $name = null, string ...$suffixes): Timer |
101
|
|
|
{ |
102
|
6 |
|
$name = $this->prepareName($name, $suffixes); |
103
|
|
|
return |
104
|
6 |
|
$this->timers[$name] ?? $this->timers[$name] = new Timer($name); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return AbstractTimer[] |
109
|
|
|
*/ |
110
|
4 |
|
public function getTimers(): array |
111
|
|
|
{ |
112
|
4 |
|
return $this->timers; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return AbstractCounter[] |
117
|
|
|
*/ |
118
|
4 |
|
public function getCounters(): array |
119
|
|
|
{ |
120
|
4 |
|
return $this->counters; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|