You are here

class LagoonVarnishPurger in Lagoon Varnish 8

HTTP Purger.

Plugin annotation


@PurgePurger(
  id = "lagoon_varnish_purger",
  label = @Translation("Lagoon Varnish Purger"),
  cooldown_time = 0.0,
  description = @Translation("Varnish purger that makes HTTP requests for each given invalidation instruction."),
  multi_instance = FALSE,
  types = {},
)

Hierarchy

Expanded class hierarchy of LagoonVarnishPurger

1 file declares its use of LagoonVarnishPurger
lagoon_varnish.module in ./lagoon_varnish.module
Contains lagoon_varnish.module.

File

src/Plugin/Purge/Purger/LagoonVarnishPurger.php, line 21

Namespace

Drupal\lagoon_varnish\Plugin\Purge\Purger
View source
class LagoonVarnishPurger extends VarnishPurgerBase implements PurgerInterface {

  // Makes const available for use elsewhere.
  const LAGOON_VARNISH_PURGER_ID = 'lagoon_varnish_purger';
  const LAGOON_VARNISH_HOSTNAME = 'varnish';
  const LAGOON_VARNISH_PORT = '8080';
  const LAGOON_VARNISH_SCHEME = 'http';
  const LAGOON_VARNISH_PATH = '/';
  const LAGOON_VARNISH_REQUEST_METHOD = 'BAN';
  const LAGOON_VARNISH_REQUEST_HEADERS = [
    'Cache-Tags' => '[invalidations:separated_pipe]',
  ];

  /**
   * {@inheritdoc}
   */
  public function invalidate(array $invalidations) {

    // Iterate every single object and fire a request per object.
    foreach ($invalidations as $invalidation) {
      $token_data = [
        'invalidation' => $invalidation,
      ];
      $uri = $this
        ->getUri($token_data);
      $opt = $this
        ->getOptions($token_data);
      try {
        $this->client
          ->request($this->settings->request_method, $uri, $opt);
        $invalidation
          ->setState(InvalidationInterface::SUCCEEDED);
      } catch (\Exception $e) {
        $invalidation
          ->setState(InvalidationInterface::FAILED);

        // Log as much useful information as we can.
        $headers = $opt['headers'];
        unset($opt['headers']);
        $debug = json_encode(str_replace("\n", ' ', [
          'msg' => $e
            ->getMessage(),
          'uri' => $uri,
          'method' => $this->settings->request_method,
          'guzzle_opt' => $opt,
          'headers' => $headers,
        ]));
        $this
          ->logger()
          ->emergency("item failed due @e, details (JSON): @debug", [
          '@e' => get_class($e),
          '@debug' => $debug,
        ]);
      }
    }
  }

  /**
   * Retrieve the URI to connect to.
   *
   * @param $token_data
   *   An array of keyed objects, to pass on to the token service.
   *
   * @return string
   *   URL string representation.
   */
  protected function getUri($token_data) {
    return sprintf('%s://%s:%s%s', self::LAGOON_VARNISH_SCHEME, self::LAGOON_VARNISH_HOSTNAME, self::LAGOON_VARNISH_PORT, $this->token
      ->replace(self::LAGOON_VARNISH_PATH, $token_data));
  }

  /**
   * Retrieve all configured headers that need to be set.
   *
   * @param $token_data
   *   An array of keyed objects, to pass on to the token service.
   *
   * @return string[]
   *   Associative array with header values and field names in the key.
   */
  protected function getHeaders($token_data) {
    $headers = [];
    $headers['user-agent'] = 'varnish_purger module for Drupal 8.';
    foreach (self::LAGOON_VARNISH_REQUEST_HEADERS as $field => $value) {
      $headers[strtolower($field)] = $this->token
        ->replace($value, $token_data);
    }
    return $headers;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContainerFactoryPluginInterface::create public static function Creates an instance of the plugin. 112
LagoonVarnishPurger::getHeaders protected function Retrieve all configured headers that need to be set.
LagoonVarnishPurger::getUri protected function Retrieve the URI to connect to.
LagoonVarnishPurger::invalidate public function Invalidate content from external caches. Overrides PurgerInterface::invalidate
LagoonVarnishPurger::LAGOON_VARNISH_HOSTNAME constant
LagoonVarnishPurger::LAGOON_VARNISH_PATH constant
LagoonVarnishPurger::LAGOON_VARNISH_PORT constant
LagoonVarnishPurger::LAGOON_VARNISH_PURGER_ID constant
LagoonVarnishPurger::LAGOON_VARNISH_REQUEST_HEADERS constant
LagoonVarnishPurger::LAGOON_VARNISH_REQUEST_METHOD constant
LagoonVarnishPurger::LAGOON_VARNISH_SCHEME constant
PurgeLoggerAwareInterface::logger public function Return the part logger.
PurgerCapacityDataInterface::getCooldownTime public function Get the time in seconds to wait after invalidation. 1
PurgerCapacityDataInterface::getIdealConditionsLimit public function Get the maximum number of invalidations that this purger can process. 1
PurgerCapacityDataInterface::getRuntimeMeasurement public function Get the runtime measurement counter. 1
PurgerCapacityDataInterface::getTimeHint public function Get the maximum number of seconds, processing a single invalidation takes. 1
PurgerCapacityDataInterface::hasRuntimeMeasurement public function Indicates whether your purger utilizes dynamic runtime measurement. 1
PurgerCapacityDataInterface::setRuntimeMeasurement public function Inject the runtime measurement counter. 1
PurgerInterface::delete public function The current instance of this purger plugin is about to be deleted. 1
PurgerInterface::getId public function Retrieve the unique instance ID for this purger instance. 1
PurgerInterface::getLabel public function Retrieve the user-readable label for this purger instance. 1
PurgerInterface::getTypes public function Retrieve the list of supported invalidation types. 1
PurgerInterface::routeTypeToMethod public function Route certain type of invalidations to other methods. 1