| 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\InvalidArgumentException; |
| 15 |
|
| 16 |
/** |
| 17 |
* StringInput represents an input provided as a string. |
| 18 |
* |
| 19 |
* Usage: |
| 20 |
* |
| 21 |
* $input = new StringInput('foo --bar="foobar"'); |
| 22 |
* |
| 23 |
* @author Fabien Potencier <fabien@symfony.com> |
| 24 |
*/ |
| 25 |
class StringInput extends ArgvInput |
| 26 |
{ |
| 27 |
const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)'; |
| 28 |
const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')'; |
| 29 |
|
| 30 |
/** |
| 31 |
* @param string $input A string representing the parameters from the CLI |
| 32 |
*/ |
| 33 |
public function __construct($input) |
| 34 |
{ |
| 35 |
parent::__construct([]); |
| 36 |
|
| 37 |
$this->setTokens($this->tokenize($input)); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Tokenizes a string. |
| 42 |
* |
| 43 |
* @param string $input The input to tokenize |
| 44 |
* |
| 45 |
* @return array An array of tokens |
| 46 |
* |
| 47 |
* @throws InvalidArgumentException When unable to parse input (should never happen) |
| 48 |
*/ |
| 49 |
private function tokenize($input) |
| 50 |
{ |
| 51 |
$tokens = []; |
| 52 |
$length = \strlen($input); |
| 53 |
$cursor = 0; |
| 54 |
while ($cursor < $length) { |
| 55 |
if (preg_match('/\s+/A', $input, $match, null, $cursor)) { |
| 56 |
} elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) { |
| 57 |
$tokens[] = $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, \strlen($match[3]) - 2))); |
| 58 |
} elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) { |
| 59 |
$tokens[] = stripcslashes(substr($match[0], 1, \strlen($match[0]) - 2)); |
| 60 |
} elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) { |
| 61 |
$tokens[] = stripcslashes($match[1]); |
| 62 |
} else { |
| 63 |
// should never happen |
| 64 |
throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10))); |
| 65 |
} |
| 66 |
|
| 67 |
$cursor += \strlen($match[0]); |
| 68 |
} |
| 69 |
|
| 70 |
return $tokens; |
| 71 |
} |
| 72 |
} |
| 73 |
|