1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Unicode; |
6
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
7
|
|
|
use Drupal\Core\Datetime\DateFormatterInterface; |
8
|
|
|
use Drupal\Core\Link; |
9
|
|
|
use Drupal\Core\Url; |
10
|
|
|
use Drupal\user\Entity\User; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class EventController provides query and render logic for Event occurrences. |
14
|
|
|
*/ |
15
|
|
|
class EventController { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The length of the absolute home URL. |
19
|
|
|
* |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
protected $baseLength; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* The date.formatter service. |
26
|
|
|
* |
27
|
|
|
* @var \Drupal\Core\Datetime\DateFormatterInterface |
28
|
|
|
*/ |
29
|
|
|
protected $dateFormatter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* The absolute path to the site home. |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $front; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* An instance cache for user accounts, which are used in a loop. |
40
|
|
|
* |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $userCache = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The MongoDB logger service, to load events. |
47
|
|
|
* |
48
|
|
|
* @var \Drupal\mongodb_watchdog\Logger |
49
|
|
|
*/ |
50
|
|
|
protected $watchdog; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* EventController constructor. |
54
|
|
|
* |
55
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config |
56
|
|
|
* The config.factory service. |
57
|
|
|
* @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter |
58
|
|
|
* The core data.formatter service. |
59
|
|
|
* @param \Drupal\mongodb_watchdog\Logger $watchdog |
60
|
|
|
* The MongoDB logger service, to load events. |
61
|
|
|
*/ |
62
|
|
|
public function __construct(ConfigFactoryInterface $config, |
63
|
|
|
DateFormatterInterface $date_formatter, |
64
|
|
|
Logger $watchdog) { |
65
|
|
|
$this->anonymous = $config->get('user.settings')->get('anonymous'); |
|
|
|
|
66
|
|
|
$this->front = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(); |
67
|
|
|
$this->baseLength = Unicode::strlen($this->front) - 1; |
|
|
|
|
68
|
|
|
$this->dateFormatter = $date_formatter; |
69
|
|
|
$this->watchdog = $watchdog; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Provide a table row representation of an event occurrence. |
74
|
|
|
* |
75
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
76
|
|
|
* The template for which the occurrence exists. |
77
|
|
|
* @param \Drupal\mongodb_watchdog\Event $event |
78
|
|
|
* The event occurrence to represent. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
* A render array. |
82
|
|
|
*/ |
83
|
|
|
public function asTableRow(EventTemplate $template, Event $event) { |
84
|
|
|
$uid = intval($event->uid); |
85
|
|
|
if (!isset($this->userCache[$uid])) { |
86
|
|
|
$this->userCache[$uid] = $uid ? User::load($uid)->toLink() : $this->anonymous; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$ret = [ |
90
|
|
|
$this->dateFormatter->format($event->timestamp, 'short'), |
91
|
|
|
$this->userCache[$uid], |
92
|
|
|
$template->asString($event->variables), |
93
|
|
|
// Locations generated from Drush/Console will not necessarily match the |
94
|
|
|
// site home URL, and will not therefore not necessarily be reachable, so |
95
|
|
|
// we only generate a link if the location is "within" the site. |
96
|
|
|
(Unicode::strpos($event->location, $this->front) === 0) |
97
|
|
|
? Link::fromTextAndUrl(Unicode::substr($event->location, $this->baseLength), Url::fromUri($event->location)) |
98
|
|
|
: $event->location, |
99
|
|
|
empty($event->referrer) ? '' : Link::fromTextAndUrl($event->referrer, Url::fromUri($event->referrer)), |
100
|
|
|
$event->hostname, |
101
|
|
|
isset($event->requestTracking_id) |
102
|
|
|
? Link::createFromRoute(t('Request'), 'mongodb_watchdog.reports.request', ['unique_id' => $event->requestTracking_id]) |
103
|
|
|
: '', |
104
|
|
|
]; |
105
|
|
|
return $ret; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Load MongoDB watchdog events for a given event template. |
110
|
|
|
* |
111
|
|
|
* @param \Drupal\mongodb_watchdog\EventTemplate $template |
112
|
|
|
* The template for which to find events. |
113
|
|
|
* @param string $skip |
114
|
|
|
* The string representation of the number of events to skip. |
115
|
|
|
* @param int $limit |
116
|
|
|
* The limit on the number of events to return. |
117
|
|
|
* |
118
|
|
|
* @return \MongoDB\Driver\Cursor |
119
|
|
|
* A cursor to the event occurrences. |
120
|
|
|
*/ |
121
|
|
|
public function find(EventTemplate $template, $skip, $limit) { |
122
|
|
|
$collection = $this->watchdog->eventCollection($template->_id); |
123
|
|
|
$selector = []; |
124
|
|
|
$options = [ |
125
|
|
|
'skip' => $skip, |
126
|
|
|
'limit' => $limit, |
127
|
|
|
'sort' => ['$natural' => -1], |
128
|
|
|
'typeMap' => [ |
129
|
|
|
'array' => 'array', |
130
|
|
|
'document' => 'array', |
131
|
|
|
'root' => 'Drupal\mongodb_watchdog\Event', |
132
|
|
|
], |
133
|
|
|
]; |
134
|
|
|
|
135
|
|
|
$result = $collection->find($selector, $options); |
136
|
|
|
return $result; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
} |
|
|
|
|
140
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: