class EventTemplateConverter in MongoDB 8.2
Class EventTemplateConverter load MongoDB watchdog event templates by id.
Hierarchy
- class \Drupal\mongodb_watchdog\EventTemplateConverter implements ParamConverterInterface
Expanded class hierarchy of EventTemplateConverter
1 string reference to 'EventTemplateConverter'
- mongodb_watchdog.services.yml in modules/
mongodb_watchdog/ mongodb_watchdog.services.yml - modules/mongodb_watchdog/mongodb_watchdog.services.yml
1 service uses EventTemplateConverter
- mongodb.watchdog_template_converter in modules/
mongodb_watchdog/ mongodb_watchdog.services.yml - Drupal\mongodb_watchdog\EventTemplateConverter
File
- modules/
mongodb_watchdog/ src/ EventTemplateConverter.php, line 14
Namespace
Drupal\mongodb_watchdogView source
class EventTemplateConverter implements ParamConverterInterface {
const PARAM_TYPE = 'mongodb_watchdog_event_template';
/**
* The core logger channel service.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* The MongoDB logger service, to load events.
*
* @var \Drupal\mongodb_watchdog\Logger
*/
protected $watchdog;
/**
* EventTemplateConverter constructor.
*
* @param \Psr\Log\LoggerInterface $logger
* The logger service, to log intervening events.
* @param \Drupal\mongodb_watchdog\Logger $watchdog
* The MongoDB logger, to load event templates.
*/
public function __construct(LoggerInterface $logger, Logger $watchdog) {
$this->logger = $logger;
$this->watchdog = $watchdog;
}
/**
* {@inheritdoc}
*/
public function convert($value, $definition, $name, array $defaults) : ?EventTemplate {
if (!is_string($value)) {
$this->logger
->notice('Non-string event template id: %id', [
'%id' => var_export($value, TRUE),
]);
return NULL;
}
$selector = [
'_id' => $value,
];
$options = [
'typeMap' => [
'array' => 'array',
'document' => 'array',
'root' => 'Drupal\\mongodb_watchdog\\EventTemplate',
],
];
// Returns null if there is no match, as expected by ParamConverter.
// Never returns an array as findOne() could, because of $options.
$template = $this->watchdog
->templateCollection()
->findOne($selector, $options);
if (empty($template)) {
$this->logger
->notice('Invalid event template id: %id', [
'%id' => $value,
]);
}
assert(is_null($template) || $template instanceof EventTemplate);
return $template;
}
/**
* {@inheritdoc}
*/
public function applies($definition, $name, Route $route) : bool {
return $definition['type'] === static::PARAM_TYPE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EventTemplateConverter:: |
protected | property | The core logger channel service. | |
EventTemplateConverter:: |
protected | property | The MongoDB logger service, to load events. | |
EventTemplateConverter:: |
public | function |
Determines if the converter applies to a specific route and variable. Overrides ParamConverterInterface:: |
|
EventTemplateConverter:: |
public | function |
Converts path variables to their corresponding objects. Overrides ParamConverterInterface:: |
|
EventTemplateConverter:: |
constant | |||
EventTemplateConverter:: |
public | function | EventTemplateConverter constructor. |