1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\ParamConverter\ParamConverterInterface; |
6
|
|
|
use Psr\Log\LoggerInterface; |
7
|
|
|
use Symfony\Component\Routing\Route; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class EventTemplateConverter load MongoDB watchdog event templates by id. |
11
|
|
|
*/ |
12
|
|
|
class EventTemplateConverter implements ParamConverterInterface { |
13
|
|
|
const PARAM_TYPE = 'mongodb_watchdog_event_template'; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* The core logger channel service. |
17
|
|
|
* |
18
|
|
|
* @var \Psr\Log\LoggerInterface |
19
|
|
|
*/ |
20
|
|
|
protected $logger; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The MongoDB logger service, to load events. |
24
|
|
|
* |
25
|
|
|
* @var \Drupal\mongodb_watchdog\Logger |
26
|
|
|
*/ |
27
|
|
|
protected $watchdog; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* EventTemplateConverter constructor. |
31
|
|
|
* |
32
|
|
|
* @param \Psr\Log\LoggerInterface $logger |
33
|
|
|
* The logger service, to log intervening events. |
34
|
|
|
* @param \Drupal\mongodb_watchdog\Logger $watchdog |
35
|
|
|
* The MongoDB logger, to load event templates. |
36
|
|
|
*/ |
37
|
|
|
public function __construct(LoggerInterface $logger, Logger $watchdog) { |
38
|
|
|
$this->logger = $logger; |
39
|
|
|
$this->watchdog = $watchdog; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function convert($value, $definition, $name, array $defaults) { |
46
|
|
|
if (!is_string($value)) { |
47
|
|
|
$this->logger->notice('Non-string event template id: %id', ['%id' => var_export($value, TRUE)]); |
48
|
|
|
return NULL; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$selector = [ |
52
|
|
|
'_id' => $value, |
53
|
|
|
]; |
54
|
|
|
$options = [ |
55
|
|
|
'typeMap' => [ |
56
|
|
|
'array' => 'array', |
57
|
|
|
'document' => 'array', |
58
|
|
|
'root' => 'Drupal\mongodb_watchdog\EventTemplate', |
59
|
|
|
], |
60
|
|
|
]; |
61
|
|
|
|
62
|
|
|
// Returns null if there is no match, as expected by ParamConverter. |
63
|
|
|
$template = $this->watchdog->templateCollection()->findOne($selector, $options); |
64
|
|
|
if (empty($template)) { |
65
|
|
|
$this->logger->debug('Invalid event template id: %id', ['%id' => $value]); |
66
|
|
|
} |
67
|
|
|
return $template; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function applies($definition, $name, Route $route) { |
74
|
|
|
return $definition['type'] === static::PARAM_TYPE; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
|
|
|
|
78
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.