Skip to content

Commit

Permalink
Added Service Container for CloudFlare module functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
aweingarten committed Sep 29, 2015
1 parent 7672d0d commit 3d3657b
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 0 deletions.
5 changes: 5 additions & 0 deletions cloudflare.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ services:
arguments: ['%cloudflare.cache_tag_header_limit%']
tags:
- { name: event_subscriber }
cloudflare:
class: Drupal\cloudflare\CloudFlareService
arguments: ['@config.factory']
tags:
- { name: cloudflare}
191 changes: 191 additions & 0 deletions src/CloudFlareService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
<?php

/**
* @file
* Contains \Drupal\cloudflare\CloudFlareInvalidator.
*/

namespace Drupal\cloudflare;
use Drupal\Core\Config\ConfigFactoryInterface;
/**
* Invalidation methods for CloudFlare.
*/
class CloudFlareService {
/*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $config;

/*
* @var string
*/
protected $apiKey;


/*
* @var string
*/
protected $email;


/*
* @var string
*/
protected $zone;

/**
* CloudFlareInvalidator constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The config factory.
*/
public function __construct(ConfigFactoryInterface $config) {
$this->config = $config->get('cloudflare.settings');
$this->api_key = $config->get('apikey');
$this->email = $config->get('email');

if ($this->hasValidApiCredentials()) {
$this->zone = $this->getCurrentZoneId();
}
}

/**
* Invalidates tags at CloudFlare edge.
*
* @param array $tags
* String array of tag names to invalidate.
*/
public function invalidateByTags(array $tags) {
if (!$this->hasValidApiCredentials()) {
return;
}

try {
$this->zoneApi = new ZoneApi($this->apiKey, $this->email);
$this->zoneApi->purgeTags($this->zone, $tags);
}

catch (CloudFlareHttpException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

catch (CloudFlareApiException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

// If no exceptions have been thrown then the request has been successful.
}

/**
* Invalidates paths at CloudFlare edge.
*
* @param array $paths
* String array of paths to invalidate.
*/
public function invalidateByPath(array $paths) {
if (!$this->hasValidApiCredentials()) {
return;
}

try {
$this->zoneApi = new ZoneApi($this->apiKey, $this->email);
$this->zoneApi->purgeIndividualFiles($this->zone, $paths);
}

catch (CloudFlareHttpException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

catch (CloudFlareApiException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}
}

/**
* Invalidates entire zone at CloudFlare edge. Use cautiously.
*/
public function invalidateZone() {
if (!$this->hasValidApiCredentials()) {
return;
}

try {
$this->zoneApi = new ZoneApi($this->apiKey, $this->email);
$this->zoneApi->purgeAllFiles($this->zone);
}

catch (CloudFlareHttpException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}

catch (CloudFlareApiException $e) {
drupal_set_message("Unable to clear zone cache. " . $e->getMessage(), 'error');
\Drupal::logger('cloudflare')->error($e->getMessage());
return;
}
}

/**
* Determine the current Cloudflare ZoneId for the site.
*
* Most CloudFlare accounts have a single zone. It can be assumed as the
* default. If there is more than one the user needs to specify the zone.
* on the cloudflare config page.
*
* @return string
* The id of the current zone.
*/
public function getCurrentZoneId() {
$has_zone_in_cmi = !is_null($this->config->get('zone'));

// If this is a multi-zone cloudflare account and a zone has been set.
if ($has_zone_in_cmi) {
return $this->config->get('zone');
}

// If there is no zone set and the account only has a single zone.
$zones_from_api = $this->zoneApi->listZones();
$num_zones_from_api = count($zones_from_api);
$is_single_zone_cloudflare_account = $num_zones_from_api == 1;
if ($is_single_zone_cloudflare_account) {
return $zones_from_api[0]->getZoneId();
}

// If the zone has multiple accounts and none is specified in CMI we cannot
// move forward.
if (!$is_single_zone_cloudflare_account) {
$link_to_settings = '/admin/config/services/cloudflare/zone';
$message = t('No default zone has been entered for CloudFlare. Please go <a href="@link_to_settings">here</a> to set.', ['@link_to_settings' => $link_to_settings]);

drupal_set_message($message, 'error');
\Drupal::logger('cloudflare')->error($message);
}
}

/**
* Checks if the site has valid CloudFlare Api credentials.
*/
private function hasValidApiCredentials() {
if (!isset($this->apiKey)) {
$link_to_settings = '/admin/config/services/cloudflare';
$message = t('No valid credentials have been entered for CloudFlare. Please go <a href="@link_to_settings">here</a> to set them.', ['@link_to_settings' => $link_to_settings]);

drupal_set_message($message, 'error');
\Drupal::logger('cloudflare')->error($message);
return FALSE;
}

return TRUE;
}

}

0 comments on commit 3d3657b

Please sign in to comment.