LoggerService.php in Purge 8.3
File
src/Logger/LoggerService.php
View source
<?php
namespace Drupal\purge\Logger;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Drupal\Core\DestructableInterface;
use Drupal\Core\Logger\RfcLogLevel;
class LoggerService extends ServiceProviderBase implements LoggerServiceInterface, DestructableInterface {
const CONFIG = 'purge.logger_channels';
const CKEY = 'channels';
protected $channels = [];
protected $config = [];
protected $configFactory;
protected $grants = [
RfcLogLevel::EMERGENCY,
RfcLogLevel::ALERT,
RfcLogLevel::CRITICAL,
RfcLogLevel::ERROR,
RfcLogLevel::WARNING,
RfcLogLevel::NOTICE,
RfcLogLevel::INFO,
RfcLogLevel::DEBUG,
];
protected $purgeLoggerPartsFactory;
protected $write = FALSE;
public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelPartFactoryInterface $purge_logger_parts_factory) {
$this->configFactory = $config_factory;
$this->purgeLoggerPartsFactory = $purge_logger_parts_factory;
if (is_array($c = $config_factory
->get(self::CONFIG)
->get(self::CKEY))) {
$this->config = $c;
}
}
public function destruct() {
if ($this->write) {
$this->configFactory
->getEditable(self::CONFIG)
->set(self::CKEY, $this->config)
->save();
$this->write = FALSE;
}
}
public function deleteChannel($id) {
foreach ($this->config as $i => $channel) {
if ($channel['id'] === $id) {
unset($this->config[$i]);
$this->write = TRUE;
return;
}
}
}
public function deleteChannels($id_starts_with) {
foreach ($this->config as $i => $channel) {
if (strpos($channel['id'], $id_starts_with) === 0) {
unset($this->config[$i]);
if (!$this->write) {
$this->write = TRUE;
}
}
}
}
public function get($id) {
if (!$this
->hasChannel($id)) {
$this
->setChannel($id, [
RfcLogLevel::EMERGENCY,
RfcLogLevel::CRITICAL,
RfcLogLevel::ERROR,
]);
}
if (!isset($this->channels[$id])) {
$grants = [];
foreach ($this->config as $channel) {
if ($channel['id'] === $id) {
$grants = $channel['grants'];
}
}
$this->channels[$id] = $this->purgeLoggerPartsFactory
->create($id, $grants);
}
return $this->channels[$id];
}
public function getChannels() {
return $this->config;
}
public function hasChannel($id) {
foreach ($this->config as $channel) {
if ($channel['id'] === $id) {
return TRUE;
}
}
return FALSE;
}
public function setChannel($id, array $grants = []) {
if (empty($id) || !is_string($id)) {
throw new \LogicException('The given ID is empty or not a string!');
}
foreach ($grants as $grant) {
if (!in_array($grant, $this->grants)) {
throw new \LogicException("Passed grant is invalid!");
}
}
$i = end($this->config) ? key($this->config) + 1 : 0;
foreach ($this->config as $index => $channel) {
if ($channel['id'] === $id) {
$i = $index;
break;
}
}
$this->config[$i] = [
'id' => $id,
'grants' => $grants,
];
$this->write = TRUE;
}
}
Classes
Name |
Description |
LoggerService |
Provides logging services to purge and its submodules, via a single channel. |