AdvaggCommands.php in Advanced CSS/JS Aggregation 8.3
File
src/Commands/AdvaggCommands.php
View source
<?php
namespace Drupal\advagg\Commands;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\File\FileSystem;
use Drush\Commands\DrushCommands;
class AdvaggCommands extends DrushCommands {
protected $advaggConfig;
protected $cache;
protected $fileSystem;
public function __construct(CacheBackendInterface $cache, ConfigFactoryInterface $config_factory, FileSystem $file_system) {
$this->cache = $cache;
$this->advaggConfig = $config_factory
->getEditable('advagg.settings');
$this->fileSystem = $file_system;
}
public function cron() {
$output = advagg_cron(TRUE);
if (!empty($output['css']) || !empty($output['js'])) {
$this
->logger()
->notice(dt('All stale aggregates have been deleted. %css_count CSS files and %js_count JS files have been removed.', [
'%css_count' => count($output['css']),
'%js_count' => count($output['js']),
]));
}
else {
$this
->logger()
->notice(dt('No stale aggregates found. Nothing was deleted.'));
}
}
public function disable() {
$this->advaggConfig
->set('enabled', 0)
->save();
$this
->logger()
->notice(dt('All Advagg functionality is disabled.'));
}
public function enable() {
$this->advaggConfig
->set('enabled', 1)
->save();
$this
->logger()
->notice(dt('All Advagg functionality is enabled.'));
}
public function clearAllFiles() {
Cache::invalidateTags([
'library_info',
]);
$this->cache
->invalidateAll();
$pub = $this->fileSystem
->realpath('public://');
$css_count = count(glob($pub . '/css/optimized/*.css'));
$js_count = count(glob($pub . '/js/optimized/*.js'));
foreach ([
'public://js/optimized',
'public://css/optimized',
] as $path) {
if (file_exists($path)) {
file_unmanaged_delete_recursive($path);
}
}
$this
->logger()
->notice(dt('All AdvAgg optimized files have been deleted. %css_count CSS files and %js_count JS files have been removed.', [
'%css_count' => $css_count,
'%js_count' => $js_count,
]));
}
public function forceNewAggregates() {
$this
->clearAllFiles();
$new_value = $this->advaggConfig
->get('global_counter') + 1;
$this->advaggConfig
->set('global_counter', $new_value)
->save();
$this
->logger()
->notice(dt('Global counter is now set to @new_value', [
'@new_value' => $new_value,
]));
_drupal_flush_css_js();
}
}