StringInput.php 2.9 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. /**
  12. * StringInput represents an input provided as a string.
  13. *
  14. * Usage:
  15. *
  16. * $input = new StringInput('foo --bar="foobar"');
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. *
  20. * @api
  21. */
  22. class StringInput extends ArgvInput
  23. {
  24. const REGEX_STRING = '([^\s]+?)(?:\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
  25. const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
  26. /**
  27. * Constructor.
  28. *
  29. * @param string $input An array of parameters from the CLI (in the argv format)
  30. * @param InputDefinition $definition A InputDefinition instance
  31. *
  32. * @deprecated The second argument is deprecated as it does not work (will be removed in 3.0), use 'bind' method instead
  33. *
  34. * @api
  35. */
  36. public function __construct($input, InputDefinition $definition = null)
  37. {
  38. if ($definition) {
  39. @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);
  40. }
  41. parent::__construct(array(), null);
  42. $this->setTokens($this->tokenize($input));
  43. if (null !== $definition) {
  44. $this->bind($definition);
  45. }
  46. }
  47. /**
  48. * Tokenizes a string.
  49. *
  50. * @param string $input The input to tokenize
  51. *
  52. * @return array An array of tokens
  53. *
  54. * @throws \InvalidArgumentException When unable to parse input (should never happen)
  55. */
  56. private function tokenize($input)
  57. {
  58. $tokens = array();
  59. $length = strlen($input);
  60. $cursor = 0;
  61. while ($cursor < $length) {
  62. if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
  63. } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
  64. $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
  65. } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
  66. $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
  67. } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
  68. $tokens[] = stripcslashes($match[1]);
  69. } else {
  70. // should never happen
  71. throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
  72. }
  73. $cursor += strlen($match[0]);
  74. }
  75. return $tokens;
  76. }
  77. }