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' => 'Type for the value. Use "auto" to detect format from value. Other recognized values are string, integer float, or boolean for corresponding primitive type, or json, yaml for complex types.',
  47. 'example-value' => 'boolean',
  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 --format=integer' => 'Sets a timestamp for last cron run.',
  58. 'php -r "print json_encode(array(\'drupal\', \'simpletest\'));" | drush state-set --format=json foo.name -'=> 'Set a key to a complex value (e.g. array)',
  59. ),
  60. 'aliases' => array('sset'),
  61. 'core' => array('8+'),
  62. );
  63. $items['state-delete'] = array(
  64. 'description' => 'Delete a state value.',
  65. 'arguments' => array(
  66. 'key' => 'The state key, for example "system.cron_last".',
  67. ),
  68. 'required arguments' => 1,
  69. 'examples' => array(
  70. 'drush state-del system.cron_last' => 'Delete state entry for system.cron_last.',
  71. ),
  72. 'aliases' => array('sdel'),
  73. 'core' => array('8+'),
  74. );
  75. return $items;
  76. }
  77. /**
  78. * State get command callback.
  79. *
  80. * @state $key
  81. * The state key.
  82. */
  83. function drush_state_get($key = NULL) {
  84. return \Drupal::state()->get($key);
  85. }
  86. /**
  87. * State set command callback.
  88. *
  89. * @param $key
  90. * The config key.
  91. * @param $value
  92. * The data to save to state.
  93. */
  94. function drush_state_set($key = NULL, $value = NULL) {
  95. // This hidden option is a convenient way to pass a value without passing a key.
  96. $value = drush_get_option('value', $value);
  97. if (!isset($value)) {
  98. return drush_set_error('DRUSH_STATE_ERROR', dt('No state value specified.'));
  99. }
  100. // Special flag indicating that the value has been passed via STDIN.
  101. if ($value === '-') {
  102. $value = stream_get_contents(STDIN);
  103. }
  104. // If the value is a string (usual case, unless we are called from code),
  105. // then format the input.
  106. if (is_string($value)) {
  107. $value = drush_value_format($value, drush_get_option('format', 'auto'));
  108. }
  109. \Drupal::state()->set($key, $value);
  110. }
  111. /**
  112. * State delete command callback.
  113. *
  114. * @state $key
  115. * The state key.
  116. */
  117. function drush_state_delete($key = NULL) {
  118. \Drupal::state()->delete($key);
  119. }