class CloudFlarePurger in CloudFlare 8
CloudFlare purger.
Plugin annotation
@PurgePurger(
id = "cloudflare",
label = @Translation("CloudFlare"),
description = @Translation("Purger for CloudFlare."),
types = {"tag", "url", "everything"},
multi_instance = FALSE,
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\purge\Plugin\Purge\Purger\PurgerBase implements PurgerInterface uses PurgeLoggerAwareTrait
- class \Drupal\cloudflarepurger\Plugin\Purge\Purger\CloudFlarePurger implements PurgerInterface
- class \Drupal\purge\Plugin\Purge\Purger\PurgerBase implements PurgerInterface uses PurgeLoggerAwareTrait
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of CloudFlarePurger
File
- modules/
cloudflarepurger/ src/ Plugin/ Purge/ Purger/ CloudFlarePurger.php, line 28
Namespace
Drupal\cloudflarepurger\Plugin\Purge\PurgerView source
class CloudFlarePurger extends PurgerBase implements PurgerInterface {
/**
* The settings configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* A logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Tracks rate limits associated with CloudFlare Api.
*
* @var \Drupal\cloudflare\CloudFlareStateInterface
*/
protected $state;
/**
* ZoneApi object for interfacing with CloudFlare Php Sdk.
*
* @var \CloudFlarePhpSdk\ApiEndpoints\ZoneApi
*/
protected $zoneApi;
/**
* The current cloudflare ZoneId.
*
* @var string
*/
protected $zone;
/**
* TRUE if composer dependencies are met. False otherwise.
*
* @var bool
*/
protected $areCloudflareComposerDepenciesMet;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('config.factory'), $container
->get('cloudflare.state'), $container
->get('logger.factory')
->get('cloudflare'), $container
->get('cloudflare.composer_dependency_check'));
}
/**
* Constructs a \Drupal\Component\Plugin\CloudFlarePurger.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* @param \Drupal\cloudflare\CloudFlareStateInterface $state
* Tracks limits associated with CloudFlare Api.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\cloudflare\CloudFlareComposerDependenciesCheckInterface $checker
* Tests that composer dependencies are met.
*
* @throws \LogicException
* Thrown if $configuration['id'] is missing, see Purger\Service::createId.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory, CloudFlareStateInterface $state, LoggerInterface $logger, CloudFlareComposerDependenciesCheckInterface $checker) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->config = $config_factory
->get('cloudflare.settings');
$this->state = $state;
$this->logger = $logger;
$this->areCloudflareComposerDepenciesMet = $checker
->check();
}
/**
* {@inheritdoc}
*/
public function routeTypeToMethod($type) {
$methods = [
'everything' => 'invalidate',
'tag' => 'invalidate',
'url' => 'invalidate',
];
return isset($methods[$type]) ? $methods[$type] : 'invalidate';
}
/**
* {@inheritdoc}
*/
public function invalidate(array $invalidations) {
$chunks = array_chunk($invalidations, CloudFlareAPI::MAX_TAG_PURGES_PER_REQUEST);
$has_invalidations = count($invalidations) > 0;
if (!$has_invalidations) {
return;
}
foreach ($chunks as $chunk) {
$this
->purgeChunk($chunk);
}
}
/**
* {@inheritdoc}
*/
public function hasRuntimeMeasurement() {
return TRUE;
}
/**
* Purges a chunk of tags.
*
* Integration point between purge and CloudFlareAPI. Purge requires state
* tracking on each item purged. This function provides that accounting and
* calls CloudflareApi.
*
* CloudFlare only allows us to purge 30 tags at once.
*
* @param array $invalidations
* Chunk of purge module invalidation objects to purge via CloudFlare.
*/
private function purgeChunk(array &$invalidations) {
// This is a unique case where the ApiSdk is being accessed directly and not
// via a service. Purging should only ever happen through the purge module
// which is why this is NOT in a service.
$api_key = $this->config
->get('apikey');
$email = $this->config
->get('email');
$this->zone = $this->config
->get('zone_id');
$this->zoneApi = new ZoneApi($api_key, $email);
$api_targets_to_purge = [];
// This method is unfortunately a bit verbose due to the fact that we
// need to update the purge states as we proceed.
foreach ($invalidations as $invalidation) {
$invalidation
->setState(InvalidationInterface::PROCESSING);
$api_targets_to_purge[] = $invalidation
->getExpression();
}
if (!$this->areCloudflareComposerDepenciesMet) {
foreach ($invalidations as $invalidation) {
$invalidation
->setState(InvalidationInterface::FAILED);
}
}
try {
// Interface with the CloudFlarePhpSdk.
$invalidation_type = $invalidations[0]
->getPluginId();
if ($invalidation_type == 'tag') {
// @todo Remove this wrapper once CloudFlare supports 16k headers.
// Also invalidate the cache tags as hashes, to automatically also work
// for responses that exceed CloudFlare's Cache-Tag header limit.
$hashes = CloudFlareCacheTagHeaderGenerator::cacheTagsToHashes($api_targets_to_purge);
$this->zoneApi
->purgeTags($this->zone, $hashes);
$this->state
->incrementTagPurgeDailyCount();
}
elseif ($invalidation_type == 'url') {
$this->zoneApi
->purgeIndividualFiles($this->zone, $api_targets_to_purge);
}
elseif ($invalidation_type == 'everything') {
$this->zoneApi
->purgeAllFiles($this->zone);
}
foreach ($invalidations as $invalidation) {
$invalidation
->setState(InvalidationInterface::SUCCEEDED);
}
} catch (\Exception $e) {
foreach ($invalidations as $invalidation) {
$invalidation
->setState(InvalidationInterface::FAILED);
}
// We only want to log a single watchdog error per request. This prevents
// the log from being flooded.
$this->logger
->error($e
->getMessage());
} finally {
$this->state
->incrementApiRateCount();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CloudFlarePurger:: |
protected | property | TRUE if composer dependencies are met. False otherwise. | |
CloudFlarePurger:: |
protected | property | The settings configuration. | |
CloudFlarePurger:: |
protected | property |
A logger instance. Overrides PurgeLoggerAwareTrait:: |
|
CloudFlarePurger:: |
protected | property | Tracks rate limits associated with CloudFlare Api. | |
CloudFlarePurger:: |
protected | property | The current cloudflare ZoneId. | |
CloudFlarePurger:: |
protected | property | ZoneApi object for interfacing with CloudFlare Php Sdk. | |
CloudFlarePurger:: |
public static | function |
Creates an instance of the plugin. Overrides PurgerBase:: |
|
CloudFlarePurger:: |
public | function |
Indicates whether your purger utilizes dynamic runtime measurement. Overrides PurgerCapacityDataInterface:: |
|
CloudFlarePurger:: |
public | function |
Invalidate content from external caches. Overrides PurgerInterface:: |
|
CloudFlarePurger:: |
private | function | Purges a chunk of tags. | |
CloudFlarePurger:: |
public | function |
Route certain type of invalidations to other methods. Overrides PurgerBase:: |
|
CloudFlarePurger:: |
public | function |
Constructs a \Drupal\Component\Plugin\CloudFlarePurger. Overrides PurgerBase:: |
|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PurgeLoggerAwareTrait:: |
public | function | ||
PurgerBase:: |
protected | property | Unique instance ID for this purger. | |
PurgerBase:: |
protected | property | The runtime measurement counter. | |
PurgerBase:: |
public | function |
The current instance of this purger plugin is about to be deleted. Overrides PurgerInterface:: |
1 |
PurgerBase:: |
public | function |
Get the time in seconds to wait after invalidation. Overrides PurgerCapacityDataInterface:: |
|
PurgerBase:: |
public | function |
Retrieve the unique instance ID for this purger instance. Overrides PurgerInterface:: |
|
PurgerBase:: |
public | function |
Get the maximum number of invalidations that this purger can process. Overrides PurgerCapacityDataInterface:: |
1 |
PurgerBase:: |
public | function |
Retrieve the user-readable label for this purger instance. Overrides PurgerInterface:: |
|
PurgerBase:: |
public | function |
Get the runtime measurement counter. Overrides PurgerCapacityDataInterface:: |
|
PurgerBase:: |
public | function |
Get the maximum number of seconds, processing a single invalidation takes. Overrides PurgerCapacityDataInterface:: |
|
PurgerBase:: |
public | function |
Retrieve the list of supported invalidation types. Overrides PurgerInterface:: |
|
PurgerBase:: |
public | function |
Inject the runtime measurement counter. Overrides PurgerCapacityDataInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |