StringInput.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. parent::__construct(array(), null);
  39. $this->setTokens($this->tokenize($input));
  40. if (null !== $definition) {
  41. $this->bind($definition);
  42. }
  43. }
  44. /**
  45. * Tokenizes a string.
  46. *
  47. * @param string $input The input to tokenize
  48. *
  49. * @return array An array of tokens
  50. *
  51. * @throws \InvalidArgumentException When unable to parse input (should never happen)
  52. */
  53. private function tokenize($input)
  54. {
  55. $tokens = array();
  56. $length = strlen($input);
  57. $cursor = 0;
  58. while ($cursor < $length) {
  59. if (preg_match('/\s+/A', $input, $match, null, $cursor)) {
  60. } elseif (preg_match('/([^="\'\s]+?)(=?)('.self::REGEX_QUOTED_STRING.'+)/A', $input, $match, null, $cursor)) {
  61. $tokens[] = $match[1].$match[2].stripcslashes(str_replace(array('"\'', '\'"', '\'\'', '""'), '', substr($match[3], 1, strlen($match[3]) - 2)));
  62. } elseif (preg_match('/'.self::REGEX_QUOTED_STRING.'/A', $input, $match, null, $cursor)) {
  63. $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
  64. } elseif (preg_match('/'.self::REGEX_STRING.'/A', $input, $match, null, $cursor)) {
  65. $tokens[] = stripcslashes($match[1]);
  66. } else {
  67. // should never happen
  68. // @codeCoverageIgnoreStart
  69. throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
  70. // @codeCoverageIgnoreEnd
  71. }
  72. $cursor += strlen($match[0]);
  73. }
  74. return $tokens;
  75. }
  76. }