Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Console\Input; Chris@0: Chris@0: use Symfony\Component\Console\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * StringInput represents an input provided as a string. Chris@0: * Chris@0: * Usage: Chris@0: * Chris@0: * $input = new StringInput('foo --bar="foobar"'); Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class StringInput extends ArgvInput Chris@0: { Chris@0: const REGEX_STRING = '([^\s]+?)(?:\s|(?setTokens($this->tokenize($input)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tokenizes a string. Chris@0: * Chris@0: * @param string $input The input to tokenize Chris@0: * Chris@0: * @return array An array of tokens Chris@0: * Chris@0: * @throws InvalidArgumentException When unable to parse input (should never happen) Chris@0: */ Chris@0: private function tokenize($input) Chris@0: { Chris@17: $tokens = []; Chris@17: $length = \strlen($input); Chris@0: $cursor = 0; Chris@0: while ($cursor < $length) { Chris@0: if (preg_match('/\s+/A', $input, $match, null, $cursor)) { Chris@0: } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) { Chris@17: $tokens[] = $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, \strlen($match[3]) - 2))); Chris@0: } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) { Chris@17: $tokens[] = stripcslashes(substr($match[0], 1, \strlen($match[0]) - 2)); Chris@0: } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) { Chris@0: $tokens[] = stripcslashes($match[1]); Chris@0: } else { Chris@0: // should never happen Chris@0: throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10))); Chris@0: } Chris@0: Chris@17: $cursor += \strlen($match[0]); Chris@0: } Chris@0: Chris@0: return $tokens; Chris@0: } Chris@0: }