[ Index ] |
PHP Cross Reference of phpBB-3.2.11-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /* 4 * This file is part of the Symfony package. 5 * 6 * (c) Fabien Potencier <fabien@symfony.com> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12 namespace Symfony\Component\Console\Input; 13 14 use Symfony\Component\Console\Exception\RuntimeException; 15 16 /** 17 * ArgvInput represents an input coming from the CLI arguments. 18 * 19 * Usage: 20 * 21 * $input = new ArgvInput(); 22 * 23 * By default, the `$_SERVER['argv']` array is used for the input values. 24 * 25 * This can be overridden by explicitly passing the input values in the constructor: 26 * 27 * $input = new ArgvInput($_SERVER['argv']); 28 * 29 * If you pass it yourself, don't forget that the first element of the array 30 * is the name of the running application. 31 * 32 * When passing an argument to the constructor, be sure that it respects 33 * the same rules as the argv one. It's almost always better to use the 34 * `StringInput` when you want to provide your own input. 35 * 36 * @author Fabien Potencier <fabien@symfony.com> 37 * 38 * @see http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html 39 * @see http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap12.html#tag_12_02 40 */ 41 class ArgvInput extends Input 42 { 43 private $tokens; 44 private $parsed; 45 46 /** 47 * @param array|null $argv An array of parameters from the CLI (in the argv format) 48 * @param InputDefinition|null $definition A InputDefinition instance 49 */ 50 public function __construct(array $argv = null, InputDefinition $definition = null) 51 { 52 if (null === $argv) { 53 $argv = $_SERVER['argv']; 54 } 55 56 // strip the application name 57 array_shift($argv); 58 59 $this->tokens = $argv; 60 61 parent::__construct($definition); 62 } 63 64 protected function setTokens(array $tokens) 65 { 66 $this->tokens = $tokens; 67 } 68 69 /** 70 * {@inheritdoc} 71 */ 72 protected function parse() 73 { 74 $parseOptions = true; 75 $this->parsed = $this->tokens; 76 while (null !== $token = array_shift($this->parsed)) { 77 if ($parseOptions && '' == $token) { 78 $this->parseArgument($token); 79 } elseif ($parseOptions && '--' == $token) { 80 $parseOptions = false; 81 } elseif ($parseOptions && 0 === strpos($token, '--')) { 82 $this->parseLongOption($token); 83 } elseif ($parseOptions && '-' === $token[0] && '-' !== $token) { 84 $this->parseShortOption($token); 85 } else { 86 $this->parseArgument($token); 87 } 88 } 89 } 90 91 /** 92 * Parses a short option. 93 * 94 * @param string $token The current token 95 */ 96 private function parseShortOption($token) 97 { 98 $name = substr($token, 1); 99 100 if (\strlen($name) > 1) { 101 if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) { 102 // an option with a value (with no space) 103 $this->addShortOption($name[0], substr($name, 1)); 104 } else { 105 $this->parseShortOptionSet($name); 106 } 107 } else { 108 $this->addShortOption($name, null); 109 } 110 } 111 112 /** 113 * Parses a short option set. 114 * 115 * @param string $name The current token 116 * 117 * @throws RuntimeException When option given doesn't exist 118 */ 119 private function parseShortOptionSet($name) 120 { 121 $len = \strlen($name); 122 for ($i = 0; $i < $len; ++$i) { 123 if (!$this->definition->hasShortcut($name[$i])) { 124 $encoding = mb_detect_encoding($name, null, true); 125 throw new RuntimeException(sprintf('The "-%s" option does not exist.', false === $encoding ? $name[$i] : mb_substr($name, $i, 1, $encoding))); 126 } 127 128 $option = $this->definition->getOptionForShortcut($name[$i]); 129 if ($option->acceptValue()) { 130 $this->addLongOption($option->getName(), $i === $len - 1 ? null : substr($name, $i + 1)); 131 132 break; 133 } else { 134 $this->addLongOption($option->getName(), null); 135 } 136 } 137 } 138 139 /** 140 * Parses a long option. 141 * 142 * @param string $token The current token 143 */ 144 private function parseLongOption($token) 145 { 146 $name = substr($token, 2); 147 148 if (false !== $pos = strpos($name, '=')) { 149 if (0 === \strlen($value = substr($name, $pos + 1))) { 150 array_unshift($this->parsed, null); 151 } 152 $this->addLongOption(substr($name, 0, $pos), $value); 153 } else { 154 $this->addLongOption($name, null); 155 } 156 } 157 158 /** 159 * Parses an argument. 160 * 161 * @param string $token The current token 162 * 163 * @throws RuntimeException When too many arguments are given 164 */ 165 private function parseArgument($token) 166 { 167 $c = \count($this->arguments); 168 169 // if input is expecting another argument, add it 170 if ($this->definition->hasArgument($c)) { 171 $arg = $this->definition->getArgument($c); 172 $this->arguments[$arg->getName()] = $arg->isArray() ? array($token) : $token; 173 174 // if last argument isArray(), append token to last argument 175 } elseif ($this->definition->hasArgument($c - 1) && $this->definition->getArgument($c - 1)->isArray()) { 176 $arg = $this->definition->getArgument($c - 1); 177 $this->arguments[$arg->getName()][] = $token; 178 179 // unexpected argument 180 } else { 181 $all = $this->definition->getArguments(); 182 if (\count($all)) { 183 throw new RuntimeException(sprintf('Too many arguments, expected arguments "%s".', implode('" "', array_keys($all)))); 184 } 185 186 throw new RuntimeException(sprintf('No arguments expected, got "%s".', $token)); 187 } 188 } 189 190 /** 191 * Adds a short option value. 192 * 193 * @param string $shortcut The short option key 194 * @param mixed $value The value for the option 195 * 196 * @throws RuntimeException When option given doesn't exist 197 */ 198 private function addShortOption($shortcut, $value) 199 { 200 if (!$this->definition->hasShortcut($shortcut)) { 201 throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut)); 202 } 203 204 $this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value); 205 } 206 207 /** 208 * Adds a long option value. 209 * 210 * @param string $name The long option key 211 * @param mixed $value The value for the option 212 * 213 * @throws RuntimeException When option given doesn't exist 214 */ 215 private function addLongOption($name, $value) 216 { 217 if (!$this->definition->hasOption($name)) { 218 throw new RuntimeException(sprintf('The "--%s" option does not exist.', $name)); 219 } 220 221 $option = $this->definition->getOption($name); 222 223 // Convert empty values to null 224 if (!isset($value[0])) { 225 $value = null; 226 } 227 228 if (null !== $value && !$option->acceptValue()) { 229 throw new RuntimeException(sprintf('The "--%s" option does not accept a value.', $name)); 230 } 231 232 if (null === $value && $option->acceptValue() && \count($this->parsed)) { 233 // if option accepts an optional or mandatory argument 234 // let's see if there is one provided 235 $next = array_shift($this->parsed); 236 if (isset($next[0]) && '-' !== $next[0]) { 237 $value = $next; 238 } elseif (empty($next)) { 239 $value = null; 240 } else { 241 array_unshift($this->parsed, $next); 242 } 243 } 244 245 if (null === $value) { 246 if ($option->isValueRequired()) { 247 throw new RuntimeException(sprintf('The "--%s" option requires a value.', $name)); 248 } 249 250 if (!$option->isArray()) { 251 $value = $option->isValueOptional() ? $option->getDefault() : true; 252 } 253 } 254 255 if ($option->isArray()) { 256 $this->options[$name][] = $value; 257 } else { 258 $this->options[$name] = $value; 259 } 260 } 261 262 /** 263 * {@inheritdoc} 264 */ 265 public function getFirstArgument() 266 { 267 foreach ($this->tokens as $token) { 268 if ($token && '-' === $token[0]) { 269 continue; 270 } 271 272 return $token; 273 } 274 } 275 276 /** 277 * {@inheritdoc} 278 */ 279 public function hasParameterOption($values) 280 { 281 $values = (array) $values; 282 283 foreach ($this->tokens as $token) { 284 foreach ($values as $value) { 285 // Options with values: 286 // For long options, test for '--option=' at beginning 287 // For short options, test for '-o' at beginning 288 $leading = 0 === strpos($value, '--') ? $value.'=' : $value; 289 if ($token === $value || '' !== $leading && 0 === strpos($token, $leading)) { 290 return true; 291 } 292 } 293 } 294 295 return false; 296 } 297 298 /** 299 * {@inheritdoc} 300 */ 301 public function getParameterOption($values, $default = false) 302 { 303 $values = (array) $values; 304 $tokens = $this->tokens; 305 306 while (0 < \count($tokens)) { 307 $token = array_shift($tokens); 308 309 foreach ($values as $value) { 310 if ($token === $value) { 311 return array_shift($tokens); 312 } 313 // Options with values: 314 // For long options, test for '--option=' at beginning 315 // For short options, test for '-o' at beginning 316 $leading = 0 === strpos($value, '--') ? $value.'=' : $value; 317 if ('' !== $leading && 0 === strpos($token, $leading)) { 318 return substr($token, \strlen($leading)); 319 } 320 } 321 } 322 323 return $default; 324 } 325 326 /** 327 * Returns a stringified representation of the args passed to the command. 328 * 329 * @return string 330 */ 331 public function __toString() 332 { 333 $self = $this; 334 $tokens = array_map(function ($token) use ($self) { 335 if (preg_match('{^(-[^=]+=)(.+)}', $token, $match)) { 336 return $match[1].$self->escapeToken($match[2]); 337 } 338 339 if ($token && '-' !== $token[0]) { 340 return $self->escapeToken($token); 341 } 342 343 return $token; 344 }, $this->tokens); 345 346 return implode(' ', $tokens); 347 } 348 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Nov 11 20:33:01 2020 | Cross-referenced by PHPXref 0.7.1 |