cache.drush.inc

  1. 8.0.x commands/core/cache.drush.inc
  2. 6.x commands/core/cache.drush.inc
  3. 7.x commands/core/cache.drush.inc
  4. 4.x commands/core/cache.drush.inc
  5. 5.x commands/core/cache.drush.inc
  6. master commands/core/cache.drush.inc

Functions

Namesort descending Description
cache_cache_clear_complete Command argument complete callback.
cache_drush_command Implementation of hook_drush_command().
cache_drush_help Implementation of hook_drush_help().
drush_cache_clear_drush Clear caches internal to drush core.
drush_cache_clear_pre_validate
drush_cache_clear_types All types of caches available for clearing. Contrib commands can alter in their own.
drush_cache_command_clear Command callback for drush cache-clear.
drush_cache_command_get Print an object returned from the cache.
drush_cache_command_set Set an object in the cache.
drush_cache_rebuild Rebuild a Drupal 8 site.
drush_cache_set_prepare_data

File

commands/core/cache.drush.inc
View source
  1. <?php
  2. use Drush\Log\LogLevel;
  3. use Drupal\Core\DrupalKernel;
  4. use Drupal\Core\Site\Settings;
  5. use Symfony\Component\HttpFoundation\Request;
  6. /**
  7. * Implementation of hook_drush_help().
  8. */
  9. function cache_drush_help($section) {
  10. switch ($section) {
  11. case 'meta:cache:title':
  12. return dt('Cache commands');
  13. case 'meta:cache:summary':
  14. return dt('Interact with Drupal\'s cache API.');
  15. }
  16. }
  17. /**
  18. * Implementation of hook_drush_command().
  19. */
  20. function cache_drush_command() {
  21. $items = array();
  22. // We specify command callbacks here because the defaults would collide with
  23. // the drush cache api functions.
  24. $items['cache-get'] = array(
  25. 'description' => 'Fetch a cached object and display it.',
  26. 'examples' => array(
  27. 'drush cache-get schema' => 'Display the data for the cache id "schema" from the "cache" bin.',
  28. 'drush cache-get update_available_releases update' => 'Display the data for the cache id "update_available_releases" from the "update" bin.',
  29. ),
  30. 'arguments' => array(
  31. 'cid' => 'The id of the object to fetch.',
  32. 'bin' => 'Optional. The cache bin to fetch from.',
  33. ),
  34. 'required-arguments' => 1,
  35. 'callback' => 'drush_cache_command_get',
  36. 'outputformat' => array(
  37. 'default' => 'print-r',
  38. 'pipe-format' => 'var_export',
  39. 'output-data-type' => TRUE,
  40. ),
  41. 'aliases' => array('cg'),
  42. );
  43. $items['cache-clear'] = array(
  44. 'bootstrap' => DRUSH_BOOTSTRAP_MAX,
  45. 'description' => 'Clear a specific cache, or all drupal caches.',
  46. 'arguments' => array(
  47. 'type' => 'The particular cache to clear. Omit this argument to choose from available caches.',
  48. ),
  49. 'callback' => 'drush_cache_command_clear',
  50. 'aliases' => array('cc'),
  51. );
  52. $items['cache-set'] = array(
  53. 'description' => 'Cache an object expressed in JSON or var_export() format.',
  54. 'arguments' => array(
  55. 'cid' => 'The id of the object to set.',
  56. 'data' => 'The object to set in the cache. Use \'-\' to read the object from STDIN.',
  57. 'bin' => 'Optional. The cache bin to store the object in.',
  58. 'expire' => 'Optional. CACHE_PERMANENT, CACHE_TEMPORARY, or a Unix timestamp.',
  59. 'tags' => 'An array of cache tags.',
  60. ),
  61. 'required-arguments' => 2,
  62. 'options' => array(
  63. // Note that this is not an outputformat option.
  64. 'format' => 'Format to parse the object. Use "string" for string (default), and "json" for JSON.',
  65. 'cache-get' => 'If the object is the result a previous fetch from the cache, only store the value in the "data" property of the object in the cache.',
  66. ),
  67. 'callback' => 'drush_cache_command_set',
  68. 'aliases' => array('cs'),
  69. );
  70. $items['cache-rebuild'] = array(
  71. 'description' => 'Rebuild a Drupal 8 site and clear all its caches.',
  72. 'options' => array(),
  73. 'arguments' => array(),
  74. // Bootstrap to DRUSH_BOOTSTAP_DRUPAL_SITE to pick the correct site.
  75. // Further bootstrap is done by the rebuild script.
  76. 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_SITE,
  77. 'core' => array('8+'),
  78. 'aliases' => array('cr', 'rebuild'),
  79. );
  80. return $items;
  81. }
  82. /**
  83. * Command argument complete callback.
  84. *
  85. * @return
  86. * Array of clear types.
  87. */
  88. function cache_cache_clear_complete() {
  89. // Bootstrap as far as possible so that Views and others can list their caches.
  90. drush_bootstrap_max();
  91. return array('values' => array_keys(drush_cache_clear_types(TRUE)));
  92. }
  93. function drush_cache_clear_pre_validate($type = NULL) {
  94. $types = drush_cache_clear_types(drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL));
  95. // Check if the provided type ($type) is a valid cache type.
  96. if ($type && !array_key_exists($type, $types)) {
  97. if ($type === 'all' && drush_drupal_major_version() >= 8) {
  98. return drush_set_error(dt('`cache-clear all` is deprecated for Drupal 8 and later. Please use the `cache-rebuild` command instead.'));
  99. }
  100. // If we haven't done a full bootstrap, provide a more
  101. // specific message with instructions to the user on
  102. // bootstrapping a Drupal site for more options.
  103. if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  104. $all_types = drush_cache_clear_types(TRUE);
  105. if (array_key_exists($type, $all_types)) {
  106. return drush_set_error(dt("'!type' cache requires a working Drupal site to operate on. Use the --root and --uri options, or a site @alias, or cd to a directory containing a Drupal settings.php file.", array('!type' => $type)));
  107. }
  108. else {
  109. return drush_set_error(dt("'!type' cache is not a valid cache type. There may be more cache types available if you select a working Drupal site.", array('!type' => $type)));
  110. }
  111. }
  112. return drush_set_error(dt("'!type' cache is not a valid cache type.", array('!type' => $type)));
  113. }
  114. }
  115. /**
  116. * Command callback for drush cache-clear.
  117. */
  118. function drush_cache_command_clear($type = NULL) {
  119. if (!drush_get_option('cache-clear', TRUE)) {
  120. drush_log(dt("Skipping cache-clear operation due to --cache-clear=0 option."), LogLevel::OK);
  121. return TRUE;
  122. }
  123. $types = drush_cache_clear_types(drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL));
  124. if (!isset($type)) {
  125. // Don't offer 'all' unless Drush has bootstrapped the Drupal site
  126. if (!drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  127. unset($types['all']);
  128. }
  129. $type = drush_choice($types, 'Enter a number to choose which cache to clear.', '!key');
  130. if (empty($type)) {
  131. return drush_user_abort();
  132. }
  133. }
  134. // Do it.
  135. drush_op($types[$type]);
  136. if ($type == 'all' && !drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
  137. drush_log(dt("No Drupal site found, only 'drush' cache was cleared."), LogLevel::WARNING);
  138. }
  139. else {
  140. drush_log(dt("'!name' cache was cleared.", array('!name' => $type)), LogLevel::SUCCESS);
  141. }
  142. }
  143. /**
  144. * Print an object returned from the cache.
  145. *
  146. * @param $cid
  147. * The cache ID of the object to fetch.
  148. * @param $bin
  149. * A specific bin to fetch from. If not specified, the default bin is used.
  150. */
  151. function drush_cache_command_get($cid = NULL, $bin = NULL) {
  152. drush_include_engine('drupal', 'cache');
  153. $result = drush_op('_drush_cache_command_get', $cid, $bin);
  154. if (empty($result)) {
  155. return drush_set_error('DRUSH_CACHE_OBJECT_NOT_FOUND', dt('The !cid object in the !bin bin was not found.', array('!cid' => $cid, '!bin' => $bin ? $bin : _drush_cache_bin_default())));
  156. }
  157. return $result;
  158. }
  159. /**
  160. * Set an object in the cache.
  161. *
  162. * @param $cid
  163. * The cache ID of the object to fetch.
  164. * @param $data
  165. * The data to save to the cache, or '-' to read from STDIN.
  166. * @param $bin
  167. * A specific bin to fetch from. If not specified, the default bin is used.
  168. * @param $expire
  169. * The expiry timestamp for the cached object.
  170. * @param $tags
  171. * Cache tags for the cached object.
  172. */
  173. function drush_cache_command_set($cid = NULL, $data = '', $bin = NULL, $expire = NULL, $tags = array()) {
  174. // In addition to prepare, this also validates. Can't easily be in own validate callback as
  175. // reading once from STDIN empties it.
  176. $data = drush_cache_set_prepare_data($data);
  177. if ($data === FALSE && drush_get_error()) {
  178. // An error was logged above.
  179. return;
  180. }
  181. drush_include_engine('drupal', 'cache');
  182. return drush_op('_drush_cache_command_set', $cid, $data, $bin, $expire, $tags);
  183. }
  184. function drush_cache_set_prepare_data($data) {
  185. if ($data == '-') {
  186. $data = file_get_contents("php://stdin");
  187. }
  188. // Now, we parse the object.
  189. switch (drush_get_option('format', 'string')) {
  190. case 'json':
  191. $data = drush_json_decode($data);
  192. break;
  193. }
  194. if (drush_get_option('cache-get')) {
  195. // $data might be an object.
  196. if (is_object($data) && $data->data) {
  197. $data = $data->data;
  198. }
  199. // But $data returned from `drush cache-get --format=json` will be an array.
  200. elseif (is_array($data) && isset($data['data'])) {
  201. $data = $data['data'];
  202. }
  203. else {
  204. // If $data is neither object nor array and cache-get was specified, then
  205. // there is a problem.
  206. return drush_set_error('CACHE_INVALID_FORMAT', dt("'cache-get' was specified as an option, but the data is neither an object or an array."));
  207. }
  208. }
  209. return $data;
  210. }
  211. /**
  212. * All types of caches available for clearing. Contrib commands can alter in their own.
  213. */
  214. function drush_cache_clear_types($include_bootstrapped_types = FALSE) {
  215. drush_include_engine('drupal', 'cache');
  216. $types = _drush_cache_clear_types($include_bootstrapped_types);
  217. // Include the appropriate environment engine, so callbacks can use core
  218. // version specific cache clearing functions directly.
  219. drush_include_engine('drupal', 'environment');
  220. // Command files may customize $types as desired.
  221. drush_command_invoke_all_ref('drush_cache_clear', $types, $include_bootstrapped_types);
  222. return $types;
  223. }
  224. /**
  225. * Clear caches internal to drush core.
  226. */
  227. function drush_cache_clear_drush() {
  228. drush_cache_clear_all(NULL, 'default'); // commandfiles, etc.
  229. drush_cache_clear_all(NULL, 'complete'); // completion
  230. // Release XML. We don't clear tarballs since those never change.
  231. $matches = drush_scan_directory(drush_directory_cache('download'), "/^https---updates.drupal.org-release-history/", array('.', '..'));
  232. array_map('unlink', array_keys($matches));
  233. }
  234. /**
  235. * Rebuild a Drupal 8 site.
  236. *
  237. * This is a transpose of core/rebuild.php. Additionally
  238. * it also clears drush cache and drupal render cache.
  239. */
  240. function drush_cache_rebuild() {
  241. if (!drush_get_option('cache-clear', TRUE)) {
  242. drush_log(dt("Skipping cache-clear operation due to --cache-clear=0 option."), LogLevel::OK);
  243. return TRUE;
  244. }
  245. chdir(DRUPAL_ROOT);
  246. // Clear the APC cache to ensure APC class loader is reset.
  247. if (function_exists('apc_fetch')) {
  248. apc_clear_cache('user');
  249. }
  250. $autoloader = drush_drupal_load_autoloader(DRUPAL_ROOT);
  251. require_once DRUSH_DRUPAL_CORE . '/includes/utility.inc';
  252. $request = Request::createFromGlobals();
  253. // Manually resemble early bootstrap of DrupalKernel::boot().
  254. require_once DRUSH_DRUPAL_CORE . '/includes/bootstrap.inc';
  255. DrupalKernel::bootEnvironment();
  256. // Avoid 'Only variables should be passed by reference'
  257. $root = DRUPAL_ROOT;
  258. $site_path = DrupalKernel::findSitePath($request);
  259. Settings::initialize($root, $site_path, $autoloader);
  260. // Use our error handler since _drupal_log_error() depends on an unavailable theme system (ugh).
  261. set_error_handler('drush_error_handler');
  262. // drupal_rebuild() calls drupal_flush_all_caches() itself, so we don't do it manually.
  263. drupal_rebuild($autoloader, $request);
  264. drush_log(dt('Cache rebuild complete.'), LogLevel::OK);
  265. // As this command replaces `drush cache-clear all` for Drupal 8 users, clear
  266. // the Drush cache as well, for consistency with that behavior.
  267. drush_cache_clear_drush();
  268. }