You are here

class DiagnosticsService in Purge 8.3

Provides a service that interacts with diagnostic checks.

Hierarchy

Expanded class hierarchy of DiagnosticsService

1 string reference to 'DiagnosticsService'
purge.services.yml in ./purge.services.yml
purge.services.yml
1 service uses DiagnosticsService
purge.diagnostics in ./purge.services.yml
Drupal\purge\Plugin\Purge\DiagnosticCheck\DiagnosticsService

File

src/Plugin/Purge/DiagnosticCheck/DiagnosticsService.php, line 14

Namespace

Drupal\purge\Plugin\Purge\DiagnosticCheck
View source
class DiagnosticsService extends ServiceBase implements DiagnosticsServiceInterface {
  use ContainerAwareTrait;
  use IteratingServiceBaseTrait;

  /**
   * Logger channel specific to the diagnostics service.
   *
   * @var \Drupal\purge\Logger\LoggerChannelPartInterface
   */
  protected $logger;

  /**
   * The purge executive service, which wipes content from external caches.
   *
   * Do not access this property directly, use ::getPurgers.
   *
   * @var \Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface
   */
  private $purgePurgers;

  /**
   * The queue in which to store, claim and release invalidation objects from.
   *
   * Do not access this property directly, use ::getQueue.
   *
   * @var \Drupal\purge\Plugin\Purge\Queue\QueueServiceInterface
   */
  private $purgeQueue;

  /**
   * Construct the diagnostics service.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface $pluginManager
   *   The plugin manager for this service.
   */
  public function __construct(PluginManagerInterface $pluginManager) {
    $this->pluginManager = $pluginManager;
  }

  /**
   * {@inheritdoc}
   *
   * @ingroup countable
   */
  public function count() {
    $this
      ->initializePluginInstances();
    return count($this->instances);
  }

  /**
   * Get checks filtered by severity.
   *
   * @param int[] $severities
   *   Non-associative list of severity integers to retrieve.
   *
   * @return \ArrayIterator
   *   \Iterator object that yields DiagnosticCheckInterface instances.
   */
  protected function filter(array $severities) {
    $this
      ->initializePluginInstances();
    $checks = new \ArrayIterator();
    foreach ($this as $check) {
      if (in_array($check
        ->getSeverity(), $severities)) {
        $checks
          ->append($check);
      }
    }
    return $checks;
  }

  /**
   * {@inheritdoc}
   */
  public function filterInfo() {
    return $this
      ->filter([
      DiagnosticCheckInterface::SEVERITY_INFO,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function filterOk() {
    return $this
      ->filter([
      DiagnosticCheckInterface::SEVERITY_OK,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function filterWarnings() {
    return $this
      ->filter([
      DiagnosticCheckInterface::SEVERITY_WARNING,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function filterWarningAndErrors() {
    return $this
      ->filter([
      DiagnosticCheckInterface::SEVERITY_WARNING,
      DiagnosticCheckInterface::SEVERITY_ERROR,
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function filterErrors() {
    return $this
      ->filter([
      DiagnosticCheckInterface::SEVERITY_ERROR,
    ]);
  }

  /**
   * Initialize and retrieve the logger via lazy loading.
   *
   * @return \Drupal\purge\Logger\LoggerChannelPartInterface
   *   The logger instance.
   */
  protected function getLogger() {
    if (is_null($this->logger)) {
      $purge_logger = $this->container
        ->get('purge.logger');
      $channel_name = 'diagnostics';

      // By default ::get() would autocreate the channel with grants that are
      // too broad. Therefore precreate it with only the ERROR grant.
      if (!$purge_logger
        ->hasChannel($channel_name)) {
        $purge_logger
          ->setChannel($channel_name, [
          RfcLogLevel::ERROR,
        ]);
      }
      $this->logger = $purge_logger
        ->get($channel_name);
    }
    return $this->logger;
  }

  /**
   * {@inheritdoc}
   */
  public function getPluginsEnabled() {
    if (!is_null($this->pluginsEnabled)) {
      return $this->pluginsEnabled;
    }

    // We blindly load all diagnostic check plugins that we discovered, but not
    // when plugins put dependencies on either a queue or purger plugin. When
    // plugins do depend, we load 'purge.purgers' and/or 'purge.queue' and
    // carefully check if we should load them or not.
    $load = function ($needles, $haystack) {
      if (empty($needles)) {
        return TRUE;
      }
      foreach ($needles as $needle) {
        if (in_array($needle, $haystack)) {
          return TRUE;
        }
      }
      return FALSE;
    };
    $this->pluginsEnabled = [];
    foreach ($this
      ->getPlugins() as $plugin) {
      if (!empty($plugin['dependent_queue_plugins'])) {
        if (!$load($plugin['dependent_queue_plugins'], $this
          ->getQueue()
          ->getPluginsEnabled())) {
          continue;
        }
      }
      if (!empty($plugin['dependent_purger_plugins'])) {
        if (!$load($plugin['dependent_purger_plugins'], $this
          ->getPurgers()
          ->getPluginsEnabled())) {
          continue;
        }
      }
      $this->pluginsEnabled[] = $plugin['id'];
      $this
        ->getLogger()
        ->debug('loaded diagnostic check plugin @id', [
        '@id' => $plugin['id'],
      ]);
    }
    return $this->pluginsEnabled;
  }

  /**
   * Retrieve the 'purge.purgers' service - lazy loaded.
   *
   * @return \Drupal\purge\Plugin\Purge\Purger\PurgersServiceInterface
   *   The 'purge.purgers' service.
   */
  protected function getPurgers() {
    if (is_null($this->purgePurgers)) {
      $this
        ->getLogger()
        ->debug("lazy loading 'purge.purgers' service.");
      $this->purgePurgers = $this->container
        ->get('purge.purgers');
    }
    return $this->purgePurgers;
  }

  /**
   * Retrieve the 'purge.queue' service - lazy loaded.
   *
   * @return \Drupal\purge\Plugin\Purge\Queue\QueueServiceInterface
   *   The 'purge.queue' service.
   */
  protected function getQueue() {
    if (is_null($this->purgeQueue)) {
      $this
        ->getLogger()
        ->debug("lazy loading 'purge.queue' service.");
      $this->purgeQueue = $this->container
        ->get('purge.queue');
    }
    return $this->purgeQueue;
  }

  /**
   * {@inheritdoc}
   */
  public function isSystemOnFire() {
    $this
      ->initializePluginInstances();
    foreach ($this as $check) {
      if ($check
        ->getSeverity() === DiagnosticCheckInterface::SEVERITY_ERROR) {
        return $check;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isSystemShowingSmoke() {
    $this
      ->initializePluginInstances();
    foreach ($this as $check) {
      if ($check
        ->getSeverity() === DiagnosticCheckInterface::SEVERITY_WARNING) {
        return $check;
      }
    }
    return FALSE;
  }

  /**
   * Override to log messages when enabled.
   *
   * @ingroup iterator
   */
  public function next() {

    // The following two lines are copied from parent::next(), since we cannot
    // call protected IteratingServiceBaseTrait::initializePluginInstances().
    $this
      ->initializePluginInstances();
    ++$this->position;

    // When the diagnostics logger has been granted a single or more grants,
    // prerun the tests (results are cached) and log at permitted levels. No
    // worries, the tests don't run when logging is disabled (default).
    if ($this
      ->valid() && $this
      ->getLogger()
      ->getGrants()) {
      $sevmethods = [
        DiagnosticCheckInterface::SEVERITY_WARNING => 'warning',
        DiagnosticCheckInterface::SEVERITY_ERROR => 'error',
        DiagnosticCheckInterface::SEVERITY_INFO => 'info',
        DiagnosticCheckInterface::SEVERITY_OK => 'notice',
      ];
      $context = [
        '@sev' => $this->instances[$this->position]
          ->getSeverityString(),
        '@msg' => $this->instances[$this->position]
          ->getRecommendation(),
        '@title' => $this->instances[$this->position]
          ->getTitle(),
      ];
      $method = $sevmethods[$this->instances[$this->position]
        ->getSeverity()];
      $this
        ->getLogger()
        ->{$method}('@sev: @title: @msg', $context);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function reload() {
    parent::reload();
    $this
      ->reloadIterator();
    $this->purgePurgers = NULL;
    $this->purgeQueue = NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function toMessageList(\Iterator $checks) {
    $messages = [];
    foreach ($checks as $check) {
      $type = strtolower($check
        ->getSeverityString());
      if (!isset($messages[$type])) {
        $messages[$type] = [];
      }
      $msg = strtoupper($check
        ->getTitle()) . ': ';
      if ($recommendation = $check
        ->getRecommendation()) {
        $msg .= ' ' . $recommendation;
      }
      $messages[$type][] = $msg;
    }
    return $messages;
  }

  /**
   * {@inheritdoc}
   */
  public function toRequirementsArray(\Iterator $checks, $prefix_title = FALSE) {
    $requirements = [];
    foreach ($checks as $check) {
      $id = $check
        ->getPluginId();
      $requirements[$id] = $check
        ->getRequirementsArray();
      if ($prefix_title) {
        $requirements[$id]['title'] = "Purge: " . (string) $requirements[$id]['title'];
      }
    }
    return $requirements;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DiagnosticsService::$logger protected property Logger channel specific to the diagnostics service.
DiagnosticsService::$purgePurgers private property The purge executive service, which wipes content from external caches.
DiagnosticsService::$purgeQueue private property The queue in which to store, claim and release invalidation objects from.
DiagnosticsService::count public function
DiagnosticsService::filter protected function Get checks filtered by severity.
DiagnosticsService::filterErrors public function Get only SEVERITY_ERROR level checks. Overrides DiagnosticsServiceInterface::filterErrors
DiagnosticsService::filterInfo public function Get only SEVERITY_INFO level checks. Overrides DiagnosticsServiceInterface::filterInfo
DiagnosticsService::filterOk public function Get only SEVERITY_OK level checks. Overrides DiagnosticsServiceInterface::filterOk
DiagnosticsService::filterWarningAndErrors public function Get only SEVERITY_WARNING and SEVERITY_ERROR level checks. Overrides DiagnosticsServiceInterface::filterWarningAndErrors
DiagnosticsService::filterWarnings public function Get only SEVERITY_WARNING level checks. Overrides DiagnosticsServiceInterface::filterWarnings
DiagnosticsService::getLogger protected function Initialize and retrieve the logger via lazy loading.
DiagnosticsService::getPluginsEnabled public function Retrieve the configured plugin_ids that the service will use. Overrides ServiceBase::getPluginsEnabled
DiagnosticsService::getPurgers protected function Retrieve the 'purge.purgers' service - lazy loaded.
DiagnosticsService::getQueue protected function Retrieve the 'purge.queue' service - lazy loaded.
DiagnosticsService::isSystemOnFire public function Reports if any of the diagnostic checks report a SEVERITY_ERROR severity. Overrides DiagnosticsServiceInterface::isSystemOnFire
DiagnosticsService::isSystemShowingSmoke public function Reports if any of the diagnostic checks report a SEVERITY_WARNING severity. Overrides DiagnosticsServiceInterface::isSystemShowingSmoke
DiagnosticsService::next public function Override to log messages when enabled. Overrides IteratingServiceBaseTrait::next
DiagnosticsService::reload public function Reload the service and reinstantiate all enabled plugins. Overrides ServiceBase::reload
DiagnosticsService::toMessageList public function Generate a status_messages #message_list argument array. Overrides DiagnosticsServiceInterface::toMessageList
DiagnosticsService::toRequirementsArray public function Generate a Drupal-like requirements array. Overrides DiagnosticsServiceInterface::toRequirementsArray
DiagnosticsService::__construct public function Construct the diagnostics service.
IteratingServiceBaseTrait::$instances protected property Holds all instantiated plugins.
IteratingServiceBaseTrait::$position protected property Current iterator position.
IteratingServiceBaseTrait::current public function Return the current element.
IteratingServiceBaseTrait::initializePluginInstances protected function Instantiate all enabled plugins or check that they are present.
IteratingServiceBaseTrait::key public function Return the key of the current element.
IteratingServiceBaseTrait::reloadIterator protected function Rewind the iterator and destruct loaded plugin instances.
IteratingServiceBaseTrait::rewind public function Rewind the Iterator to the first element.
IteratingServiceBaseTrait::valid public function Checks if current position is valid.
ServiceBase::$pluginManager protected property The plugin manager for the given service.
ServiceBase::$plugins protected property The list of all available plugins and their definitions.
ServiceBase::$pluginsEnabled protected property The list of all enabled plugins and their definitions.
ServiceBase::getPlugins public function Retrieve a list of all available plugins providing the service. Overrides ServiceInterface::getPlugins 1
ServiceBase::isPluginEnabled public function Find out whether the given plugin_id is enabled. Overrides ServiceInterface::isPluginEnabled
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