-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathObjectCountCollector.php
More file actions
121 lines (101 loc) · 3.12 KB
/
ObjectCountCollector.php
File metadata and controls
121 lines (101 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
declare(strict_types=1);
namespace DebugBar\DataCollector;
/**
* Collector for hit counts.
*/
class ObjectCountCollector extends DataCollector implements DataCollectorInterface, Renderable, Resettable
{
private string $name;
private string $icon;
protected int $classCount = 0;
/** @var array<string, array<string, int>> */
protected array $classList = [];
/** @var array<string, int> */
protected array $classSummary = [];
protected bool $collectSummary = false;
/** @var array<string, string> */
protected array $keyMap = ['value' => 'Count'];
public function __construct(string $name = 'counter', string $icon = 'box')
{
$this->name = $name;
$this->icon = $icon;
}
public function reset(): void
{
$this->classList = [];
$this->classCount = 0;
}
/**
* Allows to define an array to map internal keys to human-readable labels
*/
public function setKeyMap(array $keyMap): void
{
$this->keyMap = $keyMap;
}
/**
* Allows to add a summary row
*/
public function collectCountSummary(bool $enable = true): void
{
$this->collectSummary = $enable;
}
public function countClass(string|object $class, int $count = 1, string $key = 'value'): void
{
if (is_object($class)) {
$class = get_class($class);
}
if (!isset($this->classList[$class])) {
$this->classList[$class] = [];
}
if ($this->collectSummary) {
$this->classSummary[$key] = ($this->classSummary[$key] ?? 0) + $count;
}
$this->classList[$class][$key] = ($this->classList[$class][$key] ?? 0) + $count;
$this->classCount += $count;
}
public function collect(): array
{
uasort($this->classList, fn($a, $b) => array_sum($b) <=> array_sum($a));
$collect = [
'data' => $this->classList,
'count' => $this->classCount,
'key_map' => $this->keyMap,
'is_counter' => true,
];
if ($this->collectSummary) {
$collect['badges'] = $this->classSummary;
}
if (! $this->getXdebugLinkTemplate()) {
return $collect;
}
foreach ($this->classList as $class => $count) {
$reflector = class_exists($class) ? new \ReflectionClass($class) : null;
$file = $reflector?->getFileName();
if ($file && $link = $this->getXdebugLink($file)) {
$collect['data'][$class]['xdebug_link'] = $link;
}
}
return $collect;
}
public function getName(): string
{
return $this->name;
}
public function getWidgets(): array
{
$name = $this->getName();
return [
"$name" => [
'icon' => $this->icon,
'widget' => 'PhpDebugBar.Widgets.TableVariableListWidget',
'map' => "$name",
'default' => '{}',
],
"$name:badge" => [
'map' => "$name.count",
'default' => 0,
],
];
}
}