Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. StringInput.php

class StringInput

StringInput represents an input provided as a string.

Usage:

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

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

  • class \Symfony\Component\Console\Input\Input implements \Symfony\Component\Console\Input\InputInterface, \Symfony\Component\Console\Input\StreamableInputInterface
    • class \Symfony\Component\Console\Input\ArgvInput extends \Symfony\Component\Console\Input\Input
      • class \Symfony\Component\Console\Input\StringInput extends \Symfony\Component\Console\Input\ArgvInput

Expanded class hierarchy of StringInput

4 files declare their use of StringInput
BufferIO.php in vendor/composer/composer/src/Composer/IO/BufferIO.php
EventDispatcher.php in vendor/composer/composer/src/Composer/EventDispatcher/EventDispatcher.php
GlobalCommand.php in vendor/composer/composer/src/Composer/Command/GlobalCommand.php
RunCommandMessageHandler.php in vendor/symfony/console/Messenger/RunCommandMessageHandler.php

File

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

Namespace

Symfony\Component\Console\Input
View source
class StringInput extends ArgvInput {
    public const REGEX_UNQUOTED_STRING = '([^\\s\\\\]+?)';
    public const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
    
    /**
     * @param string $input A string representing the parameters from the CLI
     */
    public function __construct(string $input) {
        parent::__construct([]);
        $this->setTokens($this->tokenize($input));
    }
    
    /**
     * Tokenizes a string.
     *
     * @return list<string>
     *
     * @throws InvalidArgumentException When unable to parse input (should never happen)
     */
    private function tokenize(string $input) : array {
        $tokens = [];
        $length = \strlen($input);
        $cursor = 0;
        $token = null;
        while ($cursor < $length) {
            if ('\\' === $input[$cursor]) {
                $token .= $input[++$cursor] ?? '';
                ++$cursor;
                continue;
            }
            if (preg_match('/\\s+/A', $input, $match, 0, $cursor)) {
                if (null !== $token) {
                    $tokens[] = $token;
                    $token = null;
                }
            }
            elseif (preg_match('/([^="\'\\s]+?)(=?)(' . self::REGEX_QUOTED_STRING . '+)/A', $input, $match, 0, $cursor)) {
                $token .= $match[1] . $match[2] . stripcslashes(str_replace([
                    '"\'',
                    '\'"',
                    '\'\'',
                    '""',
                ], '', substr($match[3], 1, -1)));
            }
            elseif (preg_match('/' . self::REGEX_QUOTED_STRING . '/A', $input, $match, 0, $cursor)) {
                $token .= stripcslashes(substr($match[0], 1, -1));
            }
            elseif (preg_match('/' . self::REGEX_UNQUOTED_STRING . '/A', $input, $match, 0, $cursor)) {
                $token .= $match[1];
            }
            else {
                // should never happen
                throw new InvalidArgumentException(\sprintf('Unable to parse input near "... %s ...".', substr($input, $cursor, 10)));
            }
            $cursor += \strlen($match[0]);
        }
        if (null !== $token) {
            $tokens[] = $token;
        }
        return $tokens;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ArgvInput::$parsed private property
ArgvInput::$tokens private property @var list&lt;string&gt; 1
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::getRawTokens public function Returns un-parsed and not validated tokens.
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::parseToken protected function 1
ArgvInput::setTokens protected function
ArgvInput::__toString public function Returns a stringified representation of the args passed to the command. Overrides InputInterface::__toString 1
Input::$arguments protected property
Input::$definition protected property
Input::$interactive protected property
Input::$options protected property
Input::$stream protected property @var resource
Input::bind public function Binds the current Input instance with the given arguments and options. Overrides InputInterface::bind 1
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 all the given arguments merged with the default 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 all the given options merged with the default values. Overrides InputInterface::getOptions
Input::getStream public function Overrides StreamableInputInterface::getStream
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 Is this input means 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::setStream public function Overrides StreamableInputInterface::setStream
Input::validate public function Validates the input. Overrides InputInterface::validate
StringInput::REGEX_QUOTED_STRING public constant
StringInput::REGEX_UNQUOTED_STRING public constant
StringInput::tokenize private function Tokenizes a string.
StringInput::__construct public function Overrides ArgvInput::__construct

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal