diff options
Diffstat (limited to 'vendor/symfony/console/Input/StringInput.php')
-rw-r--r-- | vendor/symfony/console/Input/StringInput.php | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/vendor/symfony/console/Input/StringInput.php b/vendor/symfony/console/Input/StringInput.php new file mode 100644 index 0000000..8357001 --- /dev/null +++ b/vendor/symfony/console/Input/StringInput.php | |||
@@ -0,0 +1,85 @@ | |||
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 | public const REGEX_UNQUOTED_STRING = '([^\s\\\\]+?)'; | ||
28 | public const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')'; | ||
29 | |||
30 | /** | ||
31 | * @param string $input A string representing the parameters from the CLI | ||
32 | */ | ||
33 | public function __construct(string $input) | ||
34 | { | ||
35 | parent::__construct([]); | ||
36 | |||
37 | $this->setTokens($this->tokenize($input)); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Tokenizes a string. | ||
42 | * | ||
43 | * @return list<string> | ||
44 | * | ||
45 | * @throws InvalidArgumentException When unable to parse input (should never happen) | ||
46 | */ | ||
47 | private function tokenize(string $input): array | ||
48 | { | ||
49 | $tokens = []; | ||
50 | $length = \strlen($input); | ||
51 | $cursor = 0; | ||
52 | $token = null; | ||
53 | while ($cursor < $length) { | ||
54 | if ('\\' === $input[$cursor]) { | ||
55 | $token .= $input[++$cursor] ?? ''; | ||
56 | ++$cursor; | ||
57 | continue; | ||
58 | } | ||
59 | |||
60 | if (preg_match('/\s+/A', $input, $match, 0, $cursor)) { | ||
61 | if (null !== $token) { | ||
62 | $tokens[] = $token; | ||
63 | $token = null; | ||
64 | } | ||
65 | } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, 0, $cursor)) { | ||
66 | $token .= $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, -1))); | ||
67 | } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, 0, $cursor)) { | ||
68 | $token .= stripcslashes(substr($match[0], 1, -1)); | ||
69 | } elseif (preg_match('/'.self::REGEX_UNQUOTED_STRING.'/A', $input, $match, 0, $cursor)) { | ||
70 | $token .= $match[1]; | ||
71 | } else { | ||
72 | // should never happen | ||
73 | throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10))); | ||
74 | } | ||
75 | |||
76 | $cursor += \strlen($match[0]); | ||
77 | } | ||
78 | |||
79 | if (null !== $token) { | ||
80 | $tokens[] = $token; | ||
81 | } | ||
82 | |||
83 | return $tokens; | ||
84 | } | ||
85 | } | ||