vendor/symfony/src/Symfony/Component/Console/Input/StringInput.php
changeset 0 7f95f8617b0b
equal deleted inserted replaced
-1:000000000000 0:7f95f8617b0b
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * This file is part of the Symfony package.
       
     5  *
       
     6  * (c) Fabien Potencier <fabien@symfony.com>
       
     7  *
       
     8  * For the full copyright and license information, please view the LICENSE
       
     9  * file that was distributed with this source code.
       
    10  */
       
    11 
       
    12 namespace Symfony\Component\Console\Input;
       
    13 
       
    14 /**
       
    15  * StringInput represents an input provided as a string.
       
    16  *
       
    17  * Usage:
       
    18  *
       
    19  *     $input = new StringInput('foo --bar="foobar"');
       
    20  *
       
    21  * @author Fabien Potencier <fabien@symfony.com>
       
    22  *
       
    23  * @api
       
    24  */
       
    25 class StringInput extends ArgvInput
       
    26 {
       
    27     const REGEX_STRING = '([^ ]+?)(?: |(?<!\\\\)"|(?<!\\\\)\'|$)';
       
    28     const REGEX_QUOTED_STRING = '(?:"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'([^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')';
       
    29 
       
    30     /**
       
    31      * Constructor.
       
    32      *
       
    33      * @param string     $input An array of parameters from the CLI (in the argv format)
       
    34      * @param InputDefinition $definition A InputDefinition instance
       
    35      *
       
    36      * @api
       
    37      */
       
    38     public function __construct($input, InputDefinition $definition = null)
       
    39     {
       
    40         parent::__construct(array(), $definition);
       
    41 
       
    42         $this->setTokens($this->tokenize($input));
       
    43     }
       
    44 
       
    45     /**
       
    46      * Tokenizes a string.
       
    47      *
       
    48      * @param string $input The input to tokenize
       
    49      * @throws \InvalidArgumentException When unable to parse input (should never happen)
       
    50      */
       
    51     private function tokenize($input)
       
    52     {
       
    53         $input = preg_replace('/(\r\n|\r|\n|\t)/', ' ', $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('/([^="\' ]+?)(=?)('.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 
       
    73             $cursor += strlen($match[0]);
       
    74         }
       
    75 
       
    76         return $tokens;
       
    77     }
       
    78 }