TYPO3  7.6
StringInput.php
Go to the documentation of this file.
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 
25 class StringInput extends ArgvInput
26 {
27  const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
28  const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
29 
40  public function __construct($input, InputDefinition $definition = null)
41  {
42  if ($definition) {
43  @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);
44  }
45 
46  parent::__construct(array(), null);
47 
48  $this->setTokens($this->tokenize($input));
49 
50  if (null !== $definition) {
51  $this->bind($definition);
52  }
53  }
54 
64  private function tokenize($input)
65  {
66  $tokens = array();
67  $length = strlen($input);
68  $cursor = 0;
69  while ($cursor < $length) {
70  if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
71  } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
72  $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
73  } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
74  $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
75  } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
76  $tokens[] = stripcslashes($match[1]);
77  } else {
78  // should never happen
79  throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
80  }
81 
82  $cursor += strlen($match[0]);
83  }
84 
85  return $tokens;
86  }
87 }