f1b0b3f29415af497cc42d73fc2d24b4771a28ea
[yaffs-website] / vendor / drush / drush / src / Drupal / Commands / core / StateCommands.php
1 <?php
2
3 namespace Drush\Drupal\Commands\core;
4
5 use Consolidation\OutputFormatters\StructuredData\PropertyList;
6 use Drupal\Core\State\StateInterface;
7 use Drush\Commands\DrushCommands;
8 use Symfony\Component\Yaml\Yaml;
9
10 class StateCommands extends DrushCommands
11 {
12
13     protected $state;
14
15     public function __construct(StateInterface $state)
16     {
17         $this->state = $state;
18     }
19
20     /**
21      * @return \Drupal\Core\State\StateInterface
22      */
23     public function getState()
24     {
25         return $this->state;
26     }
27
28     /**
29      * Display a state value.
30      *
31      * @command state:get
32      *
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
37      *
38      * @return \Consolidation\OutputFormatters\StructuredData\PropertyList
39      */
40     public function get($key, $options = ['format' => 'string'])
41     {
42         $value = $this->getState()->get($key);
43         return new PropertyList([$key => $value]);
44     }
45
46     /**
47      * Set a state value.
48      *
49      * @command state:set
50      *
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
63      *
64      * @return void
65      */
66     public function set($key, $value, $options = ['input-format' => 'auto', 'value' => self::REQ])
67     {
68         // A convenient way to pass a multiline value within a backend request.
69         $value = $options['value'] ?: $value;
70
71         if (!isset($value)) {
72             throw new \Exception(dt('No state value specified.'));
73         }
74
75         // Special flag indicating that the value has been passed via STDIN.
76         if ($value === '-') {
77             $value = stream_get_contents(STDIN);
78         }
79
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']);
84         }
85
86         $this->getState()->set($key, $value);
87     }
88
89     /**
90      * Delete a state entry.
91      *
92      * @command state:delete
93      *
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
98      *
99      * @return void
100      */
101     public function delete($key)
102     {
103         $this->getState()->delete($key);
104     }
105
106   /*
107    * Cast a value according to the provided format
108    *
109    * @param mixed $value.
110    * @param string $format
111    *   Allowed values: auto, integer, float, bool, boolean, json, yaml.
112    *
113    * @return $value
114    *  The value, casted as needed.
115    */
116     public static function format($value, $format)
117     {
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')) {
123                 $format = 'bool';
124             }
125         }
126
127         // Now, we parse the object.
128         switch ($format) {
129             case 'integer':
130                 $value = (integer)$value;
131                 break;
132             // from: http://php.net/gettype
133             // for historical reasons "double" is returned in case of a float, and not simply "float"
134             case 'double':
135             case 'float':
136                 $value = (float)$value;
137                 break;
138             case 'bool':
139             case 'boolean':
140                 if ($value == 'TRUE') {
141                      $value = true;
142                 } elseif ($value == 'FALSE') {
143                     $value = false;
144                 } else {
145                     $value = (bool)$value;
146                 }
147                 break;
148             case 'json':
149                 $value = json_decode($value, true);
150                 break;
151             case 'yaml':
152                 $value = Yaml::parse($value, false, true);
153                 break;
154         }
155         return $value;
156     }
157 }