You are here

class CacheLogger in Ultimate Cron 8.2

Cache Logger.

Plugin annotation


@LoggerPlugin(
  id = "cache",
  title = @Translation("Cache"),
  description = @Translation("Stores the last log entry (and only the last) in the cache."),
)

Hierarchy

Expanded class hierarchy of CacheLogger

1 file declares its use of CacheLogger
LoggerPluginTest.php in tests/src/Kernel/LoggerPluginTest.php

File

src/Plugin/ultimate_cron/Logger/CacheLogger.php, line 22

Namespace

Drupal\ultimate_cron\Plugin\ultimate_cron\Logger
View source
class CacheLogger extends LoggerBase implements ContainerFactoryPluginInterface {

  /**
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, CacheBackendInterface $cache) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->cache = $cache;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $bin = isset($configuration['bin']) ? $configuration['bin'] : 'ultimate_cron_logger';
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('cache.' . $bin));
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return array(
      'bin' => 'ultimate_cron_logger',
      'timeout' => Cache::PERMANENT,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function load($name, $lock_id = NULL, array $log_types = [
    ULTIMATE_CRON_LOG_TYPE_NORMAL,
  ]) {
    $log_entry = new LogEntry($name, $this);
    if (!$lock_id) {
      $cache = $this->cache
        ->get('uc-name:' . $name, TRUE);
      if (empty($cache) || empty($cache->data)) {
        return $log_entry;
      }
      $lock_id = $cache->data;
    }
    $cache = $this->cache
      ->get('uc-lid:' . $lock_id, TRUE);
    if (!empty($cache->data)) {
      $log_entry
        ->setData((array) $cache->data);
      $log_entry->finished = TRUE;
    }
    return $log_entry;
  }

  /**
   * {@inheritdoc}
   */
  public function getLogEntries($name, array $log_types, $limit = 10) {
    $log_entry = $this
      ->load($name);
    return $log_entry->lid ? array(
      $log_entry,
    ) : array();
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form['bin'] = array(
      '#type' => 'textfield',
      '#title' => t('Cache bin'),
      '#description' => t('Select which cache bin to use for storing logs.'),
      '#default_value' => $this->configuration['bin'],
      '#fallback' => TRUE,
      '#required' => TRUE,
    );
    $form['timeout'] = array(
      '#type' => 'textfield',
      '#title' => t('Cache timeout'),
      '#description' => t('Seconds before cache entry expires (0 = never, -1 = on next general cache wipe).'),
      '#default_value' => $this->configuration['timeout'],
      '#fallback' => TRUE,
      '#required' => TRUE,
    );
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function save(LogEntry $log_entry) {
    if (!$log_entry->lid) {
      return;
    }
    $settings = $this
      ->getConfiguration();
    $expire = $settings['timeout'] != Cache::PERMANENT ? REQUEST_TIME + $settings['timeout'] : $settings['timeout'];
    $this->cache
      ->set('uc-name:' . $log_entry->name, $log_entry->lid, $expire);
    $this->cache
      ->set('uc-lid:' . $log_entry->lid, $log_entry
      ->getData(), $expire);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheLogger::$cache protected property
CacheLogger::buildConfigurationForm public function Form constructor. Overrides CronPlugin::buildConfigurationForm
CacheLogger::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
CacheLogger::defaultConfiguration public function Gets default configuration for this plugin. Overrides CronPlugin::defaultConfiguration
CacheLogger::getLogEntries public function Get page with log entries for a job. Overrides LoggerInterface::getLogEntries
CacheLogger::load public function Load a log. Overrides LoggerInterface::load
CacheLogger::save public function Saves a log entry. Overrides LoggerInterface::save
CacheLogger::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides CronPlugin::__construct
CronPlugin::$globalOptions public static property
CronPlugin::$instances public static property
CronPlugin::$multiple public static property 1
CronPlugin::$weight public property
CronPlugin::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
CronPlugin::cleanForm public function Clean form of empty fallback values.
CronPlugin::drupal_array_remove_nested_value public function Modified version drupal_array_get_nested_value().
CronPlugin::fallbackalize public function Process fallback form parameters.
CronPlugin::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
CronPlugin::getGlobalOption public static function Get global plugin option.
CronPlugin::getGlobalOptions public static function Get all global plugin options.
CronPlugin::getPluginTypes public static function Returns a list of plugin types.
CronPlugin::isValid public function Default plugin valid for all jobs. 1
CronPlugin::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
CronPlugin::setGlobalOption public static function Set global plugin option.
CronPlugin::settingsLabel public function Get label for a specific setting. 1
CronPlugin::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
CronPlugin::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LoggerBase::$log_entries public static property
LoggerBase::createEntry public function Create a new log entry. Overrides LoggerInterface::createEntry
LoggerBase::factoryLogEntry public function Factory method for creating a new unsaved log entry object. Overrides LoggerInterface::factoryLogEntry
LoggerBase::loadLatestLogEntries public function Load latest log entry for multiple jobs. Overrides LoggerInterface::loadLatestLogEntries 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.