|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Controller; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Core\Config\ImmutableConfig; |
|
6
|
|
|
use Drupal\Core\Controller\ControllerBase as CoreControllerBase; |
|
7
|
|
|
use Drupal\mongodb_watchdog\Logger; |
|
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Base controller class for paged reports. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class ControllerBase extends CoreControllerBase { |
|
16
|
|
|
|
|
17
|
|
|
use LoggerAwareTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The items_per_page configuration value. |
|
21
|
|
|
* |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $itemsPerPage; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* The MongoDB logger, to load events. |
|
28
|
|
|
* |
|
29
|
|
|
* @var \Drupal\mongodb_watchdog\Logger |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $watchdog; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* ControllerBase constructor. |
|
35
|
|
|
* |
|
36
|
|
|
* @param \Drupal\Core\Config\ImmutableConfig $config |
|
37
|
|
|
* The module configuration. |
|
38
|
|
|
*/ |
|
39
|
|
|
public function __construct(LoggerInterface $logger, Logger $watchdog, ImmutableConfig $config) { |
|
40
|
|
|
$this->setLogger($logger); |
|
41
|
|
|
|
|
42
|
|
|
$this->itemsPerPage = $config->get('items_per_page'); |
|
43
|
|
|
$this->watchdog = $watchdog; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* The default build() implementation. |
|
48
|
|
|
* |
|
49
|
|
|
* Cannot be a build() method because each controller build() has a |
|
50
|
|
|
* different signature. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $main |
|
53
|
|
|
* A render array for the main table. |
|
54
|
|
|
* @param array|null $top |
|
55
|
|
|
* A render array for the top element present on some controllers results. |
|
56
|
|
|
* |
|
57
|
|
|
* @return array<string,string|array> |
|
58
|
|
|
* A render array for the whole controller. |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function buildDefaults(array $main, array $top = NULL) { |
|
61
|
|
|
$ret = empty($top) ? [] : ['top' => $top]; |
|
62
|
|
|
|
|
63
|
|
|
$ret += [ |
|
64
|
|
|
'main' => $main, |
|
65
|
|
|
'pager' => ['#type' => 'pager'], |
|
66
|
|
|
'#attached' => [ |
|
67
|
|
|
'library' => ['mongodb_watchdog/styling'], |
|
68
|
|
|
], |
|
69
|
|
|
]; |
|
70
|
|
|
|
|
71
|
|
|
return $ret; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return the top element: empty by default. |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
|
|
|
|
|
78
|
|
|
* A render array for the top filter form. |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function getTop() { |
|
81
|
|
|
$top = NULL; |
|
82
|
|
|
return $top; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Set up the pager. |
|
87
|
|
|
* |
|
88
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
|
89
|
|
|
* The current request. |
|
90
|
|
|
* @param int $count |
|
91
|
|
|
* The total number of possible rows. |
|
92
|
|
|
* |
|
93
|
|
|
* @return int |
|
94
|
|
|
* The number of the page to display, starting at 0. |
|
95
|
|
|
*/ |
|
96
|
|
|
public function setupPager(Request $request, $count) { |
|
97
|
|
|
$height = $this->itemsPerPage; |
|
98
|
|
|
pager_default_initialize($count, $height); |
|
99
|
|
|
|
|
100
|
|
|
$page = intval($request->query->get('page')); |
|
101
|
|
|
if ($page < 0) { |
|
102
|
|
|
$page = 0; |
|
103
|
|
|
} |
|
104
|
|
|
else { |
|
105
|
|
|
$page_max = intval(min(ceil($count / $height), PHP_INT_MAX) - 1); |
|
106
|
|
|
if ($page > $page_max) { |
|
107
|
|
|
$page = $page_max; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $page; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.If the return type contains the type array, this check recommends the use of a more specific type like
String[]orarray<String>.