1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Controller; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\Config\ImmutableConfig; |
6
|
|
|
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
7
|
|
|
use Drupal\Core\Logger\RfcLogLevel; |
8
|
|
|
use Drupal\Core\Template\Attribute; |
9
|
|
|
use Drupal\mongodb_watchdog\EventController; |
10
|
|
|
use Drupal\mongodb_watchdog\EventTemplate; |
11
|
|
|
use Drupal\mongodb_watchdog\Logger; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class DetailController implements the controller for the event detail page. |
18
|
|
|
*/ |
19
|
|
|
class DetailController implements ContainerInjectionInterface { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The mongodb.watchdog_event_controller service. |
23
|
|
|
* |
24
|
|
|
* @var \Drupal\mongodb_watchdog\EventController |
25
|
|
|
*/ |
26
|
|
|
protected $eventController; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The items_per_page configuration value. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
protected $itemsPerPage; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The logger.mongodb_watchdog logger channel, to log events. |
37
|
|
|
* |
38
|
|
|
* @var \Psr\Log\LoggerInterface |
39
|
|
|
*/ |
40
|
|
|
protected $logger; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The MongoDB logger service, to load events. |
44
|
|
|
* |
45
|
|
|
* @var \Drupal\mongodb_watchdog\Logger |
46
|
|
|
*/ |
47
|
|
|
protected $watchdog; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constructor. |
51
|
|
|
* |
52
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
53
|
|
|
* The logger service, to log intervening events. |
54
|
|
|
* @param \Drupal\mongodb_watchdog\EventController $event_controller |
55
|
|
|
* The event controller service. |
56
|
|
|
* @param \Drupal\Core\Config\ImmutableConfig $config |
57
|
|
|
* The module configuration. |
58
|
|
|
*/ |
59
|
|
|
public function __construct(Logger $watchdog, LoggerInterface $logger, EventController $event_controller, ImmutableConfig $config) { |
60
|
|
|
$this->config = $config; |
61
|
|
|
$this->eventController = $event_controller; |
62
|
|
|
$this->logger = $logger; |
63
|
|
|
$this->watchdog = $watchdog; |
64
|
|
|
|
65
|
|
|
$this->itemsPerPage = $config->get('items_per_page'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritdoc} |
70
|
|
|
*/ |
71
|
|
|
public static function create(ContainerInterface $container) { |
72
|
|
|
/** @var \Psr\Log\LoggerInterface $logger */ |
73
|
|
|
$logger = $container->get('logger.channel.mongodb_watchdog'); |
74
|
|
|
|
75
|
|
|
/** @var \Drupal\mongodb_watchdog\EventController $eventController */ |
76
|
|
|
$eventController = $container->get('mongodb.watchdog_event_controller'); |
|
|
|
|
77
|
|
|
|
78
|
|
|
/** @var \Drupal\mongodb_watchdog\Logger $watchdog */ |
79
|
|
|
$watchdog = $container->get('mongodb.logger'); |
80
|
|
|
|
81
|
|
|
/** @var array $config */ |
82
|
|
|
$config = $container->get('config.factory')->get('mongodb_watchdog.settings'); |
83
|
|
|
|
84
|
|
|
return new static($watchdog, $logger, $eventController, $config); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Controller for mongodb_watchdog.detail. |
89
|
|
|
* |
90
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
91
|
|
|
* The event template. |
92
|
|
|
* |
93
|
|
|
* @return array |
|
|
|
|
94
|
|
|
* A render array. |
95
|
|
|
*/ |
96
|
|
|
public function detail(EventTemplate $event_template, Request $request) { |
97
|
|
|
$page = $this->setupPager($event_template, $request); |
98
|
|
|
$template_rows = $this->detailHeader($event_template); |
99
|
|
|
$event_rows = $this->detailRows($event_template, $page); |
100
|
|
|
|
101
|
|
|
$base = [ |
102
|
|
|
'#attributes' => new Attribute(['class' => 'mongodb_watchdog-detail']), |
103
|
|
|
'#type' => 'table', |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
$event_header = [ |
107
|
|
|
t('Date'), |
108
|
|
|
t('User'), |
109
|
|
|
t('Message'), |
110
|
|
|
t('Location'), |
111
|
|
|
t('Referrer'), |
112
|
|
|
t('Hostname'), |
113
|
|
|
t('Operations'), |
114
|
|
|
]; |
115
|
|
|
|
116
|
|
|
$ret = [ |
117
|
|
|
"#attached" => [ |
|
|
|
|
118
|
|
|
'library' => 'mongodb_watchdog/styling', |
119
|
|
|
], |
120
|
|
|
'template' => $base + [ |
121
|
|
|
'#caption' => t('Event template'), |
122
|
|
|
'#rows' => $template_rows, |
123
|
|
|
], |
124
|
|
|
'events' => $base + [ |
125
|
|
|
'#caption' => t('Event occurrences'), |
126
|
|
|
'#header' => $event_header, |
127
|
|
|
'#rows' => $event_rows, |
128
|
|
|
], |
129
|
|
|
'pager' => [ |
130
|
|
|
'#type' => 'pager', |
131
|
|
|
], |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
return $ret; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Build the heading rows on the event occurrences page. |
139
|
|
|
* |
140
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
141
|
|
|
* The event template. |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
* A table render array. |
145
|
|
|
*/ |
146
|
|
|
protected function detailHeader(EventTemplate $template) { |
147
|
|
|
$rows = []; |
148
|
|
|
foreach (EventTemplate::keys() as $key => $info) { |
149
|
|
|
$value = $template->{$key}; |
150
|
|
|
$row = [ |
151
|
|
|
[ |
152
|
|
|
'header' => TRUE, |
153
|
|
|
'data' => $info['label'], |
154
|
|
|
], |
155
|
|
|
isset($info['display_callback']) ? $info['display_callback']($value) : $value, |
156
|
|
|
]; |
157
|
|
|
$rows[] = $row; |
158
|
|
|
} |
159
|
|
|
return $rows; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Build the occurrence rows on the event occurrences page. |
164
|
|
|
* |
165
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
166
|
|
|
* The event template. |
167
|
|
|
* @param int $page |
168
|
|
|
* The page number, starting at 0. |
169
|
|
|
* |
170
|
|
|
* @return array |
171
|
|
|
* A table render array. |
172
|
|
|
*/ |
173
|
|
|
protected function detailRows(EventTemplate $template, $page) { |
174
|
|
|
$rows = []; |
175
|
|
|
$skip = $page * $this->itemsPerPage; |
176
|
|
|
$limit = $this->itemsPerPage; |
177
|
|
|
$events = $this->eventController->find($template, $skip, $limit); |
178
|
|
|
|
179
|
|
|
/** @var \Drupal\mongodb_watchdog\Event $event */ |
180
|
|
|
foreach ($events as $event) { |
181
|
|
|
$rows[] = $this->eventController->asTableRow($template, $event); |
182
|
|
|
} |
183
|
|
|
return $rows; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Title callback for mongodb_watchdog.detail. |
188
|
|
|
* |
189
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $event_template |
190
|
|
|
* The event template for which the title is built. |
191
|
|
|
* |
192
|
|
|
* @return \Drupal\Core\StringTranslation\TranslatableMarkup |
193
|
|
|
* The page title. |
194
|
|
|
*/ |
195
|
|
|
public function detailTitle(EventTemplate $event_template) { |
196
|
|
|
return t('MongoDB events: "@template"', ['@template' => $event_template->message]); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Set up the events pager. |
201
|
|
|
* |
202
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
203
|
|
|
* The current request. |
204
|
|
|
* |
205
|
|
|
* @return int |
206
|
|
|
* The number of the page to display, starting at 0. |
207
|
|
|
*/ |
208
|
|
View Code Duplication |
public function setupPager(EventTemplate $template, Request $request) { |
|
|
|
|
209
|
|
|
$count = $this->watchdog->eventCount($template); |
210
|
|
|
$height = $this->itemsPerPage; |
211
|
|
|
pager_default_initialize($count, $height); |
212
|
|
|
|
213
|
|
|
$page = intval($request->query->get('page')); |
214
|
|
|
if ($page < 0) { |
215
|
|
|
$page = 0; |
216
|
|
|
} |
217
|
|
|
else { |
218
|
|
|
$page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1); |
219
|
|
|
if ($page > $page_max) { |
220
|
|
|
$page = $page_max; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
return $page; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
} |
|
|
|
|
228
|
|
|
|