diff --git a/pantheon_advanced_page_cache.info.yml b/pantheon_advanced_page_cache.info.yml index 7bfa63d..7925d13 100644 --- a/pantheon_advanced_page_cache.info.yml +++ b/pantheon_advanced_page_cache.info.yml @@ -3,3 +3,5 @@ description: 'Advanced page cache capabilities for Pantheon.' package: Performance and scalability type: module core_version_requirement: ^8 || ^9 +dependencies: + - purge:purge diff --git a/src/Plugin/Purge/Purger/PantheonAdvancedPageCachePurger.php b/src/Plugin/Purge/Purger/PantheonAdvancedPageCachePurger.php new file mode 100644 index 0000000..91a59dd --- /dev/null +++ b/src/Plugin/Purge/Purger/PantheonAdvancedPageCachePurger.php @@ -0,0 +1,125 @@ +get('logger.factory')->get('pantheon_advanced_page_cache'), + ); + } + + /** + * 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 \Psr\Log\LoggerInterface $logger + * A logger instance. + * + * @throws \Exception + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->logger = $logger; + } + + /** + * @inheritDoc + */ + public function invalidate(array $invalidations) { + // Build an array of these items to send to pantheon for purging. + $urls = []; + $tags = []; + + foreach ($invalidations as $invalidation) { + $invalidationType = $invalidations[0]->getPluginId(); + switch ($invalidationType) { + case 'tag': + // These pantheon functions don't exist outside of Pantheon Environments. + if (function_exists('pantheon_clear_edge_keys')) { + // Still developing this plugin, uncomment when we remove the event subscriber. + //$tags[] = $invalidation->getExpression(); + } + break; + case 'path': + // Didn't have any path type invalidations to test, so leaving this commented out. + // $urls[] = \Drupal::request()->getSchemeAndHttpHost() . $invalidation->getExpression(); + break; + case 'url': + // Don't try to invalidate files in the temporary directory. + if (!strpos($invalidation->getExpression(), 'system/temporary')) { + $urls[] = $invalidation->getExpression(); + } + break; + } + } + + try { + foreach ($urls as $url) { + // From Pantheon, a request + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url . '?' . time()); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_exec($ch); + curl_close($ch); + $this->logger->info('purged ' . $url); + } + if (function_exists('pantheon_clear_edge_keys')) { + pantheon_clear_edge_keys($tags); + } + } + catch (\Exception $e) { + $this->logger->error($e->getMessage()); + } + + foreach ($invalidations as $invalidation) { + $invalidation->setState(InvalidationInterface::SUCCEEDED); + } + } + +/** + * @inheritDoc + */ + public function hasRuntimeMeasurement() { + return true; + } +}