Completed
Pull Request — 8.x-2.x (#5)
by Frédéric G.
04:36 queued 02:00
created

AdminController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 4
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * @file
4
 * Contains AdminController.
5
 */
6
7
namespace Drupal\mongodb_watchdog\Controller;
8
9
use Drupal\Component\Utility\SafeMarkup;
10
use Drupal\Component\Utility\Unicode;
11
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12
use Drupal\Core\Controller\ControllerBase;
13
use Drupal\Core\Url;
14
use Drupal\mongodb\Connection;
15
use MongoDB\Database;
16
use Psr\Log\LoggerInterface;
17
use Psr\Log\LogLevel;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19
use Drupal\Core\Extension\ModuleHandlerInterface;
20
use Drupal\Core\Form\FormBuilderInterface;
21
22
23
/**
24
 * Class AdminController.
25
 *
26
 * @package Drupal\mongodb_watchdog
27
 */
28
class AdminController extends ControllerBase {
29
30
  /**
31
   * @var \MongoDB
32
   */
33
  protected $database;
34
35
  /**
36
   * @var \Drupal\mongodb_watchdog\Logger
37
   */
38
  protected $logger;
39
40
  /**
41
   * The module handler service.
42
   *
43
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
44
   */
45
  protected $moduleHandler;
46
47
  /**
48
   * The form builder service.
49
   *
50
   * @var \Drupal\Core\Form\FormBuilderInterface
51
   */
52
  protected $formBuilder;
53
54
  /**
55
   * Constructor.
56
   *
57
   * @param \MongoDB $database
58
   *   The watchdog database.
59
<<<<<<< HEAD
60
   * @param \Psr\Log\LoggerInterface $logger
61
=======
62
   * @param \Drupal\mongodb_watchdog\Logger $logger
63
>>>>>>> 94b0d5d... Form, tabs and style, as close as possible to dblog code
64
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
65
   *   A module handler.
66
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
67
   *   The form builder service.
68
   */
69
<<<<<<< HEAD
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_SL, expecting T_FUNCTION
Loading history...
70
  public function __construct(Database $database, LoggerInterface $logger, ModuleHandlerInterface $module_handler, FormBuilderInterface $form_builder) {
71
=======
72
  public function __construct(\MongoDB\database $database, Logger $logger, ModuleHandlerInterface $module_handler, FormBuilderInterface $form_builder) {
73
>>>>>>> 94b0d5d... Form, tabs and style, as close as possible to dblog code
74
    $this->database = $database;
75
    $this->logger = $logger;
76
    $this->moduleHandler = $module_handler;
77
    $this->formBuilder = $form_builder;
78
  }
79
80
  /**
81
   * Controller for mongodb_watchdog.detail.
82
   *
83
   * @return array
84
   *   A render array.
85
   */
86
  public function detail() {
87
    return [];
88
  }
89
90
  /**
91
   * Controller for mongodb_watchdog.overview.
92
   *
93
   * @return array
94
   *   A render array.
95
   */
96
  public function overview() {
97
    $icons = array(
98
      LogLevel::DEBUG     => '',
99
      LogLevel::INFO      => '',
100
      LogLevel::NOTICE    => '',
101
      LogLevel::WARNING   => ['#theme' => 'image', 'path' => 'misc/watchdog-warning.png', 'alt' => t('warning'), 'title' => t('warning')],
102
      LogLevel::ERROR     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('error'), 'title' => t('error')],
103
      LogLevel::CRITICAL  => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('critical'), 'title' => t('critical')],
104
      LogLevel::ALERT     => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('alert'), 'title' => t('alert')],
105
      LogLevel::EMERGENCY => ['#theme' => 'image', 'path' => 'misc/watchdog-error.png', 'alt' => t('emergency'), 'title' => t('emergency')],
106
    );
107
108
    $collection = $this->database->selectCollection(Logger::TEMPLATE_COLLECTION);
109
    $cursor = $collection->find();
110
111
    $this->moduleHandler->loadInclude('mongodb_watchdog', 'admin.inc');
112
113
    $build['dblog_filter_form'] = $this->formBuilder->getForm('Drupal\mongodb_watchdog\Form\MongodbWatchdogFilterForm');
114
115
    $header = array(
116
      // Icon column.
117
      '',
118
      t('#'),
119
      array('data' => t('Type')),
120
      array('data' => t('Date')),
121
      t('Source'),
122
      t('Message'),
123
    );
124
125
    $rows = array();
126
127
    foreach ($cursor as $id => $value) {
128
      if ($id < 5) {
129
        kint($value, $id);
130
        kint($value->_id);
131
//        if ($value['type'] == 'php' && $value['message'] == '%type: %message in %function (line %line of %file).') {
132
//          $collection = $this->logger->eventCollection($value['_id']);
133
//          $result = $collection->find()
134
//            ->sort(['$natural' => -1])
135
//            ->limit(1)
136
//            ->getNext();
137
//          if ($value) {
138
//            $value['file'] = basename($result['variables']['%file']);
139
//            $value['line'] = $result['variables']['%line'];
140
//            $value['message'] = '%type in %function';
141
//            $value['variables'] = $result['variables'];
142
//          }
143
//        }
144
        $message = Unicode::truncate(strip_tags(SafeMarkup::format($value['message'], [])), 56, TRUE, TRUE);
145
        $value['count'] = $this->logger->eventCollection($value['_id'])->count();
146
        $rows[$id] = [
147
          $icons[$value->severity],
148
          isset($value['count']) && $value['count'] > 1 ? intval($value['count']) : 0,
149
          t($value['type']),
150
          empty($value['timestamp']) ? '' : format_date($value['timestamp'], 'short'),
151
          empty($value['file']) ? '' : Unicode::truncate(basename($value['file']), 30) . (empty($value['line']) ? '' : ('+' . $value['line'])),
152
          \Drupal::l($message, Url::fromRoute('mongodb_watchdog.detail', ['id' => $id])),
153
        ];
154
      }
155
156
    }
157
kint($rows);
158
    $build['mongodb_watchdog_table'] = array(
159
      '#theme' => 'table',
160
      '#header' => $header,
161
      '#rows' => $rows,
162
      '#attributes' => ['id' => 'admin-mongodb_watchdog'],
163
      '#attached' => array(
164
        'library' => array('mongodb_watchdog/drupal.mongodb_watchdog'),
165
      ),
166
    );
167
168
    $build['mongodb_watchdog_pager'] = array('#type' => 'pager');
169
170
    return $build;
171
  }
172
173
  /**
174
   * The controller factory.
175
   *
176
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
177
   *   The DIC.
178
   *
179
   * @return static
180
   *   The database instance.
181
   */
182
  public static function create(ContainerInterface $container) {
183
    /** @var \MongoDB $database */
184
    $database = $container->get('mongodb.watchdog_storage');
185
    /** @var \Psr\Log\LoggerInterface $logger */
186
    $logger = $container->get('logger.factory')->get('mongodb_watchdog');
187
188
    $form_builder = $container->get('form_builder');
189
    $module_handler = $container->get('module_handler');
190
    return new static(
191
      $database,
192
      $logger,
193
      $module_handler,
194
      $form_builder
195
    );
196
  }
197
}
198