5 * Drush commands for Advanced CSS/JS Aggregation.
9 * Implements hook_drush_cache_clear().
11 function advagg_drush_cache_clear(&$types) {
12 // Add in Advanced CSS/JS Aggregation.
13 $types['advagg'] = 'drush_advagg_smart_cache_flush';
17 * Implements hook_drush_help().
19 function advagg_drush_help($command) {
21 case 'drush:advagg-cron':
22 return dt('Run the advagg cron hook. This will clear out all stale advagg aggregated files, remove aggregates that include missing files, and remove unused aggregates.');
24 case 'drush:advagg-clear-db-cache':
25 return dt('Remove all entries from the advagg cache bins. Useful if you suspect a cache is not getting cleared.');
27 case 'drush:advagg-clear-all-files':
28 return dt('Remove all generated files. Useful if you think some of the generated files got corrupted and thus need to be deleted.');
30 case 'drush:advagg-force-new-aggregates':
31 return dt('Force the creation of all new aggregates by incrementing a global counter. Current value of counter: %value. This is useful if a CDN has cached an aggregate incorrectly as it will force new ones to be used even if nothing else has changed.', ['%value' => advagg_get_global_counter()]);
36 * Implements hook_drush_command().
38 function advagg_drush_command() {
40 $items['advagg-cron'] = [
41 'description' => dt('Run the advagg cron hook.'),
43 'Standard example' => 'drush advagg-cron',
45 'aliases' => ['advagg-c'],
47 $items['advagg-clear-db-cache'] = [
48 'description' => dt('Remove all entries from the advagg cache bins.'),
50 'Standard example' => 'drush advagg-clear-db-cache',
52 'aliases' => ['advagg-cdc'],
54 $items['advagg-clear-all-files'] = [
55 'description' => dt('Remove all generated files.'),
57 'Standard example' => 'drush advagg-clear-all-files',
59 'aliases' => ['advagg-caf'],
61 $items['advagg-force-new-aggregates'] = [
62 'description' => dt('Force the creation of all new aggregates by incrementing a global counter.'),
64 'Standard example' => 'drush advagg-force-new-aggregates',
66 'aliases' => ['advagg-fna'],
72 * Callback function for drush advagg-force-new-aggregates.
74 * Callback is called by using drush_hook_command() where
75 * hook is the name of the module (advagg) and command is the name of
76 * the Drush command with all "-" characters converted to "_" characters.
78 function drush_advagg_force_new_aggregates() {
79 // Clear out the cache.
80 drush_advagg_clear_db_cache();
83 $config = \Drupal::service('config.factory')->getEditable('advagg.settings');
84 $new_value = $config->get('global_counter') + 1;
85 $config->set('global_counter', $new_value)->save();
86 drush_log(dt('Global counter is now set to @new_value', ['@new_value' => $new_value]), 'ok');
90 * Callback function for drush advagg-clear-all-files.
92 * Callback is called by using drush_hook_command() where
93 * hook is the name of the module (advagg) and command is the name of
94 * the Drush command with all "-" characters converted to "_" characters.
96 function drush_advagg_clear_all_files() {
98 $css_files = \Drupal::service('asset.css.collection_optimizer')->deleteAllReal();
99 $js_files = \Drupal::service('asset.js.collection_optimizer')->deleteAllReal();
101 // Report back the results.
102 drush_log(dt('All AdvAgg files have been deleted. @css_count CSS files and @js_count JS files have been removed.', [
103 '@css_count' => count($css_files),
104 '@js_count' => count($js_files),
109 * Callback function for drush advagg-clear-db-cache.
111 * Callback is called by using drush_hook_command() where
112 * hook is the name of the module (advagg) and command is the name of
113 * the Drush command with all "-" characters converted to "_" characters.
115 function drush_advagg_clear_db_cache() {
116 \Drupal::service('cache.advagg.minify')->deleteAll();
118 // Report back the results.
119 drush_log(dt('All AdvAgg cache bins have been cleared.'), 'ok');
123 * Callback function for drush advagg-cron.
125 * Callback is called by using drush_hook_command() where
126 * hook is the name of the module (advagg) and command is the name of
127 * the Drush command with all "-" characters converted to "_" characters.
129 function drush_advagg_cron() {
130 // Run AdvAgg cron job.
131 $output = advagg_cron(TRUE);
133 // Output results from running AssetCollectionOptimizer::deleteStale().
134 list($css_files, $js_files) = $output['stale'];
135 if (!empty($css_files) || !empty($js_files)) {
136 drush_log(dt('All stale aggregates have been deleted. %css_count CSS files and %js_count JS files have been removed.', [
137 '%css_count' => count($css_files),
138 '%js_count' => count($js_files),
142 drush_log(dt('No stale aggregates found. Nothing was deleted.'), 'ok');
147 * Flush the correct caches so CSS/JS changes go live.
149 function drush_advagg_smart_cache_flush() {
151 \Drupal::service('asset.js.collection_optimizer')->deleteAll();
152 \Drupal::service('asset.css.collection_optimizer')->deleteAll();
153 _drupal_flush_css_js();
154 drush_log('Advagg Aggregates Cache Cleared', 'ok');