CacheRefresher.php in Optimizely 8.0
File
src/CacheRefresher.php
View source
<?php
namespace Drupal\optimizely;
class CacheRefresher {
public static function doRefresh($path_array, $original_path_array = NULL) {
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$cid_base = $protocol . '://' . $_SERVER['HTTP_HOST'] . '/';
if (isset($original_path_array)) {
$path_array = array_merge($path_array, $original_path_array);
}
foreach ($path_array as $path_count => $path) {
$recursive = NULL;
if (strpos($path, '*') !== 0) {
if (strpos($path, '<front>') === 0) {
$frontpage = \Drupal::config('system.site')
->get('page.front');
$frontpage = $frontpage ? $frontpage : 'node';
$cid = $cid_base . '/' . $frontpage;
$recursive = FALSE;
}
elseif (strpos($path, '/*') > 0) {
$cid = $cid_base . substr($path, 0, strlen($path) - 2);
$recursive = TRUE;
}
else {
$cid = $cid_base . $path;
$recursive = FALSE;
}
$cache = \Drupal::cache('render');
$recursive ? $cache
->deleteAll() : $cache
->delete($cid);
}
else {
$cache = \Drupal::cache('render');
$cache
->deleteAll();
break;
}
}
if (\Drupal::moduleHandler()
->moduleExists('varnish')) {
varnish_expire_cache($path_array);
drupal_set_message(t('Successfully purged cached page from Varnish.'));
}
drupal_set_message(t('"Render" cache has been cleared based on the project path settings.'), 'status');
}
}