|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Drupal\mongodb_watchdog; |
|
6
|
|
|
|
|
7
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
|
|
|
|
|
|
8
|
|
|
use Drupal\Core\Datetime\DateFormatterInterface; |
|
|
|
|
|
|
9
|
|
|
use Drupal\Core\Link; |
|
|
|
|
|
|
10
|
|
|
use Drupal\Core\StringTranslation\StringTranslationTrait; |
|
|
|
|
|
|
11
|
|
|
use Drupal\Core\Url; |
|
|
|
|
|
|
12
|
|
|
use Drupal\user\Entity\User; |
|
|
|
|
|
|
13
|
|
|
use MongoDB\Driver\Cursor; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class EventController provides query and render logic for Event occurrences. |
|
17
|
|
|
* |
|
18
|
|
|
* It is not a page/Response controller, hence its location outside the |
|
19
|
|
|
* Controller namespace. |
|
20
|
|
|
*/ |
|
21
|
|
|
class EventController { |
|
22
|
|
|
use StringTranslationTrait; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The name of the anonymous user account. |
|
26
|
|
|
* |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $anonymous; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The length of the absolute home URL. |
|
33
|
|
|
* |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $baseLength; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The date.formatter service. |
|
40
|
|
|
* |
|
41
|
|
|
* @var \Drupal\Core\Datetime\DateFormatterInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $dateFormatter; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The absolute path to the site home. |
|
47
|
|
|
* |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $front; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* An instance cache for user accounts, which are used in a loop. |
|
54
|
|
|
* |
|
55
|
|
|
* @var array |
|
56
|
|
|
*/ |
|
57
|
|
|
protected $userCache = []; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* The MongoDB logger service, to load events. |
|
61
|
|
|
* |
|
62
|
|
|
* @var \Drupal\mongodb_watchdog\Logger |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $watchdog; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* EventController constructor. |
|
68
|
|
|
* |
|
69
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config |
|
70
|
|
|
* The config.factory service. |
|
71
|
|
|
* @param \Drupal\Core\Datetime\DateFormatterInterface $dateFormatter |
|
72
|
|
|
* The core data.formatter service. |
|
73
|
|
|
* @param \Drupal\mongodb_watchdog\Logger $watchdog |
|
74
|
|
|
* The MongoDB logger service, to load events. |
|
75
|
|
|
*/ |
|
76
|
|
|
public function __construct( |
|
77
|
|
|
ConfigFactoryInterface $config, |
|
78
|
|
|
DateFormatterInterface $dateFormatter, |
|
79
|
|
|
Logger $watchdog) { |
|
80
|
|
|
// Needed for other values so build it first. |
|
81
|
|
|
$this->front = Url::fromRoute('<front>', [], ['absolute' => TRUE]) |
|
82
|
|
|
->toString(); |
|
83
|
|
|
|
|
84
|
|
|
$this->anonymous = $config->get('user.settings')->get('anonymous'); |
|
85
|
|
|
$this->baseLength = mb_strlen($this->front) - 1; |
|
86
|
|
|
$this->dateFormatter = $dateFormatter; |
|
87
|
|
|
$this->watchdog = $watchdog; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Provide a table row representation of an event occurrence. |
|
92
|
|
|
* |
|
93
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
|
94
|
|
|
* The template for which the occurrence exists. |
|
95
|
|
|
* @param \Drupal\mongodb_watchdog\Event $event |
|
96
|
|
|
* The event occurrence to represent. |
|
97
|
|
|
* |
|
98
|
|
|
* @return array |
|
99
|
|
|
* A render array. |
|
100
|
|
|
* |
|
101
|
|
|
* @throws \Drupal\Core\Entity\EntityMalformedException |
|
102
|
|
|
*/ |
|
103
|
|
|
public function asTableRow(EventTemplate $template, Event $event): array { |
|
104
|
|
|
$uid = $event->uid(); |
|
105
|
|
|
if (!isset($this->userCache[$uid])) { |
|
106
|
|
|
$this->userCache[$uid] = $uid ? User::load($uid)->toLink() : $this->anonymous; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$location = $event->location(); |
|
110
|
|
|
$ret = [ |
|
111
|
|
|
$this->dateFormatter->format($event->timestamp, 'short'), |
|
112
|
|
|
$this->userCache[$uid], |
|
113
|
|
|
$template->asString($event->variables), |
|
114
|
|
|
// Locations generated from Drush/Console will not necessarily match the |
|
115
|
|
|
// site home URL, and will not therefore not necessarily be reachable, so |
|
116
|
|
|
// we only generate a link if the location is "within" the site. |
|
117
|
|
|
(mb_strpos($location, $this->front) === 0) |
|
118
|
|
|
? Link::fromTextAndUrl(mb_substr($location, $this->baseLength), Url::fromUri($location)) |
|
119
|
|
|
: $location, |
|
120
|
|
|
empty($event->referrer) ? '' : Link::fromTextAndUrl($event->referrer, Url::fromUri($event->referrer)), |
|
121
|
|
|
$event->hostname, |
|
122
|
|
|
isset($event->requestTracking_id) |
|
123
|
|
|
? Link::createFromRoute($this->t('Request'), |
|
124
|
|
|
'mongodb_watchdog.reports.request', |
|
125
|
|
|
['uniqueId' => $event->requestTracking_id]) |
|
126
|
|
|
: '', |
|
127
|
|
|
]; |
|
128
|
|
|
|
|
129
|
|
|
return $ret; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Load MongoDB watchdog events for a given event template. |
|
134
|
|
|
* |
|
135
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
|
136
|
|
|
* The template for which to find events. |
|
137
|
|
|
* @param string $skip |
|
138
|
|
|
* The string representation of the number of events to skip. |
|
139
|
|
|
* @param int $limit |
|
140
|
|
|
* The limit on the number of events to return. |
|
141
|
|
|
* |
|
142
|
|
|
* @return \MongoDB\Driver\Cursor |
|
143
|
|
|
* A cursor to the event occurrences. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function find(EventTemplate $template, $skip, $limit): Cursor { |
|
146
|
|
|
$collection = $this->watchdog->eventCollection($template->_id); |
|
147
|
|
|
$selector = []; |
|
148
|
|
|
$options = [ |
|
149
|
|
|
'skip' => $skip, |
|
150
|
|
|
'limit' => $limit, |
|
151
|
|
|
'sort' => ['$natural' => -1], |
|
152
|
|
|
'typeMap' => [ |
|
153
|
|
|
'array' => 'array', |
|
154
|
|
|
'document' => 'array', |
|
155
|
|
|
'root' => 'Drupal\mongodb_watchdog\Event', |
|
156
|
|
|
], |
|
157
|
|
|
]; |
|
158
|
|
|
|
|
159
|
|
|
$result = $collection->find($selector, $options); |
|
160
|
|
|
return $result; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
} |
|
164
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths