DiagnosticsService.php in Purge 8.3
File
src/Plugin/Purge/DiagnosticCheck/DiagnosticsService.php
View source
<?php
namespace Drupal\purge\Plugin\Purge\DiagnosticCheck;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\purge\IteratingServiceBaseTrait;
use Drupal\purge\ServiceBase;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
class DiagnosticsService extends ServiceBase implements DiagnosticsServiceInterface {
use ContainerAwareTrait;
use IteratingServiceBaseTrait;
protected $logger;
private $purgePurgers;
private $purgeQueue;
public function __construct(PluginManagerInterface $pluginManager) {
$this->pluginManager = $pluginManager;
}
public function count() {
$this
->initializePluginInstances();
return count($this->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;
}
public function filterInfo() {
return $this
->filter([
DiagnosticCheckInterface::SEVERITY_INFO,
]);
}
public function filterOk() {
return $this
->filter([
DiagnosticCheckInterface::SEVERITY_OK,
]);
}
public function filterWarnings() {
return $this
->filter([
DiagnosticCheckInterface::SEVERITY_WARNING,
]);
}
public function filterWarningAndErrors() {
return $this
->filter([
DiagnosticCheckInterface::SEVERITY_WARNING,
DiagnosticCheckInterface::SEVERITY_ERROR,
]);
}
public function filterErrors() {
return $this
->filter([
DiagnosticCheckInterface::SEVERITY_ERROR,
]);
}
protected function getLogger() {
if (is_null($this->logger)) {
$purge_logger = $this->container
->get('purge.logger');
$channel_name = 'diagnostics';
if (!$purge_logger
->hasChannel($channel_name)) {
$purge_logger
->setChannel($channel_name, [
RfcLogLevel::ERROR,
]);
}
$this->logger = $purge_logger
->get($channel_name);
}
return $this->logger;
}
public function getPluginsEnabled() {
if (!is_null($this->pluginsEnabled)) {
return $this->pluginsEnabled;
}
$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;
}
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;
}
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;
}
public function isSystemOnFire() {
$this
->initializePluginInstances();
foreach ($this as $check) {
if ($check
->getSeverity() === DiagnosticCheckInterface::SEVERITY_ERROR) {
return $check;
}
}
return FALSE;
}
public function isSystemShowingSmoke() {
$this
->initializePluginInstances();
foreach ($this as $check) {
if ($check
->getSeverity() === DiagnosticCheckInterface::SEVERITY_WARNING) {
return $check;
}
}
return FALSE;
}
public function next() {
$this
->initializePluginInstances();
++$this->position;
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);
}
}
public function reload() {
parent::reload();
$this
->reloadIterator();
$this->purgePurgers = NULL;
$this->purgeQueue = NULL;
}
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;
}
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;
}
}