Drupal investigation

StringInput.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * Constructor.
  27. *
  28. * @param string $input An array of parameters from the CLI (in the argv format)
  29. * @param InputDefinition $definition A InputDefinition instance
  30. *
  31. * @deprecated The second argument is deprecated as it does not work (will be removed in 3.0), use 'bind' method instead
  32. */
  33. public function __construct($input, InputDefinition $definition = null)
  34. {
  35. if ($definition) {
  36. @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);
  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. throw new InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
  69. }
  70. $cursor += strlen($match[0]);
  71. }
  72. return $tokens;
  73. }
  74. }