4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Console\Input;
14 use Symfony\Component\Console\Exception\InvalidArgumentException;
17 * StringInput represents an input provided as a string.
21 * $input = new StringInput('foo --bar="foobar"');
23 * @author Fabien Potencier <fabien@symfony.com>
25 class StringInput extends ArgvInput
27 const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
28 const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
33 * @param string $input An array of parameters from the CLI (in the argv format)
35 public function __construct($input)
37 parent::__construct(array());
39 $this->setTokens($this->tokenize($input));
45 * @param string $input The input to tokenize
47 * @return array An array of tokens
49 * @throws InvalidArgumentException When unable to parse input (should never happen)
51 private function tokenize($input)
54 $length = strlen($input);
56 while ($cursor < $length) {
57 if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
58 } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
59 $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
60 } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
61 $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
62 } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
63 $tokens[] = stripcslashes($match[1]);
65 // should never happen
66 throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
69 $cursor += strlen($match[0]);