1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Controller; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Unicode; |
6
|
|
|
use Drupal\Core\Datetime\DateFormatterInterface; |
7
|
|
|
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; |
8
|
|
|
use Drupal\Core\Logger\RfcLogLevel; |
9
|
|
|
use Drupal\mongodb_watchdog\Logger; |
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Implements the controller for the request events page. |
14
|
|
|
*/ |
15
|
|
|
class RequestController implements ContainerInjectionInterface { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The core date.formatter service. |
19
|
|
|
* |
20
|
|
|
* @var \Drupal\Core\Datetime\DateFormatterInterface |
21
|
|
|
*/ |
22
|
|
|
protected $dateFormatter; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The length of the absolute path to the site root, in runes. |
26
|
|
|
* |
27
|
|
|
* @var int |
28
|
|
|
*/ |
29
|
|
|
protected $rootLength; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The mongodb_watchdog logger, to access events. |
33
|
|
|
* |
34
|
|
|
* @var \Drupal\mongodb_watchdog\Logger |
35
|
|
|
*/ |
36
|
|
|
protected $watchdog; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* RequestController constructor. |
40
|
|
|
* |
41
|
|
|
* @param \Drupal\mongodb_watchdog\Logger $watchdog |
42
|
|
|
* The MongoDB logger service, to load event data. |
43
|
|
|
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
44
|
|
|
* The core date.formatter service. |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Logger $watchdog, DateFormatterInterface $date_formatter) { |
47
|
|
|
$this->dateFormatter = $date_formatter; |
48
|
|
|
$this->watchdog = $watchdog; |
49
|
|
|
|
50
|
|
|
// Add terminal "/". |
51
|
|
|
$this->rootLength = Unicode::strlen(DRUPAL_ROOT) + 1; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Build the top part of the page, about the request. |
56
|
|
|
* |
57
|
|
|
* @param string $unique_id |
58
|
|
|
* The unique request id. |
59
|
|
|
* @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
60
|
|
|
* A fully loaded array of events and their templates. |
61
|
|
|
* |
62
|
|
|
* @return array |
|
|
|
|
63
|
|
|
* A render array for a table. |
64
|
|
|
*/ |
65
|
|
|
public function buildTrackRequest($unique_id, array $events) { |
66
|
|
|
if ($events) { |
|
|
|
|
67
|
|
|
/** @var \Drupal\mongodb_watchdog\Event $first */ |
68
|
|
|
$first = $events[0][1]; |
69
|
|
|
$location = $first->location; |
70
|
|
|
$timestamp = $this->dateFormatter->format($first->timestamp, 'long'); |
71
|
|
|
} |
72
|
|
|
else { |
73
|
|
|
$location = $timestamp = t('No information'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$rows = [ |
77
|
|
|
[t('Request ID'), $unique_id], |
78
|
|
|
[t('Location'), $location], |
79
|
|
|
[t('Date/time'), $timestamp], |
80
|
|
|
]; |
81
|
|
|
|
82
|
|
|
foreach ($rows as &$row) { |
83
|
|
|
$row[0] = [ |
84
|
|
|
'data' => $row[0], |
85
|
|
|
'header' => TRUE, |
86
|
|
|
]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$ret = [ |
90
|
|
|
'request' => [ |
91
|
|
|
'#type' => 'table', |
92
|
|
|
'#rows' => $rows, |
93
|
|
|
], |
94
|
|
|
]; |
95
|
|
|
return $ret; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Build the bottom part of the page, about the events during the request. |
100
|
|
|
* |
101
|
|
|
* @param array<\Drupal\mongodb_watchdog\EventTemplate\Drupal\mongodb_watchdog\Event[]> $events |
102
|
|
|
* A fully loaded array of events and their templates. |
103
|
|
|
* |
104
|
|
|
* @return array |
|
|
|
|
105
|
|
|
* A render array for a table. |
106
|
|
|
*/ |
107
|
|
|
public function buildTrackRows(array $events) { |
108
|
|
|
$header = [ |
109
|
|
|
t('Sequence'), |
110
|
|
|
t('Type'), |
111
|
|
|
t('Severity'), |
112
|
|
|
t('Event'), |
113
|
|
|
t('File'), |
114
|
|
|
t('Line'), |
115
|
|
|
]; |
116
|
|
|
$rows = []; |
117
|
|
|
$levels = RfcLogLevel::getLevels(); |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var \Drupal\mongodb_watchdog\EventTemplate $template |
121
|
|
|
* @var \Drupal\mongodb_watchdog\Event $event |
122
|
|
|
*/ |
123
|
|
|
foreach ($events as list($template, $event)) { |
|
|
|
|
124
|
|
|
$row = [ |
125
|
|
|
['data' => $event->requestTracking_sequence], |
|
|
|
|
126
|
|
|
$template->type, |
|
|
|
|
127
|
|
|
$levels[$template->severity], |
|
|
|
|
128
|
|
|
['data' => $template->asString($event->variables)], |
|
|
|
|
129
|
|
|
$this->simplifyPath($event->variables['%file']), |
|
|
|
|
130
|
|
|
$event->variables['%line'], |
|
|
|
|
131
|
|
|
]; |
132
|
|
|
$rows[] = $row; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$ret = [ |
136
|
|
|
'events' => [ |
137
|
|
|
'#type' => 'table', |
138
|
|
|
'#rows' => $rows, |
139
|
|
|
'#header' => $header, |
140
|
|
|
], |
141
|
|
|
]; |
142
|
|
|
return $ret; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
|
|
public static function create(ContainerInterface $container) { |
149
|
|
|
/** @var \Drupal\mongodb_watchdog\Logger $watchdog */ |
150
|
|
|
$watchdog = $container->get('mongodb.logger'); |
151
|
|
|
|
152
|
|
|
/** @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */ |
153
|
|
|
$date_formatter = $container->get('date.formatter'); |
154
|
|
|
|
155
|
|
|
return new static($watchdog, $date_formatter); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Convert an absolute path to a relative one if below the site root. |
160
|
|
|
* |
161
|
|
|
* @param string $path |
162
|
|
|
* An absolute path on the filesystem. |
163
|
|
|
* |
164
|
|
|
* @return string |
165
|
|
|
* A relative path if possible, otherwise the input path. |
166
|
|
|
*/ |
167
|
|
|
public function simplifyPath($path) { |
168
|
|
|
$ret = (Unicode::strpos($path, DRUPAL_ROOT) === 0) |
169
|
|
|
? Unicode::substr($path, $this->rootLength) |
170
|
|
|
: $path; |
171
|
|
|
return $ret; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Controller. |
176
|
|
|
* |
177
|
|
|
* @param string $unique_id |
178
|
|
|
* The unique request id from mod_unique_id. |
179
|
|
|
* |
180
|
|
|
* @return array |
|
|
|
|
181
|
|
|
* A render array. |
182
|
|
|
*/ |
183
|
|
|
public function track($unique_id) { |
184
|
|
|
$events = $this->watchdog->requestEvents($unique_id); |
185
|
|
|
$ret = $this->buildTrackRequest($unique_id, $events) |
186
|
|
|
+ $this->buildTrackRows($events); |
|
|
|
|
187
|
|
|
return $ret; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
} |
|
|
|
|
191
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.