3 namespace Drush\Drupal\Commands\core;
5 use Consolidation\OutputFormatters\StructuredData\PropertyList;
6 use Drupal\Core\State\StateInterface;
7 use Drush\Commands\DrushCommands;
8 use Symfony\Component\Yaml\Yaml;
10 class StateCommands extends DrushCommands
15 public function __construct(StateInterface $state)
17 $this->state = $state;
21 * @return \Drupal\Core\State\StateInterface
23 public function getState()
29 * Display a state value.
33 * @param string $key The key name.
34 * @usage drush state:get system.cron_last
35 * Displays last cron run timestamp
36 * @aliases sget,state-get
38 * @return \Consolidation\OutputFormatters\StructuredData\PropertyList
40 public function get($key, $options = ['format' => 'string'])
42 $value = $this->getState()->get($key);
43 return new PropertyList([$key => $value]);
51 * @param string $key The state key, for example: system.cron_last.
52 * @param mixed $value The value to assign to the state key. Use '-' to read from STDIN.
53 * @option input-format Type for the value. Defaults to 'auto'. Other recognized values: string, integer float, boolean, json, yaml.
54 * @option value For internal use only.
55 * @hidden-options value
56 * @usage drush sset system.maintenance_mode 1 --input-format=integer
57 * Put site into Maintenance mode.
58 * @usage drush state:set system.cron_last 1406682882 --input-format=integer
59 * Sets a timestamp for last cron run.
60 * @usage php -r "print json_encode(array(\'drupal\', \'simpletest\'));" | drush state-set --input-format=json foo.name -
61 * Set a key to a complex value (e.g. array)
62 * @aliases sset,state-set
66 public function set($key, $value, $options = ['input-format' => 'auto', 'value' => self::REQ])
68 // A convenient way to pass a multiline value within a backend request.
69 $value = $options['value'] ?: $value;
72 throw new \Exception(dt('No state value specified.'));
75 // Special flag indicating that the value has been passed via STDIN.
77 $value = stream_get_contents(STDIN);
80 // If the value is a string (usual case, unless we are called from code),
81 // then format the input.
82 if (is_string($value)) {
83 $value = $this->format($value, $options['input-format']);
86 $this->getState()->set($key, $value);
90 * Delete a state entry.
92 * @command state:delete
94 * @param string $key The state key, for example "system.cron_last".
95 * @usage drush state:del system.cron_last
96 * Delete state entry for system.cron_last.
97 * @aliases sdel,state-delete
101 public function delete($key)
103 $this->getState()->delete($key);
107 * Cast a value according to the provided format
109 * @param mixed $value.
110 * @param string $format
111 * Allowed values: auto, integer, float, bool, boolean, json, yaml.
114 * The value, casted as needed.
116 public static function format($value, $format)
118 if ($format == 'auto') {
119 if (is_numeric($value)) {
120 $value = $value + 0; // http://php.net/manual/en/function.is-numeric.php#107326
121 $format = gettype($value);
122 } elseif (($value == 'TRUE') || ($value == 'FALSE')) {
127 // Now, we parse the object.
130 $value = (integer)$value;
132 // from: http://php.net/gettype
133 // for historical reasons "double" is returned in case of a float, and not simply "float"
136 $value = (float)$value;
140 if ($value == 'TRUE') {
142 } elseif ($value == 'FALSE') {
145 $value = (bool)$value;
149 $value = json_decode($value, true);
152 $value = Yaml::parse($value, false, true);