Yaffs site version 1.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         $debug = $input->getParameterOption(['--debug'], false);
155         $uri = $input->getParameterOption(['--uri', '-l']) ?: 'default';
156         if ($uri && !preg_match('/^(http|https):\/\//', $uri)) {
157             $uri = sprintf('http://%s', $uri);
158         }
159
160         $this->set('command', $input->getFirstArgument());
161         $this->set('root', $root);
162         $this->set('uri', $uri);
163         $this->set('debug', $debug);
164         $this->set('source', $source);
165         $this->set('target', $target);
166     }
167
168     /**
169      * @param $option
170      * @param $value
171      */
172     private function set($option, $value)
173     {
174         if ($value) {
175             $this->options[$option] = $value;
176
177             return;
178         }
179
180         if (!array_key_exists($option, $this->options)) {
181             unset($this->options[$option]);
182         }
183     }
184
185     /**
186      * @param $option
187      * @param null   $value
188      *
189      * @return string
190      */
191     public function get($option, $value = null)
192     {
193         if (!array_key_exists($option, $this->options)) {
194             return $value;
195         }
196
197         return $this->options[$option];
198     }
199
200     /**
201      * @return array
202      */
203     public function getAll()
204     {
205         return $this->options;
206     }
207
208     /**
209      * setOptionsAsArgv
210      */
211     public function setOptionsAsArgv()
212     {
213         foreach ($this->options as $optionName => $optionValue) {
214             if ($optionName == 'command') {
215                 continue;
216             }
217             $optionFound = false;
218             foreach ($_SERVER['argv'] as $key => $argv) {
219                 if (strpos($argv, '--'.$optionName) === 0) {
220                     $_SERVER['argv'][$key] = '--'.$optionName.'='.$optionValue;
221                     $optionFound = true;
222                     break;
223                 }
224             }
225             if (!$optionFound) {
226                 $_SERVER['argv'][] = '--'.$optionName.'='.$optionValue;
227             }
228         }
229     }
230
231     /**
232      * @return array
233      */
234     public function restoreOriginalArgvValues()
235     {
236         return $_SERVER['argv'] = $this->originalArgvValues;
237     }
238 }