You are here

class LoggerService in Purge 8.3

Provides logging services to purge and its submodules, via a single channel.

Hierarchy

Expanded class hierarchy of LoggerService

1 file declares its use of LoggerService
LoggerServiceTest.php in tests/src/Unit/Logger/LoggerServiceTest.php
1 string reference to 'LoggerService'
purge.services.yml in ./purge.services.yml
purge.services.yml
1 service uses LoggerService
purge.logger in ./purge.services.yml
Drupal\purge\Logger\LoggerService

File

src/Logger/LoggerService.php, line 13

Namespace

Drupal\purge\Logger
View source
class LoggerService extends ServiceProviderBase implements LoggerServiceInterface, DestructableInterface {

  /**
   * The name of the configuration object.
   *
   * @var string
   */
  const CONFIG = 'purge.logger_channels';

  /**
   * The name of the used sequence key in the configuration object.
   *
   * @var string
   */
  const CKEY = 'channels';

  /**
   * Initialized channel part instances.
   *
   * @var \Drupal\purge\Logger\LoggerChannelPartInterface[]
   */
  protected $channels = [];

  /**
   * Raw configuration payload as stored in CMI.
   *
   * @var array[]
   */
  protected $config = [];

  /**
   * The factory for configuration objects.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Available RFC 5424 log types.
   *
   * @var int[]
   */
  protected $grants = [
    RfcLogLevel::EMERGENCY,
    RfcLogLevel::ALERT,
    RfcLogLevel::CRITICAL,
    RfcLogLevel::ERROR,
    RfcLogLevel::WARNING,
    RfcLogLevel::NOTICE,
    RfcLogLevel::INFO,
    RfcLogLevel::DEBUG,
  ];

  /**
   * The channel part factory.
   *
   * @var \Drupal\purge\Logger\LoggerChannelPartFactoryInterface
   */
  protected $purgeLoggerPartsFactory;

  /**
   * Whether configuration needs to get written to CMI at object destruction.
   *
   * @var false|true
   */
  protected $write = FALSE;

  /**
   * Construct \Drupal\purge\Logger\LoggerService.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\purge\Logger\LoggerChannelPartFactoryInterface $purge_logger_parts_factory
   *   The channel part factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelPartFactoryInterface $purge_logger_parts_factory) {
    $this->configFactory = $config_factory;
    $this->purgeLoggerPartsFactory = $purge_logger_parts_factory;

    // Set configuration when CMI has it.
    if (is_array($c = $config_factory
      ->get(self::CONFIG)
      ->get(self::CKEY))) {
      $this->config = $c;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function destruct() {
    if ($this->write) {
      $this->configFactory
        ->getEditable(self::CONFIG)
        ->set(self::CKEY, $this->config)
        ->save();
      $this->write = FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteChannel($id) {
    foreach ($this->config as $i => $channel) {
      if ($channel['id'] === $id) {
        unset($this->config[$i]);
        $this->write = TRUE;
        return;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  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;
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  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];
  }

  /**
   * {@inheritdoc}
   */
  public function getChannels() {
    return $this->config;
  }

  /**
   * {@inheritdoc}
   */
  public function hasChannel($id) {
    foreach ($this->config as $channel) {
      if ($channel['id'] === $id) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setChannel($id, array $grants = []) {

    // Perform input validation.
    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!");
      }
    }

    // Determine the config index that we'll write to (existing or new).
    $i = end($this->config) ? key($this->config) + 1 : 0;
    foreach ($this->config as $index => $channel) {
      if ($channel['id'] === $id) {
        $i = $index;
        break;
      }
    }

    // (Over)write the channel and its grants.
    $this->config[$i] = [
      'id' => $id,
      'grants' => $grants,
    ];
    $this->write = TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LoggerService::$channels protected property Initialized channel part instances.
LoggerService::$config protected property Raw configuration payload as stored in CMI.
LoggerService::$configFactory protected property The factory for configuration objects.
LoggerService::$grants protected property Available RFC 5424 log types.
LoggerService::$purgeLoggerPartsFactory protected property The channel part factory.
LoggerService::$write protected property Whether configuration needs to get written to CMI at object destruction.
LoggerService::CKEY constant The name of the used sequence key in the configuration object.
LoggerService::CONFIG constant The name of the configuration object.
LoggerService::deleteChannel public function Delete a channel part. Overrides LoggerServiceInterface::deleteChannel
LoggerService::deleteChannels public function Delete channel parts of which the ID starts with... Overrides LoggerServiceInterface::deleteChannels
LoggerService::destruct public function Performs destruct operations. Overrides DestructableInterface::destruct
LoggerService::get public function Retrieve a channel part instance. Overrides LoggerServiceInterface::get
LoggerService::getChannels public function Retrieve information on all registered channels and their grants. Overrides LoggerServiceInterface::getChannels
LoggerService::hasChannel public function Check whether the given channel is registered. Overrides LoggerServiceInterface::hasChannel
LoggerService::setChannel public function Add or update a channel part and its permissions. Overrides LoggerServiceInterface::setChannel
LoggerService::__construct public function Construct \Drupal\purge\Logger\LoggerService.
ServiceProviderBase::alter public function Modifies existing service definitions. Overrides ServiceModifierInterface::alter 5
ServiceProviderBase::register public function Registers services to the container. Overrides ServiceProviderInterface::register 1