You are here

abstract class WarmerPluginBase in Warmer 8

Same name and namespace in other branches
  1. 2.x src/Plugin/WarmerPluginBase.php \Drupal\warmer\Plugin\WarmerPluginBase

Base class for warmer plugins that implement settings forms.

Hierarchy

Expanded class hierarchy of WarmerPluginBase

See also

\Drupal\warmer\Annotation\Warmer

\Drupal\warmer\Plugin\WarmerPluginManager

\Drupal\warmer\Plugin\WarmerInterface

Plugin API

8 files declare their use of WarmerPluginBase
CdnWarmer.php in modules/warmer_cdn/src/Plugin/warmer/CdnWarmer.php
EnqueueForm.php in src/Form/EnqueueForm.php
EntityWarmer.php in modules/warmer_entity/src/Plugin/warmer/EntityWarmer.php
HookImplementations.php in src/HookImplementations.php
QueueManager.php in src/QueueManager.php

... See full list

File

src/Plugin/WarmerPluginBase.php, line 25

Namespace

Drupal\warmer\Plugin
View source
abstract class WarmerPluginBase extends PluginBase implements ContainerFactoryPluginInterface, PluginFormInterface, ConfigurableInterface, WarmerInterface, DependentPluginInterface {

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

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

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $settings = $container
      ->get('config.factory')
      ->get('warmer.settings')
      ->get('warmers');
    $plugin_settings = empty($settings[$plugin_id]) ? [] : $settings[$plugin_id];
    $configuration = array_merge($plugin_settings, $configuration);
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('state'), $container
      ->get('datetime.time'));
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return [
      'id' => $this
        ->getPluginId(),
    ] + $this->configuration + $this
      ->defaultConfiguration();
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration + $this
      ->defaultConfiguration();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'frequency' => 5 * 60,
      'batchSize' => 50,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    $frequency = $form_state
      ->getValue('frequency');
    $batch_size = $form_state
      ->getValue('batchSize');
    if (!is_numeric($frequency) || $frequency < 0) {
      $form_state
        ->setError($form[$this
        ->getPluginId()]['frequency'], $this
        ->t('Frequency should be a positive number.'));
    }
    if (!is_numeric($batch_size) || $batch_size < 1) {
      $form_state
        ->setError($form[$this
        ->getPluginId()]['batchSize'], $this
        ->t('Batch size should be a number greater than 1.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public final function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $configuration = $this
      ->getConfiguration() + $this
      ->defaultConfiguration();
    $plugin_id = $configuration['id'];
    $definition = $this
      ->getPluginDefinition();
    $form[$plugin_id] = empty($form[$plugin_id]) ? [
      '#type' => 'details',
      '#open' => TRUE,
      '#title' => empty($definition['label']) ? $plugin_id : $definition['label'],
      '#group' => 'warmers',
      '#tree' => TRUE,
    ] : $form[$plugin_id];
    if (!empty($definition['description'])) {
      $form[$plugin_id]['description'] = [
        '#type' => 'html_tag',
        '#tag' => 'em',
        '#value' => $definition['description'],
      ];
    }
    $form[$plugin_id]['frequency'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Frequency'),
      '#description' => $this
        ->t('Only re-enqueue warming operations after at least this many seconds have passed.'),
      '#default_value' => $this
        ->getFrequency(),
    ];
    $form[$plugin_id]['batchSize'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Batch Size'),
      '#description' => $this
        ->t('Number of items to enqueue and process in a single go.'),
      '#default_value' => $this
        ->getBatchSize(),
    ];
    $subform_state = SubformState::createForSubform($form[$plugin_id], $form, $form_state);
    $form[$plugin_id] = $this
      ->addMoreConfigurationFormElements($form[$plugin_id], $subform_state);
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $this
      ->setConfiguration($form_state
      ->getValues() + $this->configuration);
  }

  /**
   * {@inheritdoc}
   */
  public function getBatchSize() {
    $configuration = $this
      ->getConfiguration();
    return $configuration['batchSize'];
  }

  /**
   * {@inheritdoc}
   */
  public function getFrequency() {
    $configuration = $this
      ->getConfiguration();
    return $configuration['frequency'];
  }

  /**
   * {@inheritdoc}
   */
  public function isActive() {
    $configuration = $this
      ->getConfiguration();
    $last_run = $this->state
      ->get('previous_enqueue_time:' . $configuration['id']);
    return $this->time
      ->getRequestTime() > $last_run + $this
      ->getFrequency();
  }

  /**
   * {@inheritdoc}
   */
  public function markAsEnqueued() {
    $configuration = $this
      ->getConfiguration();
    $this->state
      ->set('previous_enqueue_time:' . $configuration['id'], $this->time
      ->getRequestTime());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
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.
WarmerInterface::addMoreConfigurationFormElements public function Adds additional form elements to the configuration form. 3
WarmerInterface::buildIdsBatch public function Builds the next batch of IDs based on a position cursor. 3
WarmerInterface::loadMultiple public function Loads multiple items based on their IDs. 3
WarmerInterface::warmMultiple public function Warms multiple items. 3
WarmerPluginBase::$state protected property The state service.
WarmerPluginBase::$time protected property The time service.
WarmerPluginBase::buildConfigurationForm final public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm
WarmerPluginBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
WarmerPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 3
WarmerPluginBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration 1
WarmerPluginBase::getBatchSize public function Returns the batch size for the warming operation. Overrides WarmerInterface::getBatchSize
WarmerPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
WarmerPluginBase::getFrequency public function Returns the frequency for the warming operation. Overrides WarmerInterface::getFrequency
WarmerPluginBase::isActive public function Checks if the plugin should warm in this particular moment. Overrides WarmerInterface::isActive
WarmerPluginBase::markAsEnqueued public function Marks a warmer as enqueued. Overrides WarmerInterface::markAsEnqueued
WarmerPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
WarmerPluginBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 2
WarmerPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 2
WarmerPluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct