comparison vendor/symfony/console/Input/StringInput.php @ 0:4c8ae668cc8c

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