class StateCommands

Namespace

Drush\CommandFiles\core

Hierarchy

Expanded class hierarchy of StateCommands

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
StateCommands::delete public function Delete a state entry.
StateCommands::get public function Display a state value.
StateCommands::set public function Set a state value.

File

lib/Drush/CommandFiles/core/StateCommands.php, line 7

View source
class StateCommands {

  /**
   * Display a state value.
   *
   * @command state-get
   *
   * @bootstrap DRUSH_BOOTSTRAP_DRUPAL_FULL
   * @param string $key The key name.
   * @usage drush state-get system.cron_last
   *   Displays last cron run timestamp
   * @aliases sget
   *
   * @return \Consolidation\OutputFormatters\StructuredData\AssociativeList
   */
  public function get($key, $options =['format' 'string' 'fields' '']) {
    $value = \Drupal::state()->get($key);
    return new AssociativeList([$key $value]);
  }

  /**
   * Set a state value.
   *
   * @command state-set
   *
   * @bootstrap DRUSH_BOOTSTRAP_DRUPAL_FULL
   * @param string $key The state key, for example: system.cron_last.
   * @param mixed $value The value to assign to the state key. Use '-' to read from STDIN.
   * @option input-format Type for the value. Defaults to 'auto'. Other recognized values: string, integer float, boolean, json, yaml.
   * @option value For internal use only.
   * @hidden-option value
   * @usage drush state-set system.cron_last 1406682882 --input-format=integer
   *  Sets a timestamp for last cron run.
   * @usage php -r "print json_encode(array(\'drupal\', \'simpletest\'));"  | drush state-set --input-format=json foo.name -
   *   Set a key to a complex value (e.g. array)
   * @aliases sset
   *
   * @return void
   */
  public function set($key, $value, $options =['input-format' 'auto' 'value' NULL]) {
    // A convenient way to pass a multiline value within a backend request.
    $value = $options['value'] ? : $value;

    if (!isset($value)) {
      throw new \Exception(dt('No state value specified.'));
      return NULL;
    }

    // Special flag indicating that the value has been passed via STDIN.
    if ($value === '-') {
      $value = stream_get_contents(STDIN);
    }

    // If the value is a string (usual case, unless we are called from code),
    // then format the input.
    if (is_string($value)) {
      $value = drush_value_format($value, $options['input-format']);
    }

    \Drupal::state()->set($key, $value);
  }

  /**
   * Delete a state entry.
   *
   * @command state-delete
   *
   * @bootstrap DRUSH_BOOTSTRAP_DRUPAL_FULL
   * @param string $key The state key, for example "system.cron_last".
   * @usage drush state-del system.cron_last
   *   Delete state entry for system.cron_last.
   * @aliases sdel
   *
   * @return void
   */
  public function delete($key) {
    \Drupal::state()->delete($key);
  }
}