1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\profile; |
9
|
|
|
|
10
|
|
|
use yii\base\Component; |
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 [[Profiler]] according |
16
|
|
|
* to its [[categories]] and [[except]] properties. |
17
|
|
|
* |
18
|
|
|
* For more details and usage information on Target, see the [guide article on profiling & targets](guide:runtime-profiling). |
19
|
|
|
* |
20
|
|
|
* @author Paul Klimov <[email protected]> |
21
|
|
|
* @since 2.1 |
22
|
|
|
*/ |
23
|
|
|
abstract class Target extends Component |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var bool whether to enable this log target. Defaults to true. |
27
|
|
|
*/ |
28
|
|
|
public $enabled = true; |
29
|
|
|
/** |
30
|
|
|
* @var array list of message categories that this target is interested in. Defaults to empty, meaning all categories. |
31
|
|
|
* You can use an asterisk at the end of a category so that the category may be used to |
32
|
|
|
* match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
33
|
|
|
* categories starting with 'yii\db\', such as `yii\db\Connection`. |
34
|
|
|
*/ |
35
|
|
|
public $categories = []; |
36
|
|
|
/** |
37
|
|
|
* @var array list of message categories that this target is NOT interested in. Defaults to empty, meaning no uninteresting messages. |
38
|
|
|
* If this property is not empty, then any category listed here will be excluded from [[categories]]. |
39
|
|
|
* You can use an asterisk at the end of a category so that the category can be used to |
40
|
|
|
* match those categories sharing the same common prefix. For example, 'yii\db\*' will match |
41
|
|
|
* categories starting with 'yii\db\', such as `yii\db\Connection`. |
42
|
|
|
* @see categories |
43
|
|
|
*/ |
44
|
|
|
public $except = []; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Processes the given log messages. |
49
|
|
|
* This method will filter the given messages with [[levels]] and [[categories]]. |
50
|
|
|
* And if requested, it will also export the filtering result to specific medium (e.g. email). |
51
|
|
|
* @param array $messages profiling messages to be processed. See [[Logger::messages]] for the structure |
52
|
|
|
* of each message. |
53
|
|
|
*/ |
54
|
1 |
|
public function collect(array $messages) |
55
|
|
|
{ |
56
|
1 |
|
if (!$this->enabled) { |
57
|
1 |
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$messages = $this->filterMessages($messages); |
61
|
1 |
|
if (count($messages) > 0) { |
62
|
1 |
|
$this->export($messages); |
63
|
|
|
} |
64
|
1 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Exports profiling messages to a specific destination. |
68
|
|
|
* Child classes must implement this method. |
69
|
|
|
* @param array $messages profiling messages to be exported. |
70
|
|
|
*/ |
71
|
|
|
abstract public function export(array $messages); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Filters the given messages according to their categories. |
75
|
|
|
* @param array $messages messages to be filtered. |
76
|
|
|
* The message structure follows that in [[Logger::messages]]. |
77
|
|
|
* @return array the filtered messages. |
78
|
|
|
*/ |
79
|
6 |
|
protected function filterMessages($messages) |
80
|
|
|
{ |
81
|
6 |
|
foreach ($messages as $i => $message) { |
82
|
6 |
|
$matched = empty($this->categories); |
83
|
6 |
|
foreach ($this->categories as $category) { |
84
|
2 |
|
if ($message['category'] === $category || !empty($category) && substr_compare($category, '*', -1, 1) === 0 && strpos($message['category'], rtrim($category, '*')) === 0) { |
85
|
1 |
|
$matched = true; |
86
|
2 |
|
break; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
6 |
|
if ($matched) { |
91
|
5 |
|
foreach ($this->except as $category) { |
92
|
2 |
|
$prefix = rtrim($category, '*'); |
93
|
2 |
|
if (($message['category'] === $category || $prefix !== $category) && strpos($message['category'], $prefix) === 0) { |
94
|
1 |
|
$matched = false; |
95
|
2 |
|
break; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
6 |
|
if (!$matched) { |
101
|
6 |
|
unset($messages[$i]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
6 |
|
return $messages; |
105
|
|
|
} |
106
|
|
|
} |