You are here

class StringInput in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/symfony/console/Input/StringInput.php \Symfony\Component\Console\Input\StringInput

StringInput represents an input provided as a string.

Usage:

$input = new StringInput('foo --bar="foobar"');

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of StringInput

3 files declare their use of StringInput
CommandTest.php in vendor/symfony/console/Tests/Command/CommandTest.php
Shell.php in vendor/symfony/console/Shell.php
StringInputTest.php in vendor/symfony/console/Tests/Input/StringInputTest.php

File

vendor/symfony/console/Input/StringInput.php, line 23

Namespace

Symfony\Component\Console\Input
View source
class StringInput extends ArgvInput {
  const REGEX_STRING = '([^\\s]+?)(?:\\s|(?<!\\\\)"|(?<!\\\\)\'|$)';
  const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';

  /**
   * Constructor.
   *
   * @param string          $input      An array of parameters from the CLI (in the argv format)
   * @param InputDefinition $definition A InputDefinition instance
   *
   * @deprecated The second argument is deprecated as it does not work (will be removed in 3.0), use 'bind' method instead
   */
  public function __construct($input, InputDefinition $definition = null) {
    if ($definition) {
      @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);
    }
    parent::__construct(array(), null);
    $this
      ->setTokens($this
      ->tokenize($input));
    if (null !== $definition) {
      $this
        ->bind($definition);
    }
  }

  /**
   * Tokenizes a string.
   *
   * @param string $input The input to tokenize
   *
   * @return array An array of tokens
   *
   * @throws \InvalidArgumentException When unable to parse input (should never happen)
   */
  private function tokenize($input) {
    $tokens = array();
    $length = strlen($input);
    $cursor = 0;
    while ($cursor < $length) {
      if (preg_match('/\\s+/A', $input, $match, null, $cursor)) {
      }
      elseif (preg_match('/([^="\'\\s]+?)(=?)(' . self::REGEX_QUOTED_STRING . '+)/A', $input, $match, null, $cursor)) {
        $tokens[] = $match[1] . $match[2] . stripcslashes(str_replace(array(
          '"\'',
          '\'"',
          '\'\'',
          '""',
        ), '', substr($match[3], 1, strlen($match[3]) - 2)));
      }
      elseif (preg_match('/' . self::REGEX_QUOTED_STRING . '/A', $input, $match, null, $cursor)) {
        $tokens[] = stripcslashes(substr($match[0], 1, strlen($match[0]) - 2));
      }
      elseif (preg_match('/' . self::REGEX_STRING . '/A', $input, $match, null, $cursor)) {
        $tokens[] = stripcslashes($match[1]);
      }
      else {

        // should never happen
        throw new \InvalidArgumentException(sprintf('Unable to parse input near "... %s ..."', substr($input, $cursor, 10)));
      }
      $cursor += strlen($match[0]);
    }
    return $tokens;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ArgvInput::$parsed private property
ArgvInput::$tokens private property
ArgvInput::addLongOption private function Adds a long option value.
ArgvInput::addShortOption private function Adds a short option value.
ArgvInput::getFirstArgument public function Returns the first argument from the raw parameters (not parsed). Overrides InputInterface::getFirstArgument
ArgvInput::getParameterOption public function Returns the value of a raw option (not parsed). Overrides InputInterface::getParameterOption
ArgvInput::hasParameterOption public function Returns true if the raw parameters (not parsed) contain a value. Overrides InputInterface::hasParameterOption
ArgvInput::parse protected function Processes command line arguments. Overrides Input::parse
ArgvInput::parseArgument private function Parses an argument.
ArgvInput::parseLongOption private function Parses a long option.
ArgvInput::parseShortOption private function Parses a short option.
ArgvInput::parseShortOptionSet private function Parses a short option set.
ArgvInput::setTokens protected function
ArgvInput::__toString public function Returns a stringified representation of the args passed to the command.
Input::$arguments protected property
Input::$definition protected property
Input::$interactive protected property
Input::$options protected property
Input::bind public function Binds the current Input instance with the given arguments and options. Overrides InputInterface::bind
Input::escapeToken public function Escapes a token through escapeshellarg if it contains unsafe chars.
Input::getArgument public function Returns the argument value for a given argument name. Overrides InputInterface::getArgument
Input::getArguments public function Returns the argument values. Overrides InputInterface::getArguments
Input::getOption public function Returns the option value for a given option name. Overrides InputInterface::getOption
Input::getOptions public function Returns the options values. Overrides InputInterface::getOptions
Input::hasArgument public function Returns true if an InputArgument object exists by name or position. Overrides InputInterface::hasArgument
Input::hasOption public function Returns true if an InputOption object exists by name. Overrides InputInterface::hasOption
Input::isInteractive public function Checks if the input is interactive. Overrides InputInterface::isInteractive
Input::setArgument public function Sets an argument value by name. Overrides InputInterface::setArgument
Input::setInteractive public function Sets the input interactivity. Overrides InputInterface::setInteractive
Input::setOption public function Sets an option value by name. Overrides InputInterface::setOption
Input::validate public function Validates the input. Overrides InputInterface::validate
StringInput::REGEX_QUOTED_STRING constant
StringInput::REGEX_STRING constant
StringInput::tokenize private function Tokenizes a string.
StringInput::__construct Deprecated public function Constructor. Overrides ArgvInput::__construct