-
-
Notifications
You must be signed in to change notification settings - Fork 424
Expand file tree
/
Copy pathTemplateCollector.php
More file actions
127 lines (109 loc) · 3.62 KB
/
TemplateCollector.php
File metadata and controls
127 lines (109 loc) · 3.62 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
122
123
124
125
126
127
<?php
declare(strict_types=1);
namespace DebugBar\DataCollector;
class TemplateCollector extends DataCollector implements Renderable, AssetProvider, Resettable
{
use HasTimeDataCollector;
protected string $name;
protected array $templates = [];
protected bool|string $collect_data;
/** @var array<string> */
protected array $exclude_paths;
protected int|bool $group;
/**
*
* @param string[] $excludePaths Paths to exclude from collection
*/
public function __construct(bool|string $collectData = true, array $excludePaths = [], int|bool $group = true)
{
$this->collect_data = $collectData;
$this->templates = [];
$this->exclude_paths = $excludePaths;
$this->group = $group;
}
public function reset(): void
{
$this->templates = [];
}
public function getName(): string
{
return 'templates';
}
public function getWidgets(): array
{
$name = $this->getName();
return [
$name => [
'icon' => 'file-code',
'widget' => 'PhpDebugBar.Widgets.TemplatesWidget',
'map' => $name,
'default' => '[]',
],
"$name:badge" => [
'map' => $name . '.nb_templates',
'default' => 0,
],
];
}
public function getAssets(): array
{
return [
'css' => 'widgets/templates/widget.css',
'js' => 'widgets/templates/widget.js',
];
}
public function addTemplate(string $name, array $data, ?string $type, ?string $path): void
{
// Prevent duplicates
$hash = $type . $path . $name . ($this->collect_data ? implode(array_keys($data)) : '');
if ($this->collect_data === 'keys') {
$params = array_keys($data);
} elseif ($this->collect_data) {
$params = array_map(
fn($value) => $this->getDataFormatter()->formatVar($value, false),
$data,
);
} else {
$params = [];
}
$template = [
'name' => $name,
'param_count' => $this->collect_data ? count($params) : null,
'params' => $params,
'start' => microtime(true),
'type' => $type,
'hash' => $hash,
];
if ($path && $this->getXdebugLinkTemplate()) {
$template['xdebug_link'] = $this->getXdebugLink($path);
}
if ($this->hasTimeDataCollector()) {
$this->addTimeMeasure($name, $template['start']);
}
$this->templates[] = $template;
}
public function collect(): array
{
if ($this->group === true || ($this->group !== false && count($this->templates) > $this->group)) {
$templates = [];
foreach ($this->templates as $template) {
$hash = $template['hash'];
if (!isset($templates[$hash])) {
$template['render_count'] = 0;
$template['name_original'] = $template['name'];
$templates[$hash] = $template;
}
$templates[$hash]['render_count']++;
$templates[$hash]['name'] = $templates[$hash]['render_count'] . 'x ' . $templates[$hash]['name_original'];
}
$templates = array_values($templates);
} else {
$templates = $this->templates;
}
return [
'count' => count($this->templates),
'nb_templates' => count($this->templates),
'templates' => $templates,
];
}
}