Version 1
[yaffs-website] / vendor / drupal / console-core / src / Utils / ArgvInputReader.php
1 <?php
2 /**
3  * @file
4  * Contains \Drupal\Console\Core\Utils\ArgvInputReader.
5  */
6
7 namespace Drupal\Console\Core\Utils;
8
9 use Symfony\Component\Console\Input\ArgvInput;
10
11 /**
12  * Class ArgvInputReader.
13  */
14 class ArgvInputReader
15 {
16     /**
17      * @var array
18      */
19     protected $options;
20
21     /**
22      * @var
23      */
24     protected $input;
25
26     /**
27      * @var array
28      */
29     protected $originalArgvValues;
30
31     /**
32      * ArgvInputReader constructor.
33      */
34     public function __construct()
35     {
36         $this->originalArgvValues = $_SERVER['argv'];
37         $this->options = [];
38         $this->setOptionsFromPlaceHolders();
39         $this->readArgvInputValues();
40     }
41
42     /**
43      * @param array $targetConfig
44      */
45     public function setOptionsFromTargetConfiguration($targetConfig)
46     {
47         $options = [];
48         if (array_key_exists('root', $targetConfig)) {
49             $options['root'] = $targetConfig['root'];
50         }
51         if (array_key_exists('uri', $targetConfig)) {
52             $options['uri'] = $targetConfig['uri'];
53         }
54
55         if (array_key_exists('remote', $targetConfig)) {
56             $this->set('remote', true);
57         }
58
59         $this->setArgvOptions($options);
60     }
61
62     /**
63      * @param array $options
64      */
65     public function setOptionsFromConfiguration($options)
66     {
67         $this->setArgvOptions($options);
68     }
69
70     /**
71      * @param $options
72      */
73     private function setArgvOptions($options)
74     {
75         $argvInput = new ArgvInput();
76         foreach ($options as $key => $option) {
77             if (!$option) {
78                 continue;
79             }
80
81             if (!$argvInput->hasParameterOption($key)) {
82                 if ($option == 1) {
83                     $_SERVER['argv'][] = sprintf('--%s', $key);
84                 } else {
85                     $_SERVER['argv'][] = sprintf('--%s=%s', $key, $option);
86                 }
87                 continue;
88             }
89             if ($key === 'root') {
90                 $option = sprintf(
91                     '%s%s',
92                     $argvInput->getParameterOption(['--root'], null),
93                     $option
94                 );
95             }
96             foreach ($_SERVER['argv'] as $argvKey => $argv) {
97                 if (strpos($argv, '--'.$key) === 0) {
98                     if ($option == 1) {
99                         $_SERVER['argv'][$argvKey] = sprintf('--%s', $key);
100                     } else {
101                         $_SERVER['argv'][$argvKey] = sprintf(
102                             '--%s=%s',
103                             $key,
104                             $option
105                         );
106                     }
107                     continue;
108                 }
109             }
110         }
111         $this->readArgvInputValues();
112     }
113
114     /**
115      * setOptionsFromPlaceHolders.
116      */
117     private function setOptionsFromPlaceHolders()
118     {
119         if (count($_SERVER['argv']) > 2
120             && stripos($_SERVER['argv'][1], '@') === 0
121             && stripos($_SERVER['argv'][2], '@') === 0
122         ) {
123             $_SERVER['argv'][1] = sprintf(
124                 '--source=%s',
125                 substr($_SERVER['argv'][1], 1)
126             );
127
128             $_SERVER['argv'][2] = sprintf(
129                 '--target=%s',
130                 substr($_SERVER['argv'][2], 1)
131             );
132
133             return;
134         }
135
136         if (count($_SERVER['argv']) > 1 && stripos($_SERVER['argv'][1], '@') === 0) {
137             $_SERVER['argv'][1] = sprintf(
138                 '--target=%s',
139                 substr($_SERVER['argv'][1], 1)
140             );
141         }
142     }
143
144     /**
145      * ReadArgvInputValues.
146      */
147     private function readArgvInputValues()
148     {
149         $input = new ArgvInput();
150
151         $source = $input->getParameterOption(['--source', '-s'], null);
152         $target = $input->getParameterOption(['--target', '-t'], null);
153         $root = $input->getParameterOption(['--root'], null);
154         $uri = $input->getParameterOption(['--uri', '-l']) ?: 'default';
155         if ($uri && !preg_match('/^(http|https):\/\//', $uri)) {
156             $uri = sprintf('http://%s', $uri);
157         }
158
159         $this->set('command', $input->getFirstArgument());
160         $this->set('root', $root);
161         $this->set('uri', $uri);
162         $this->set('source', $source);
163         $this->set('target', $target);
164     }
165
166     /**
167      * @param $option
168      * @param $value
169      */
170     private function set($option, $value)
171     {
172         if ($value) {
173             $this->options[$option] = $value;
174
175             return;
176         }
177
178         if (!array_key_exists($option, $this->options)) {
179             unset($this->options[$option]);
180         }
181     }
182
183     /**
184      * @param $option
185      * @param null   $value
186      *
187      * @return string
188      */
189     public function get($option, $value = null)
190     {
191         if (!array_key_exists($option, $this->options)) {
192             return $value;
193         }
194
195         return $this->options[$option];
196     }
197
198     /**
199      * @return array
200      */
201     public function getAll()
202     {
203         return $this->options;
204     }
205
206     /**
207      * setOptionsAsArgv
208      */
209     public function setOptionsAsArgv()
210     {
211         foreach ($this->options as $optionName => $optionValue) {
212             if ($optionName == 'command') {
213                 continue;
214             }
215             $optionFound = false;
216             foreach ($_SERVER['argv'] as $key => $argv) {
217                 if (strpos($argv, '--'.$optionName) === 0) {
218                     $_SERVER['argv'][$key] = '--'.$optionName.'='.$optionValue;
219                     $optionFound = true;
220                     break;
221                 }
222             }
223             if (!$optionFound) {
224                 $_SERVER['argv'][] = '--'.$optionName.'='.$optionValue;
225             }
226         }
227     }
228
229     /**
230      * @return array
231      */
232     public function restoreOriginalArgvValues()
233     {
234         return $_SERVER['argv'] = $this->originalArgvValues;
235     }
236 }