state.drush.inc

  1. 8.0.x commands/core/state.drush.inc
  2. 7.x commands/core/state.drush.inc

Provides State commands.

Functions

Namesort descending Description
drush_state_delete State delete command callback.
drush_state_get State get command callback.
drush_state_set State set command callback.
state_drush_command Implementation of hook_drush_command().
state_drush_help Implementation of hook_drush_help().

File

commands/core/state.drush.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Provides State commands.
  5. */
  6. /**
  7. * Implementation of hook_drush_help().
  8. */
  9. function state_drush_help($section) {
  10. switch ($section) {
  11. case 'meta:state:title':
  12. return dt('State commands');
  13. case 'meta:state:summary':
  14. return dt('Interact with the State system.');
  15. }
  16. }
  17. /**
  18. * Implementation of hook_drush_command().
  19. */
  20. function state_drush_command() {
  21. $items['state-get'] = array(
  22. 'description' => 'Display a state value.',
  23. 'arguments' => array(
  24. 'key' => 'The key name.',
  25. ),
  26. 'required-arguments' => 1,
  27. 'examples' => array(
  28. 'drush state-get system.cron_last' => 'Displays last cron run timestamp',
  29. ),
  30. 'outputformat' => array(
  31. 'default' => 'json',
  32. 'pipe-format' => 'json',
  33. ),
  34. 'aliases' => array('sget'),
  35. 'core' => array('8+'),
  36. );
  37. $items['state-set'] = array(
  38. 'description' => 'Set a state value.',
  39. 'arguments' => array(
  40. 'key' => 'The state key, for example "system.cron_last".',
  41. 'value' => 'The value to assign to the state key. Use \'-\' to read from STDIN.',
  42. ),
  43. 'required arguments' => 2,
  44. 'options' => array(
  45. 'format' => array(
  46. 'description' => 'Format to parse the object. Use "string" for string (default), and "yaml" for YAML.',
  47. 'example-value' => 'yaml',
  48. 'value' => 'required',
  49. ),
  50. // A convenient way to pass a multiline value within a backend request.
  51. 'value' => array(
  52. 'description' => 'The value to assign to the state key (if any).',
  53. 'hidden' => TRUE,
  54. ),
  55. ),
  56. 'examples' => array(
  57. 'drush state-set system.cron_last 1406682882' => 'Sets a timestamp for last cron run.',
  58. ),
  59. 'aliases' => array('sset'),
  60. 'core' => array('8+'),
  61. );
  62. $items['state-delete'] = array(
  63. 'description' => 'Delete a state value.',
  64. 'arguments' => array(
  65. 'key' => 'The state key, for example "system.cron_last".',
  66. ),
  67. 'required arguments' => 1,
  68. 'examples' => array(
  69. 'drush state-del system.cron_last' => 'Delete state entry for system.cron_last.',
  70. ),
  71. 'aliases' => array('sdel'),
  72. 'core' => array('8+'),
  73. );
  74. return $items;
  75. }
  76. /**
  77. * State get command callback.
  78. *
  79. * @state $key
  80. * The state key.
  81. */
  82. function drush_state_get($key = NULL) {
  83. return \Drupal::state()->get($key);
  84. }
  85. /**
  86. * State set command callback.
  87. *
  88. * @param $key
  89. * The config key.
  90. * @param $data
  91. * The data to save to config.
  92. */
  93. function drush_state_set($key = NULL, $data = NULL) {
  94. // This hidden option is a convenient way to pass a value without passing a key.
  95. $data = drush_get_option('value', $data);
  96. if (!isset($data)) {
  97. return drush_set_error('DRUSH_STATE_ERROR', dt('No state value specified.'));
  98. }
  99. // Special flag indicating that the value has been passed via STDIN.
  100. if ($data === '-') {
  101. $data = stream_get_contents(STDIN);
  102. }
  103. \Drupal::state()->set($key, $data);
  104. }
  105. /**
  106. * State delete command callback.
  107. *
  108. * @state $key
  109. * The state key.
  110. */
  111. function drush_state_delete($key = NULL) {
  112. \Drupal::state()->delete($key);
  113. }