1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Profiler\Target; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Profiler\Message; |
8
|
|
|
use Yiisoft\Strings\WildcardPattern; |
9
|
|
|
|
10
|
|
|
use function count; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Target is the base class for all profiling target classes. |
14
|
|
|
* |
15
|
|
|
* A profile target object will filter the messages stored by {@see Profiler} according |
16
|
|
|
* to its {@see AbstractTarget::include()} and {@see AbstractTarget::exclude()}. |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractTarget implements TargetInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string[] List of message categories that this target is interested in. Defaults to empty, meaning all |
22
|
|
|
* categories. |
23
|
|
|
* |
24
|
|
|
* You can use an asterisk at the end of a category so that the category may be used to |
25
|
|
|
* match those categories sharing the same common prefix. For example, 'Yiisoft\Db\**' will match |
26
|
|
|
* categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. |
27
|
|
|
* |
28
|
|
|
* @see WildcardPattern |
29
|
|
|
*/ |
30
|
|
|
private array $include = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string[] List of message categories that this target is NOT interested in. Defaults to empty, meaning no |
34
|
|
|
* uninteresting messages. |
35
|
|
|
* |
36
|
|
|
* If this property is not empty, then any category listed here will be excluded from {@see include()}. |
37
|
|
|
* You can use an asterisk at the end of a category so that the category can be used to |
38
|
|
|
* match those categories sharing the same common prefix. For example, 'Yiisoft\Db\**' will match |
39
|
|
|
* categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. |
40
|
|
|
* |
41
|
|
|
* @see WildcardPattern |
42
|
|
|
*/ |
43
|
|
|
private array $exclude = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var bool Whether to enable this log target. Defaults to true. |
47
|
|
|
*/ |
48
|
|
|
private bool $enabled = true; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
* |
53
|
|
|
* This method will filter the given messages with {@see include()} and {@see exclude()}. |
54
|
|
|
* And if requested, it will also export the filtering result to specific medium (e.g. email). |
55
|
|
|
*/ |
56
|
5 |
|
public function collect(array $messages): void |
57
|
|
|
{ |
58
|
5 |
|
if (!$this->enabled) { |
59
|
1 |
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
4 |
|
$messages = $this->filterMessages($messages); |
63
|
|
|
|
64
|
4 |
|
if (count($messages) > 0) { |
65
|
3 |
|
$this->export($messages); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string[] $include List of message categories that this target is interested in. Defaults to empty, meaning all |
71
|
|
|
* categories. |
72
|
|
|
* |
73
|
|
|
* You can use an asterisk at the end of a category so that the category may be used to |
74
|
|
|
* match those categories sharing the same common prefix. For example, 'Yiisoft\Db\**' will match |
75
|
|
|
* categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. |
76
|
|
|
* |
77
|
|
|
* @see WildcardPattern |
78
|
|
|
* |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
6 |
|
public function include(array $include): self |
82
|
|
|
{ |
83
|
6 |
|
$new = clone $this; |
84
|
6 |
|
$new->include = $include; |
85
|
6 |
|
return $new; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string[] $exclude List of message categories that this target is NOT interested in. Defaults to empty, meaning no |
90
|
|
|
* uninteresting messages. |
91
|
|
|
* |
92
|
|
|
* If this property is not empty, then any category listed here will be excluded from {@see include()}. |
93
|
|
|
* You can use an asterisk at the end of a category so that the category can be used to |
94
|
|
|
* match those categories sharing the same common prefix. For example, 'Yiisoft\Db\**' will match |
95
|
|
|
* categories starting with 'Yiisoft\Db\', such as `Yiisoft\Db\Connection`. |
96
|
|
|
* |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
7 |
|
public function exclude(array $exclude): self |
100
|
|
|
{ |
101
|
7 |
|
$new = clone $this; |
102
|
7 |
|
$new->exclude = $exclude; |
103
|
7 |
|
return $new; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Enable or disable target. |
108
|
|
|
*/ |
109
|
3 |
|
public function enable(bool $value = true): void |
110
|
|
|
{ |
111
|
3 |
|
$this->enabled = $value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns target is enabled. |
116
|
|
|
*/ |
117
|
2 |
|
public function isEnabled(): bool |
118
|
|
|
{ |
119
|
2 |
|
return $this->enabled; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Exports profiling messages to a specific destination. |
124
|
|
|
* |
125
|
|
|
* Child classes must implement this method. |
126
|
|
|
* |
127
|
|
|
* @param Message[] $messages Profiling messages to be exported. |
128
|
|
|
*/ |
129
|
|
|
abstract public function export(array $messages): void; |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Filters the given messages according to their categories. |
133
|
|
|
* |
134
|
|
|
* @param Message[] $messages Messages to be filtered. |
135
|
|
|
* The message structure follows that in {@see TargetInterface::collect()}. |
136
|
|
|
* |
137
|
|
|
* @return Message[] The filtered messages. |
138
|
|
|
*/ |
139
|
9 |
|
protected function filterMessages(array $messages): array |
140
|
|
|
{ |
141
|
9 |
|
foreach ($messages as $i => $message) { |
142
|
9 |
|
if (!$this->isCategoryMatched($message->level())) { |
143
|
3 |
|
unset($messages[$i]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
9 |
|
return $messages; |
147
|
|
|
} |
148
|
|
|
|
149
|
9 |
|
private function isCategoryMatched(string $category): bool |
150
|
|
|
{ |
151
|
9 |
|
$matched = empty($this->include); |
152
|
|
|
|
153
|
9 |
|
foreach ($this->include as $pattern) { |
154
|
2 |
|
if ((new WildcardPattern($pattern))->match($category)) { |
155
|
1 |
|
$matched = true; |
156
|
1 |
|
break; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
9 |
|
if ($matched) { |
161
|
8 |
|
foreach ($this->exclude as $pattern) { |
162
|
3 |
|
if ((new WildcardPattern($pattern))->match($category)) { |
163
|
2 |
|
$matched = false; |
164
|
2 |
|
break; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
} |
168
|
9 |
|
return $matched; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|