Skip to content
Snippets Groups Projects

Extend MaxAgeCheck to support http_cache_control

1 file
+ 22
3
Compare changes
  • Side-by-side
  • Inline
@@ -3,6 +3,7 @@
@@ -3,6 +3,7 @@
namespace Drupal\purge\Plugin\Purge\DiagnosticCheck;
namespace Drupal\purge\Plugin\Purge\DiagnosticCheck;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
 
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
/**
@@ -25,11 +26,20 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
@@ -25,11 +26,20 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
*/
*/
protected $config;
protected $config;
 
/**
 
* The module handler service.
 
*
 
* @var \Drupal\Core\Extension\ModuleHandlerInterface
 
*/
 
protected $moduleHandler;
 
/**
/**
* Construct a MaxAgeCheck object.
* Construct a MaxAgeCheck object.
*
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The factory for configuration objects.
* The factory for configuration objects.
 
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
 
* The module handler service.
* @param array $configuration
* @param array $configuration
* A configuration array containing information about the plugin instance.
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* @param string $plugin_id
@@ -37,9 +47,10 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
@@ -37,9 +47,10 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
* @param mixed $plugin_definition
* @param mixed $plugin_definition
* The plugin implementation definition.
* The plugin implementation definition.
*/
*/
final public function __construct(ConfigFactoryInterface $config_factory, array $configuration, $plugin_id, $plugin_definition) {
final public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->config = $config_factory->get('system.performance');
$this->config = $config_factory;
 
$this->moduleHandler = $module_handler;
}
}
/**
/**
@@ -48,6 +59,7 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
@@ -48,6 +59,7 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
return new static(
$container->get('config.factory'),
$container->get('config.factory'),
 
$container->get('module_handler'),
$configuration,
$configuration,
$plugin_id,
$plugin_id,
$plugin_definition
$plugin_definition
@@ -58,7 +70,14 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
@@ -58,7 +70,14 @@ class MaxAgeCheck extends DiagnosticCheckBase implements DiagnosticCheckInterfac
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public function run() {
public function run() {
$max_age = $this->config->get('cache.page.max_age');
// Avoid to get false positive when "s-maxage" is enabled
 
// by http_cache_control.
 
if ($this->moduleHandler->moduleExists('http_cache_control')) {
 
$max_age = $this->config->get('http_cache_control.settings')->get('cache.http.s_maxage');
 
}
 
else {
 
$max_age = $this->config->get('system.performance')->get('cache.page.max_age');
 
}
$this->value = $this->valueTranslatable($max_age);
$this->value = $this->valueTranslatable($max_age);
if ($max_age === 0) {
if ($max_age === 0) {
$this->recommendation = $this->t("Your site instructs external caching systems not to cache anything. Not only does this make cache invalidation futile, it is also a great danger to your website as any form of traffic can bring it down quickly!");
$this->recommendation = $this->t("Your site instructs external caching systems not to cache anything. Not only does this make cache invalidation futile, it is also a great danger to your website as any form of traffic can bring it down quickly!");
Loading