a40ddba31dec8bbe5e2002e4e922b138c32bd754
[yaffs-website] / vendor / symfony / console / Input / StringInput.php
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      * @param InputDefinition $definition A InputDefinition instance
35      *
36      * @deprecated The second argument is deprecated as it does not work (will be removed in 3.0), use 'bind' method instead
37      */
38     public function __construct($input, InputDefinition $definition = null)
39     {
40         if ($definition) {
41             @trigger_error('The $definition argument of the '.__METHOD__.' method is deprecated and will be removed in 3.0. Set this parameter with the bind() method instead.', E_USER_DEPRECATED);
42         }
43
44         parent::__construct(array(), null);
45
46         $this->setTokens($this->tokenize($input));
47
48         if (null !== $definition) {
49             $this->bind($definition);
50         }
51     }
52
53     /**
54      * Tokenizes a string.
55      *
56      * @param string $input The input to tokenize
57      *
58      * @return array An array of tokens
59      *
60      * @throws InvalidArgumentException When unable to parse input (should never happen)
61      */
62     private function tokenize($input)
63     {
64         $tokens = array();
65         $length = strlen($input);
66         $cursor = 0;
67         while ($cursor < $length) {
68             if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
69             } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
70                 $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
71             } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
72                 $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
73             } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
74                 $tokens[] = stripcslashes($match[1]);
75             } else {
76                 // should never happen
77                 throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
78             }
79
80             $cursor += strlen($match[0]);
81         }
82
83         return $tokens;
84     }
85 }