-
Notifications
You must be signed in to change notification settings - Fork 1
/
PhpWebProfiler.php
62 lines (47 loc) · 1.34 KB
/
PhpWebProfiler.php
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
<?php
declare(strict_types=1);
namespace WebProfiler;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DebugBar;
use WebProfiler\DataCollectors\DataCollectorToolbar;
final class PhpWebProfiler extends DebugBar
{
private string $prefixEndpoint = 'debug';
private int $keepMaxLogs = 30;
public function getToolbarCollectors(): array
{
return array_filter(
$this->collectors,
static fn (DataCollector $collector) => $collector instanceof DataCollectorToolbar
);
}
public function collect(): array
{
if (!$this->isTraceable()) {
return [];
}
$this->logRotate();
return parent::collect();
}
private function isTraceable(): bool
{
$requestUri = $_SERVER['REQUEST_URI'] ?? null;
if (null === $requestUri) {
return true;
}
$pattern = sprintf('/^\/%s$|^\/%s\//', $this->prefixEndpoint, $this->prefixEndpoint);
return 0 === preg_match($pattern, $requestUri);
}
private function logRotate(): void
{
$storage = $this->getStorage();
$logs = $storage->find([]);
if ($this->keepMaxLogs < count($logs)) {
$storage->clear();
}
}
public function getPrefixEndpoint(): string
{
return $this->prefixEndpoint;
}
}