Skip to content

Commit

Permalink
Refactoring & renaming for consistency and clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
kepol committed Jul 6, 2024
1 parent d52e394 commit 5d5b59c
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 85 deletions.
2 changes: 1 addition & 1 deletion modules/quant_purger/src/Entity/QuantPurgerSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class QuantPurgerSettings extends PurgerSettingsBase implements PurgerSettingsIn
*
* @var string
*/
public $name = '';
public $name = 'Quant Purger';

/**
* The invalidation plugin ID that this purger invalidates.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace Drupal\quant_purger\Form;

/**
* Configuration form for the HTTP Bundled Purger.
* Configuration form for the Quant Purger.
*/
class QuantPurgeForm extends QuantPurgeFormBase {
class QuantPurgerConfigForm extends QuantPurgerConfigFormBase {

/**
* The token group names this purger supports replacing tokens for.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace Drupal\quant_purger\Form;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationsServiceInterface;
use Drupal\purge_ui\Form\PurgerConfigFormBase;
use Drupal\quant_purger\Entity\QuantPurgerSettings;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Abstract form base for HTTP based configurable purgers.
* Abstract form base for Quant Purger configuration.
*/
abstract class QuantPurgeFormBase extends PurgerConfigFormBase {
abstract class QuantPurgerConfigFormBase extends PurgerConfigFormBase {

/**
* The service that generates invalidation objects on-demand.
Expand All @@ -23,7 +23,7 @@ abstract class QuantPurgeFormBase extends PurgerConfigFormBase {
protected $purgeInvalidationFactory;

/**
* Constructs a \Drupal\quant_purger\Form\ConfigurationForm object.
* Constructs a base Quant Purger configuration form.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
Expand Down Expand Up @@ -122,7 +122,7 @@ public function buildFormPerformance(array &$form, FormStateInterface $form_stat
'#title' => $this->t('Cooldown time'),
'#default_value' => $settings->cooldown_time,
'#required' => TRUE,
'#description' => $this->t('Number of seconds to wait after a group of HTTP requests (so that other purgers get fresh content)'),
'#description' => $this->t('Number of seconds to wait after a group of HTTP requests so that other purgers get fresh content.'),
];
$form['performance']['max_requests'] = [
'#type' => 'number',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
use Drupal\purge_ui\Form\QueuerConfigFormBase;

/**
* Configuration form for the Quant queuer.
* Configuration form for the Quant Purger Queuer.
*/
class ConfigurationForm extends QueuerConfigFormBase {
class QuantPurgerQueuerConfigForm extends QueuerConfigFormBase {

/**
* {@inheritdoc}
Expand All @@ -23,6 +23,8 @@ protected function getEditableConfigNames() {
* {@inheritdoc}
*/
public function getFormId() {
// @todo This should be 'quant_purger.queuer_config_form' which will
// require an update hook.
return 'quant_purger.configuration_form';
}

Expand All @@ -39,7 +41,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];

$form['info'] = [
'#markup' => $this->t('<p class="messages messages--warning">After making changes to the Quant Purger configuration, all content must be re-seeded so the database reflects the changes.<p>'),
'#markup' => $this->t('<p class="messages messages--warning">After making changes to the Quant Purger Queuer configuration, all content must be re-seeded so the database reflects the changes.<p>'),
'#weight' => -10,
];

Expand Down Expand Up @@ -153,7 +155,7 @@ public function submitFormSuccess(array &$form, FormStateInterface $form_state)
->set('path_allowlist', $form_state->getValue('path_allowlist'))
->save();

\Drupal::messenger()->addMessage($this->t('Successfully saved the Quant Purger configuration. All content must be re-seeded so the database reflects the changes.'));
\Drupal::messenger()->addMessage($this->t('Successfully saved the Quant Purger Queuer configuration. All content must be re-seeded so the database reflects the changes.'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,75 @@
*
* @PurgePurger(
* id = "quant_purger",
* label = @Translation("QuantCDN Purger"),
* configform = "\Drupal\quant_purger\Form\QuantPurgeForm",
* label = @Translation("Quant Purger"),
* configform = "\Drupal\quant_purger\Form\QuantPurgerConfigForm",
* cooldown_time = 0.2,
* description = @Translation("Purger that sends invalidation expressions from your Drupal instance to the QuantCDN platform."),
* multi_instance = FALSE,
* types = {"tag", "everything", "path"},
* types = {"everything", "path", "tag"},
* )
*/
class QuantPurge extends QuantPurgeBase implements PurgerInterface {
class QuantPurger extends QuantPurgerBase implements PurgerInterface {

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

$filtered_validations = $this->processInvalidations($invalidations);
$processed = $this->processInvalidations($invalidations);

if ($filtered_validations['everything']) {
if ($processed['everything']) {
$this->invalidateEverything($invalidations);
return;
}

if (!empty($filtered_validations['tags'])) {
$this->invalidateTags($filtered_validations['tags']);
if (!empty($processed['paths'])) {
$this->invalidatePaths($processed['paths']);
}

if (!empty($filtered_validations['paths'])) {
$this->invalidatePaths($filtered_validations['paths']);
if (!empty($processed['tags'])) {
$this->invalidateTags($processed['tags']);
}

}

/**
* {@inheritdoc}
* Invalidate with the path '/*' to purge the entire project cache.
*
* @param array $invalidations
* This takes in an array of Invalidation objects, processing them all in a
* loop, generally from the purge queue.
*/
public function invalidateTags(array $invalidations) {
$tags = [];
foreach ($invalidations as $invalidation) {
$tags[] = Hash::cacheTags([$invalidation->getExpression()])[0];
}
public function invalidateEverything(array $invalidations) {

try {
$this->logger()->debug('[tags] Purging tags: ' . implode(' ', $tags));
$this->purgeTags($tags);
$invalidation->setState(InvalidationInterface::SUCCEEDED);
$this->logger()->debug('[everything] Purging entire site cache (/*)');
$this->purgePath('/*');
foreach ($invalidations as $invalidation) {
$invalidation->setState(InvalidationInterface::SUCCEEDED);
}
}
catch (\Exception $e) {
$this->logger()->notice('Error attempting to purge cache path: ' . $e->getMessage());
$this->logger()->notice('Error attempting to purge entire cache: ' . $e->getMessage());
error_log($e->getMessage());
$invalidation->setState(InvalidationInterface::FAILED);
foreach ($invalidations as $invalidation) {
$invalidation->setState(InvalidationInterface::FAILED);
}
}
}

/**
* Invalidate path-based invalidations in a loop.
* Invalidate path-based invalidations.
*
* @param array $invalidations
* This takes in an array of Invalidation, processing them all in a loop,
* generally from the purge queue.
* Array of Invalidation objects to process.
*/
public function invalidatePaths(array $invalidations) {

foreach ($invalidations as $invalidation) {
try {
$path = '/' . $invalidation->getExpression();
$this->logger()->debug('[path] Purging path invalidation: ' . $path);
$this->logger()->debug('[path] Purging path: ' . $path);
$this->purgePath($path);
$invalidation->setState(InvalidationInterface::SUCCEEDED);
}
Expand All @@ -88,27 +92,27 @@ public function invalidatePaths(array $invalidations) {
}

/**
* Invalidate with the path '/*' to purge the entire project cache.
* Invalidate tag-based invalidations.
*
* @param array $invalidations
* This takes in an array of Invalidation, processing them all in a loop,
* generally from the purge queue.
* Array of Invalidation objects to process.
*/
public function invalidateEverything(array $invalidations) {

public function invalidateTags(array $invalidations) {
try {
$this->logger()->debug('[everything] Purging entire site cache (/*)');
$this->purgePath('/*');
$this->logger()->debug('[tags] Purging tags: ' . implode(' ', $invalidations));

$tags = [];
foreach ($invalidations as $invalidation) {
$invalidation->setState(InvalidationInterface::SUCCEEDED);
$tags[] = Hash::cacheTags([$invalidation->getExpression()])[0];
}

$this->purgeTags($tags);
$invalidation->setState(InvalidationInterface::SUCCEEDED);
}
catch (\Exception $e) {
$this->logger()->notice('Error attempting to purge entire cache: ' . $e->getMessage());
$this->logger()->notice('Error attempting to purge cache path: ' . $e->getMessage());
error_log($e->getMessage());
foreach ($invalidations as $invalidation) {
$invalidation->setState(InvalidationInterface::FAILED);
}
$invalidation->setState(InvalidationInterface::FAILED);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
namespace Drupal\quant_purger\Plugin\Purge\Purger;

use Drupal\Core\Utility\Token;
use GuzzleHttp\ClientInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;
use Drupal\purge\Plugin\Purge\Purger\PurgerBase;
use Drupal\purge\Plugin\Purge\Purger\PurgerInterface;
use Drupal\quant_purger\Entity\QuantPurgerSettings;
use GuzzleHttp\ClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface;

/**
* Abstract base class for HTTP based configurable purgers.
* Abstract base class for Quant Purger.
*/
abstract class QuantPurgeBase extends PurgerBase implements PurgerInterface {
abstract class QuantPurgerBase extends PurgerBase implements PurgerInterface {

/**
* The Guzzle HTTP client.
Expand Down Expand Up @@ -105,51 +105,49 @@ public function getLabel() {
}

/**
* Returns array of tags and paths to purge in the provided array.
* Process invalidations based on type.
*
* @param array $invalidations
* The invalidations array.
*
* @return array
* The array of filtered tags and paths.
* The processed array with 'everything', 'paths' and 'tags' keys.
*/
public function processInvalidations(array $invalidations) {
$filtered_tags = [];
$filtered_paths = [];

$everything = FALSE;
$paths = [];
$tags = [];

foreach ($invalidations as $invalidation) {

if ($invalidation->getType() == 'tag') {
if ($invalidation->getType() == 'everything') {
// 'Everything' trumps everything and will issue a site-wide purge.
$invalidation->setState(InvalidationInterface::PROCESSING);
$filtered_tags[] = $invalidation;
$everything = TRUE;
$paths = [];
$tags = [];
break;
}
elseif ($invalidation->getType() == 'path') {
// @todo Not sure what this looks like.
$invalidation->setState(InvalidationInterface::PROCESSING);
$filtered_paths[] = $invalidation;
$paths[] = $invalidation;
}
elseif ($invalidation->getType() == 'everything') {
// 'Everything' trumps everything and will issue a site-wide purge.
elseif ($invalidation->getType() == 'tag') {
$invalidation->setState(InvalidationInterface::PROCESSING);
$everything = TRUE;
$filtered_paths = [];
$filtered_tags = [];
break;
$tags[] = $invalidation;
}
else {
$invalidation->setState(InvalidationInterface::NOT_SUPPORTED);
}
}

$filtered_array = [
$processed = [
'everything' => $everything,
'tags' => $filtered_tags,
'paths' => $filtered_paths,
'paths' => $paths,
'tags' => $tags,
];

return $filtered_array;
return $processed;
}

/**
Expand All @@ -170,9 +168,9 @@ public function getTimeHint() {
*/
public function getTypes() {
return [
"tag",
"everything",
"path",
'everything',
'path',
'tag',
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class QuantPurger implements CacheTagsInvalidatorInterface, ContainerAwareInterf
/**
* The queuer plugin or FALSE when the plugin is disabled.
*
* @var null|false|\Drupal\quant_purger\Plugin\Purge\Queuer\QuantPurgerPlugin
* @var null|false|\Drupal\quant_purger\Plugin\Purge\Queuer\QuantPurgerQueuer
*/
protected $queuer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
use Drupal\purge\Plugin\Purge\Queuer\QueuerInterface;

/**
* Quant purger.
* Quant Purger Queuer.
*
* @PurgeQueuer(
* id = "quant",
* label = @Translation("Purge Quant"),
* label = @Translation("Quant Purger Queuer"),
* description = @Translation("Queue impacted content updates."),
* enable_by_default = true,
* types = {"tag"},
* configform = "\Drupal\quant_purger\Form\ConfigurationForm",
* configform = "\Drupal\quant_purger\Form\QuantPurgerQueuerConfigForm",
* )
*/
class QuantPurgerPlugin extends QueuerBase implements QueuerInterface {}
// @todo Change id to 'quant_purger_queuer' which requires an update hook.
class QuantPurgerQueuer extends QueuerBase implements QueuerInterface {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace Drupal\quant_purger\Plugin\Purge\TagsHeader;

use Drupal\quant_purger\Entity\Hash;
use Drupal\purge\Plugin\Purge\TagsHeader\TagsHeaderInterface;
use Drupal\purge\Plugin\Purge\TagsHeader\TagsHeaderBase;
use Drupal\purge\Plugin\Purge\TagsHeader\TagsHeaderInterface;
use Drupal\quant_purger\Entity\Hash;

/**
* Sets and formats the default response header with cache tags.
*
* @PurgeTagsHeader(
* id = "quant_tagsheader",
* id = "quant_purger_tags_header",
* header_name = "Cache-Keys",
* )
*/
class QuantCacheTagsHeader extends TagsHeaderBase implements TagsHeaderInterface {
class QuantPurgerTagsHeader extends TagsHeaderBase implements TagsHeaderInterface {

/**
* {@inheritdoc}
Expand Down

0 comments on commit 5d5b59c

Please sign in to comment.