CloudFlarePurger.php in CloudFlare 8
File
modules/cloudflarepurger/src/Plugin/Purge/Purger/CloudFlarePurger.php
View source
<?php
namespace Drupal\cloudflarepurger\Plugin\Purge\Purger;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\cloudflare\CloudFlareStateInterface;
use Drupal\cloudflare\CloudFlareComposerDependenciesCheckInterface;
use Drupal\cloudflarepurger\EventSubscriber\CloudFlareCacheTagHeaderGenerator;
use Drupal\purge\Plugin\Purge\Purger\PurgerBase;
use Drupal\purge\Plugin\Purge\Purger\PurgerInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;
use CloudFlarePhpSdk\ApiEndpoints\CloudFlareAPI;
use CloudFlarePhpSdk\ApiEndpoints\ZoneApi;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Psr\Log\LoggerInterface;
class CloudFlarePurger extends PurgerBase implements PurgerInterface {
protected $config;
protected $logger;
protected $state;
protected $zoneApi;
protected $zone;
protected $areCloudflareComposerDepenciesMet;
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'));
}
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();
}
public function routeTypeToMethod($type) {
$methods = [
'everything' => 'invalidate',
'tag' => 'invalidate',
'url' => 'invalidate',
];
return isset($methods[$type]) ? $methods[$type] : 'invalidate';
}
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);
}
}
public function hasRuntimeMeasurement() {
return TRUE;
}
private function purgeChunk(array &$invalidations) {
$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 = [];
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 {
$invalidation_type = $invalidations[0]
->getPluginId();
if ($invalidation_type == 'tag') {
$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);
}
$this->logger
->error($e
->getMessage());
} finally {
$this->state
->incrementApiRateCount();
}
}
}