View source
<?php
declare (strict_types=1);
namespace Drupal\mongodb_watchdog\Controller;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBuilderInterface;
use Drupal\Core\Link;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Pager\PagerManagerInterface;
use Drupal\Core\Render\RenderableInterface;
use Drupal\mongodb_watchdog\Event;
use Drupal\mongodb_watchdog\EventTemplate;
use Drupal\mongodb_watchdog\Form\OverviewFilterForm;
use Drupal\mongodb_watchdog\Logger;
use Psr\Log\LogLevel;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
class OverviewController extends ControllerBase {
const EVENT_TYPE_MAP = [
'typeMap' => [
'array' => 'array',
'document' => 'array',
'root' => 'Drupal\\mongodb_watchdog\\Event',
],
];
const SEVERITY_PREFIX = 'mongodb-watchdog__severity--';
const SEVERITY_CLASSES = [
RfcLogLevel::DEBUG => self::SEVERITY_PREFIX . LogLevel::DEBUG,
RfcLogLevel::INFO => self::SEVERITY_PREFIX . LogLevel::INFO,
RfcLogLevel::NOTICE => self::SEVERITY_PREFIX . LogLevel::NOTICE,
RfcLogLevel::WARNING => self::SEVERITY_PREFIX . LogLevel::WARNING,
RfcLogLevel::ERROR => self::SEVERITY_PREFIX . LogLevel::ERROR,
RfcLogLevel::CRITICAL => self::SEVERITY_PREFIX . LogLevel::CRITICAL,
RfcLogLevel::ALERT => self::SEVERITY_PREFIX . LogLevel::ALERT,
RfcLogLevel::EMERGENCY => self::SEVERITY_PREFIX . LogLevel::EMERGENCY,
];
protected $dateFormatter;
protected $formBuilder;
protected $moduleHandler;
protected $rootLength;
public function __construct(LoggerInterface $logger, Logger $watchdog, ImmutableConfig $config, ModuleHandlerInterface $moduleHandler, FormBuilderInterface $formBuilder, DateFormatterInterface $dateFormatter, PagerManagerInterface $pagerManager) {
parent::__construct($logger, $watchdog, $pagerManager, $config);
$this->dateFormatter = $dateFormatter;
$this->formBuilder = $formBuilder;
$this->moduleHandler = $moduleHandler;
$this->rootLength = mb_strlen(DRUPAL_ROOT);
}
public function build(Request $request) : array {
$top = $this
->getTop();
$rows = $this
->getRowData($request);
$main = empty($rows) ? $this
->buildEmpty($this
->t('No event found in logger.')) : $this
->buildMainTable($rows);
$ret = $this
->buildDefaults($main, $top);
return $ret;
}
protected function buildMainTable(array $rows) : array {
$ret = [
'#header' => $this
->buildMainTableHeader(),
'#rows' => $this
->buildMainTableRows($rows),
'#type' => 'table',
];
return $ret;
}
protected function buildMainTableHeader() : array {
$header = [
$this
->t('#'),
$this
->t('Latest'),
$this
->t('Severity'),
$this
->t('Type'),
$this
->t('Message'),
$this
->t('Source'),
];
return $header;
}
protected function buildMainTableRows(array $templates) : array {
$rows = [];
$levels = RfcLogLevel::getLevels();
foreach ($templates as $template) {
$row = [];
$row[] = $template->count;
$row[] = $this->dateFormatter
->format($template->changed, 'short');
$row[] = [
'class' => static::SEVERITY_CLASSES[$template->severity],
'data' => $levels[$template->severity],
];
$row[] = $template->type;
$row[] = $this
->getEventLink($template);
$row[] = [
'data' => $this
->getEventSource($template),
];
$rows[] = $row;
}
return $rows;
}
public static function create(ContainerInterface $container) : self {
$logger = $container
->get('logger.channel.mongodb_watchdog');
$watchdog = $container
->get(Logger::SERVICE_LOGGER);
$config = $container
->get('config.factory')
->get('mongodb_watchdog.settings');
$dateFormatter = $container
->get('date.formatter');
$formBuilder = $container
->get('form_builder');
$moduleHandler = $container
->get('module_handler');
$pagerManager = $container
->get('pager.manager');
return new static($logger, $watchdog, $config, $moduleHandler, $formBuilder, $dateFormatter, $pagerManager);
}
protected function getEventLink(EventTemplate $template) : RenderableInterface {
switch ($template->type) {
case 'page not found':
$cell = Link::createFromRoute($this
->t('( Top 404 )'), 'mongodb_watchdog.reports.top404');
break;
case 'access denied':
$cell = Link::createFromRoute($this
->t('( Top 403 )'), 'mongodb_watchdog.reports.top403');
break;
default:
$markup = new FormattableMarkup($template->message, []);
$message = Unicode::truncate(strip_tags($markup
->__toString()), 56, TRUE, TRUE);
$cell = Link::createFromRoute($message, 'mongodb_watchdog.reports.detail', [
'eventTemplate' => $template->_id,
]);
break;
}
return $cell;
}
protected function getEventSource(EventTemplate $template) : array {
$cell = [
'#markup' => '',
];
if (in_array($template->type, TopController::TYPES)) {
return $cell;
}
$eventCollection = $this->watchdog
->eventCollection($template->_id);
$event = $eventCollection
->findOne([], static::EVENT_TYPE_MAP);
if (!$event instanceof Event) {
return $cell;
}
$file = $event->variables['%file'] ?? '';
if ($file && strncmp($file, DRUPAL_ROOT, $this->rootLength) === 0) {
$hover = mb_substr($file, $this->rootLength + 1);
$file = Unicode::truncate(basename($file), 30);
}
else {
$hover = '';
}
$line = $event->variables['%line'] ?? '';
$cell = [
'#type' => 'html_tag',
'#tag' => 'span',
'#value' => implode("#", [
$file,
$line,
]),
];
if ($hover !== '') {
$cell['#attributes'] = [
'class' => 'mongodb-watchdog__code-path',
'title' => $hover,
];
}
return $cell;
}
protected function getRowData(Request $request) : array {
$count = $this->watchdog
->templatesCount();
$page = $this
->setupPager($request, $count);
$skip = $page * $this->itemsPerPage;
$limit = $this->itemsPerPage;
$filters = $_SESSION[OverviewFilterForm::SESSION_KEY] ?? NULL;
$rows = $this->watchdog
->templates($filters['type'] ?? [], $filters['severity'] ?? [], $skip, $limit)
->toArray();
return $rows;
}
protected function getTop() : array {
$top = $this->formBuilder
->getForm('Drupal\\mongodb_watchdog\\Form\\OverviewFilterForm');
return $top;
}
}