StringInput.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Input;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. /**
  13. * StringInput represents an input provided as a string.
  14. *
  15. * Usage:
  16. *
  17. * $input = new StringInput('foo --bar="foobar"');
  18. *
  19. * @author Fabien Potencier <fabien@symfony.com>
  20. */
  21. class StringInput extends ArgvInput
  22. {
  23. const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
  24. const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
  25. /**
  26. * @param string $input A string representing the parameters from the CLI
  27. * @param InputDefinition $definition A InputDefinition instance
  28. *
  29. * @deprecated The second argument is deprecated as it does not work (will be removed in 3.0), use 'bind' method instead
  30. */
  31. public function __construct($input, InputDefinition $definition = null)
  32. {
  33. if ($definition) {
  34. @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);
  35. }
  36. parent::__construct(array(), null);
  37. $this->setTokens($this->tokenize($input));
  38. if (null !== $definition) {
  39. $this->bind($definition);
  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. $cursor += \strlen($match[0]);
  69. }
  70. return $tokens;
  71. }
  72. }