annotate vendor/symfony/console/Input/StringInput.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Input;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Exception\InvalidArgumentException;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * StringInput represents an input provided as a string.
Chris@0 18 *
Chris@0 19 * Usage:
Chris@0 20 *
Chris@0 21 * $input = new StringInput('foo --bar="foobar"');
Chris@0 22 *
Chris@0 23 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 24 */
Chris@0 25 class StringInput extends ArgvInput
Chris@0 26 {
Chris@0 27 const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
Chris@0 28 const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
Chris@0 29
Chris@0 30 /**
Chris@14 31 * @param string $input A string representing the parameters from the CLI
Chris@0 32 */
Chris@0 33 public function __construct($input)
Chris@0 34 {
Chris@17 35 parent::__construct([]);
Chris@0 36
Chris@0 37 $this->setTokens($this->tokenize($input));
Chris@0 38 }
Chris@0 39
Chris@0 40 /**
Chris@0 41 * Tokenizes a string.
Chris@0 42 *
Chris@0 43 * @param string $input The input to tokenize
Chris@0 44 *
Chris@0 45 * @return array An array of tokens
Chris@0 46 *
Chris@0 47 * @throws InvalidArgumentException When unable to parse input (should never happen)
Chris@0 48 */
Chris@0 49 private function tokenize($input)
Chris@0 50 {
Chris@17 51 $tokens = [];
Chris@17 52 $length = \strlen($input);
Chris@0 53 $cursor = 0;
Chris@0 54 while ($cursor < $length) {
Chris@0 55 if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
Chris@0 56 } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
Chris@17 57 $tokens[] = $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, \strlen($match[3]) - 2)));
Chris@0 58 } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
Chris@17 59 $tokens[] = stripcslashes(substr($match[0], 1, \strlen($match[0]) - 2));
Chris@0 60 } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
Chris@0 61 $tokens[] = stripcslashes($match[1]);
Chris@0 62 } else {
Chris@0 63 // should never happen
Chris@0 64 throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
Chris@0 65 }
Chris@0 66
Chris@17 67 $cursor += \strlen($match[0]);
Chris@0 68 }
Chris@0 69
Chris@0 70 return $tokens;
Chris@0 71 }
Chris@0 72 }