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@0
|
31 * Constructor.
|
Chris@0
|
32 *
|
Chris@0
|
33 * @param string $input An array of parameters from the CLI (in the argv format)
|
Chris@0
|
34 */
|
Chris@0
|
35 public function __construct($input)
|
Chris@0
|
36 {
|
Chris@0
|
37 parent::__construct(array());
|
Chris@0
|
38
|
Chris@0
|
39 $this->setTokens($this->tokenize($input));
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Tokenizes a string.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @param string $input The input to tokenize
|
Chris@0
|
46 *
|
Chris@0
|
47 * @return array An array of tokens
|
Chris@0
|
48 *
|
Chris@0
|
49 * @throws InvalidArgumentException When unable to parse input (should never happen)
|
Chris@0
|
50 */
|
Chris@0
|
51 private function tokenize($input)
|
Chris@0
|
52 {
|
Chris@0
|
53 $tokens = array();
|
Chris@0
|
54 $length = strlen($input);
|
Chris@0
|
55 $cursor = 0;
|
Chris@0
|
56 while ($cursor < $length) {
|
Chris@0
|
57 if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
|
Chris@0
|
58 } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
|
Chris@0
|
59 $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
|
Chris@0
|
60 } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
|
Chris@0
|
61 $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
|
Chris@0
|
62 } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
|
Chris@0
|
63 $tokens[] = stripcslashes($match[1]);
|
Chris@0
|
64 } else {
|
Chris@0
|
65 // should never happen
|
Chris@0
|
66 throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 $cursor += strlen($match[0]);
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 return $tokens;
|
Chris@0
|
73 }
|
Chris@0
|
74 }
|