forked from laradumps/laradumps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCacheObserver.php
152 lines (123 loc) · 3.94 KB
/
CacheObserver.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
namespace LaraDumps\LaraDumps\Observers;
use Illuminate\Cache\Events\{CacheEvent, CacheHit, CacheMissed, KeyForgotten, KeyWritten};
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Str;
use LaraDumps\LaraDumpsCore\Actions\Config;
use LaraDumps\LaraDumpsCore\LaraDumps;
use LaraDumps\LaraDumpsCore\Payloads\TableV2Payload;
class CacheObserver
{
protected ?string $label = 'Cache';
protected array $hidden = [];
private bool $enabled = false;
public function register(): void
{
Event::listen(CacheHit::class, function (CacheHit $event) {
if (!$this->isEnabled()) {
return;
}
$this->sendCache($event, [
'Type' => 'hit',
'Key' => $event->key,
'Value' => $this->formatValue($event),
], 'width: 120px', 'Cache Hit');
});
Event::listen(CacheMissed::class, function (CacheMissed $event) {
if (!$this->isEnabled()) {
return;
}
$this->sendCache($event, [
'Type' => 'missed',
'Key' => $event->key,
], 'width: 120px', 'Cache Missed');
});
Event::listen(KeyForgotten::class, function (KeyForgotten $event) {
if (!$this->isEnabled()) {
return;
}
$this->sendCache($event, [
'Type' => 'forget',
'Key' => $event->key,
], 'width: 120px', 'Cache Forgot');
});
Event::listen(KeyWritten::class, function (KeyWritten $event) {
if (!$this->isEnabled()) {
return;
}
$this->sendCache($event, [
'Type' => 'set',
'Key' => $event->key,
'Value' => $this->formatValue($event),
'Expiration' => $this->formatExpiration($event),
], 'width: 120px', 'Cache Written');
});
}
protected function sendCache(CacheEvent $event, array $data, string $headerStyle = '', string $label = ''): void
{
if (!$this->isEnabled()) {
return;
}
if ($this->shouldIgnore($event)) {
return;
}
$dump = new LaraDumps();
$payload = new TableV2Payload($data, $headerStyle);
$dump->send($payload);
$dump->label($this->label ?: $label);
$dump->toScreen('Cache');
}
public function enable(string $label = ''): void
{
$this->label = $label;
$this->enabled = true;
}
public function disable(): void
{
$this->enabled = false;
}
public function isEnabled(): bool
{
if (!boolval(Config::get('observers.cache', false))) {
return $this->enabled;
}
return boolval(Config::get('observers.cache', false));
}
public function hidden(array $hidden = []): array
{
if (!empty($hidden)) {
$this->hidden = array_merge($hidden);
}
return $this->hidden ?? [];
}
private function formatValue(mixed $event): mixed
{
return (!$this->shouldHideValue($event))
? $event->value // @phpstan-ignore-line
: '********';
}
private function shouldHideValue(mixed $event): bool
{
return Str::is(
$this->hidden(),
$event->key // @phpstan-ignore-line
);
}
protected function formatExpiration(KeyWritten $event): mixed
{
return property_exists($event, 'seconds') // @phpstan-ignore-line
? $event->seconds
: $event->minutes * 60; // @phpstan-ignore-line
}
private function shouldIgnore(mixed $event): bool
{
return Str::is(
[
'illuminate:queue:restart',
'framework/schedule*',
'telescope:*',
],
$event->key // @phpstan-ignore-line
);
}
}