Version 1
[yaffs-website] / web / modules / contrib / advagg / advagg.drush.inc
1 <?php
2
3 /**
4  * @file
5  * Drush commands for Advanced CSS/JS Aggregation.
6  */
7
8 /**
9  * Implements hook_drush_cache_clear().
10  */
11 function advagg_drush_cache_clear(&$types) {
12   // Add in Advanced CSS/JS Aggregation.
13   $types['advagg'] = 'drush_advagg_smart_cache_flush';
14 }
15
16 /**
17  * Implements hook_drush_help().
18  */
19 function advagg_drush_help($command) {
20   switch ($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.');
23
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.');
26
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.');
29
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()]);
32   }
33 }
34
35 /**
36  * Implements hook_drush_command().
37  */
38 function advagg_drush_command() {
39   $items = [];
40   $items['advagg-cron'] = [
41     'description' => dt('Run the advagg cron hook.'),
42     'examples' => [
43       'Standard example' => 'drush advagg-cron',
44     ],
45     'aliases' => ['advagg-c'],
46   ];
47   $items['advagg-clear-db-cache'] = [
48     'description' => dt('Remove all entries from the advagg cache bins.'),
49     'examples' => [
50       'Standard example' => 'drush advagg-clear-db-cache',
51     ],
52     'aliases' => ['advagg-cdc'],
53   ];
54   $items['advagg-clear-all-files'] = [
55     'description' => dt('Remove all generated files.'),
56     'examples' => [
57       'Standard example' => 'drush advagg-clear-all-files',
58     ],
59     'aliases' => ['advagg-caf'],
60   ];
61   $items['advagg-force-new-aggregates'] = [
62     'description' => dt('Force the creation of all new aggregates by incrementing a global counter.'),
63     'examples' => [
64       'Standard example' => 'drush advagg-force-new-aggregates',
65     ],
66     'aliases' => ['advagg-fna'],
67   ];
68   return $items;
69 }
70
71 /**
72  * Callback function for drush advagg-force-new-aggregates.
73  *
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.
77  */
78 function drush_advagg_force_new_aggregates() {
79   // Clear out the cache.
80   drush_advagg_clear_db_cache();
81
82   // Increment counter.
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');
87 }
88
89 /**
90  * Callback function for drush advagg-clear-all-files.
91  *
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.
95  */
96 function drush_advagg_clear_all_files() {
97   // Run the command.
98   $css_files = \Drupal::service('asset.css.collection_optimizer')->deleteAllReal();
99   $js_files = \Drupal::service('asset.js.collection_optimizer')->deleteAllReal();
100
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),
105   ]), 'ok');
106 }
107
108 /**
109  * Callback function for drush advagg-clear-db-cache.
110  *
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.
114  */
115 function drush_advagg_clear_db_cache() {
116   \Drupal::service('cache.advagg.minify')->deleteAll();
117
118   // Report back the results.
119   drush_log(dt('All AdvAgg cache bins have been cleared.'), 'ok');
120 }
121
122 /**
123  * Callback function for drush advagg-cron.
124  *
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.
128  */
129 function drush_advagg_cron() {
130   // Run AdvAgg cron job.
131   $output = advagg_cron(TRUE);
132
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),
139     ]), 'ok');
140   }
141   else {
142     drush_log(dt('No stale aggregates found. Nothing was deleted.'), 'ok');
143   }
144 }
145
146 /**
147  * Flush the correct caches so CSS/JS changes go live.
148  */
149 function drush_advagg_smart_cache_flush() {
150   // Run the commands.
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');
155 }