|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Logger\RfcLogLevel; |
|
6
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
|
7
|
|
|
use MongoDB\BSON\Unserializable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class EventTemplate models an event template. |
|
11
|
|
|
* |
|
12
|
|
|
* Templates are the invariant part of events. |
|
13
|
|
|
*/ |
|
14
|
|
|
class EventTemplate implements Unserializable { |
|
|
|
|
|
|
15
|
|
|
use StringTranslationTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The event identifier. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
public $_id; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The message "type": a Drupal logger "channel". |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
public $type; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The event template message with placeholders, not substituted. |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
public $message; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The RFC 5424 severity level. |
|
40
|
|
|
* |
|
41
|
|
|
* @var int |
|
42
|
|
|
*/ |
|
43
|
|
|
public $severity; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* EventTemplate constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param array $data |
|
49
|
|
|
* The raw properties. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(array $data) { |
|
52
|
|
|
$this->bsonUnserialize($data); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* List the template keys and their behaviours. |
|
57
|
|
|
* |
|
58
|
|
|
* @return array |
|
|
|
|
|
|
59
|
|
|
* A properties by key array. |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function keys() { |
|
62
|
|
|
$ret = [ |
|
63
|
|
|
'_id' => [ |
|
64
|
|
|
'label' => t('ID'), |
|
65
|
|
|
], |
|
66
|
|
|
'type' => [ |
|
67
|
|
|
'label' => t('Type'), |
|
68
|
|
|
], |
|
69
|
|
|
'message' => [ |
|
70
|
|
|
'label' => t('Message'), |
|
71
|
|
|
], |
|
72
|
|
|
'severity' => [ |
|
73
|
|
|
'label' => t('Severity'), |
|
74
|
|
|
'creation_callback' => 'intval', |
|
75
|
|
|
'display_callback' => function ($level) { |
|
76
|
|
|
return RfcLogLevel::getLevels()[$level]; |
|
77
|
|
|
}, |
|
78
|
|
|
], |
|
79
|
|
|
]; |
|
80
|
|
|
return $ret; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
|
|
public function bsonUnserialize(array $data) { |
|
87
|
|
|
foreach (static::keys() as $key => $info) { |
|
88
|
|
|
$this->{$key} = isset($info['creation_callback']) |
|
89
|
|
|
? $info['creation_callback']($data[$key]) |
|
90
|
|
|
: $data[$key]; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns the message with its variables substituted into it. |
|
96
|
|
|
* |
|
97
|
|
|
* This code passes a variable to $this->t() because the "variable" ultimately |
|
98
|
|
|
* comes from a module code, not from user input. This assumes modules |
|
99
|
|
|
* correctly pass only template messages to PSR-3 methods, instead of already |
|
100
|
|
|
* assembled messages. |
|
101
|
|
|
* |
|
102
|
|
|
* XXX babysit broken modules using messages containing user input. |
|
103
|
|
|
* |
|
104
|
|
|
* @param array $variables |
|
105
|
|
|
* The event variables. |
|
106
|
|
|
* |
|
107
|
|
|
* @return \Drupal\Core\StringTranslation\TranslatableMarkup |
|
108
|
|
|
* The template message with its variables substituted. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function asString(array $variables) { |
|
111
|
|
|
return $this->t($this->message, $variables); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
|
|
|
|
|
115
|
|
|
|
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.