StringInput.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. /**
  24. * @deprecated since Symfony 6.1
  25. */
  26. public const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
  27. public const REGEX_UNQUOTED_STRING = '([^\s\\\\]+?)';
  28. public const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
  29. /**
  30. * @param string $input A string representing the parameters from the CLI
  31. */
  32. public function __construct(string $input)
  33. {
  34. parent::__construct([]);
  35. $this->setTokens($this->tokenize($input));
  36. }
  37. /**
  38. * Tokenizes a string.
  39. *
  40. * @throws InvalidArgumentException When unable to parse input (should never happen)
  41. */
  42. private function tokenize(string $input): array
  43. {
  44. $tokens = [];
  45. $length = \strlen($input);
  46. $cursor = 0;
  47. $token = null;
  48. while ($cursor < $length) {
  49. if ('\\' === $input[$cursor]) {
  50. $token .= $input[++$cursor] ?? '';
  51. ++$cursor;
  52. continue;
  53. }
  54. if (preg_match('/\s+/A', $input, $match, 0, $cursor)) {
  55. if (null !== $token) {
  56. $tokens[] = $token;
  57. $token = null;
  58. }
  59. } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, 0, $cursor)) {
  60. $token .= $match[1].$match[2].stripcslashes(str_replace(['"\'', '\'"', '\'\'', '""'], '', substr($match[3], 1, -1)));
  61. } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, 0, $cursor)) {
  62. $token .= stripcslashes(substr($match[0], 1, -1));
  63. } elseif (preg_match('/'.self::REGEX_UNQUOTED_STRING.'/A', $input, $match, 0, $cursor)) {
  64. $token .= $match[1];
  65. } else {
  66. // should never happen
  67. throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10)));
  68. }
  69. $cursor += \strlen($match[0]);
  70. }
  71. if (null !== $token) {
  72. $tokens[] = $token;
  73. }
  74. return $tokens;
  75. }
  76. }