You are here

final class CdnWarmer in Warmer 8

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

The cache warmer for the built-in entity cache.

Plugin annotation


@Warmer(
  id = "cdn",
  label = @Translation("CDN"),
  description = @Translation("Executes HTTP requests to warm the edge caches. It is useful without a CDN as well, as it will also warm Varnish and Page Cache.")
)

Hierarchy

Expanded class hierarchy of CdnWarmer

File

modules/warmer_cdn/src/Plugin/warmer/CdnWarmer.php, line 25

Namespace

Drupal\warmer_cdn\Plugin\warmer
View source
final class CdnWarmer extends WarmerPluginBase {
  use UserInputParserTrait;

  /**
   * The HTTP client.
   *
   * @var \GuzzleHttp\ClientInterface
   */
  private $httpClient;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    assert($instance instanceof CdnWarmer);
    $instance
      ->setHttpClient($container
      ->get('http_client'));
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function loadMultiple(array $ids = []) {

    // Ensure items are fully loaded URLs.
    $urls = array_map([
      $this,
      'resolveUri',
    ], $ids);
    return array_filter($urls, [
      UrlHelper::class,
      'isValid',
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function warmMultiple(array $items = []) {
    $headers = $this
      ->parseHeaders();
    $verify = (bool) $this
      ->getConfiguration()['verify'];
    $responses = array_map(function ($url) use ($headers, $verify) {
      try {
        return $this->httpClient
          ->request('GET', $url, [
          'headers' => $headers,
          'verify' => $verify,
        ]);
      } catch (ClientException $exception) {
        return $exception
          ->getResponse();
      } catch (RequestException $exception) {
        return $exception
          ->getResponse();
      }
    }, $items);
    $responses = array_filter($responses, function ($res) {
      return $res instanceof ResponseInterface;
    });
    $successful = array_filter($responses, function (ResponseInterface $res) {
      return $res
        ->getStatusCode() < 399;
    });
    return count($successful);
  }

  /**
   * Parses the configuration to extract the headers to inject in every request.
   *
   * @return array
   *   The array of headers as expected by Guzzle.
   */
  private function parseHeaders() {
    $configuration = $this
      ->getConfiguration();
    $header_lines = $configuration['headers'];

    // Parse headers.
    return array_reduce($header_lines, function ($carry, $header_line) {
      list($name, $value_line) = array_map('trim', explode(':', $header_line));
      $values = array_map('trim', explode(';', $value_line));
      $values = array_filter($values);
      $values = count($values) === 1 ? reset($values) : $values;
      $carry[$name] = $values;
      return $carry;
    }, []);
  }

  /**
   * {@inheritdoc}
   */
  public function buildIdsBatch($cursor) {

    // Parse the sitemaps and extract the URLs.
    $config = $this
      ->getConfiguration();
    $urls = empty($config['urls']) ? [] : $config['urls'];
    $cursor_position = is_null($cursor) ? -1 : array_search($cursor, $urls);
    if ($cursor_position === FALSE) {
      return [];
    }
    return array_slice($urls, $cursor_position + 1, (int) $this
      ->getBatchSize());
  }
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
    parent::validateConfigurationForm($form, $form_state);
    $this
      ->validateHeaders($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function addMoreConfigurationFormElements(array $form, SubformStateInterface $form_state) {
    $configuration = $this
      ->getConfiguration();
    $form['urls'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('URLs'),
      '#description' => $this
        ->t('Enter the list of URLs. One on each line. Examples: https://example.org/foo/bar, /foo/bar.'),
      '#default_value' => empty($configuration['urls']) ? '' : implode("\n", $configuration['urls']),
    ];
    $form['headers'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Headers'),
      '#description' => $this
        ->t('Specific headers to use when making HTTP requests. Format: <code>Header-Name: value1; value2</code>'),
      '#default_value' => empty($configuration['headers']) ? '' : implode("\n", $configuration['headers']),
    ];
    $form['verify'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable SSL verification'),
      '#description' => $this
        ->t('Enable SSL verification. Recommended to keep it checked for security reasons.'),
      '#default_value' => isset($configuration['verify']) ? $configuration['verify'] : TRUE,
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
    $configuration = $form_state
      ->getValues() + $this->configuration;
    $configuration['urls'] = $this
      ->extractTextarea($configuration, 'urls');
    $configuration['headers'] = $this
      ->extractTextarea($configuration, 'headers');
    $this
      ->setConfiguration($configuration);
  }

  /**
   * Set the HTTP client.
   *
   * @param \GuzzleHttp\ClientInterface $client
   *   The client.
   */
  public function setHttpClient(ClientInterface $client) {
    $this->httpClient = $client;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CdnWarmer::$httpClient private property The HTTP client.
CdnWarmer::addMoreConfigurationFormElements public function Adds additional form elements to the configuration form. Overrides WarmerInterface::addMoreConfigurationFormElements
CdnWarmer::buildIdsBatch public function Builds the next batch of IDs based on a position cursor. Overrides WarmerInterface::buildIdsBatch
CdnWarmer::create public static function Creates an instance of the plugin. Overrides WarmerPluginBase::create
CdnWarmer::loadMultiple public function Loads multiple items based on their IDs. Overrides WarmerInterface::loadMultiple
CdnWarmer::parseHeaders private function Parses the configuration to extract the headers to inject in every request.
CdnWarmer::setHttpClient public function Set the HTTP client.
CdnWarmer::submitConfigurationForm public function Form submission handler. Overrides WarmerPluginBase::submitConfigurationForm
CdnWarmer::validateConfigurationForm public function Form validation handler. Overrides WarmerPluginBase::validateConfigurationForm
CdnWarmer::warmMultiple public function Warms multiple items. Overrides WarmerInterface::warmMultiple
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.
UserInputParserTrait::extractTextarea private function Parses the string under $key in the $values collection.
UserInputParserTrait::resolveUri private function Resolves a URI into a fully loaded URL.
UserInputParserTrait::validateHeaders private function Validate the input for the headers.
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::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::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct