2021-05-10 00:23:30 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
2021-09-05 00:27:40 +00:00
namespace RectorPrefix20210905\Symfony\Component\Console\Input ;
2021-05-10 00:23:30 +00:00
2021-09-05 00:27:40 +00:00
use RectorPrefix20210905\Symfony\Component\Console\Exception\InvalidArgumentException ;
2021-05-10 00:23:30 +00:00
/**
* StringInput represents an input provided as a string .
*
* Usage :
*
* $input = new StringInput ( 'foo --bar="foobar"' );
*
* @ author Fabien Potencier < fabien @ symfony . com >
*/
2021-09-05 00:27:40 +00:00
class StringInput extends \RectorPrefix20210905\Symfony\Component\Console\Input\ArgvInput
2021-05-10 00:23:30 +00:00
{
public const REGEX_STRING = '([^\\s]+?)(?:\\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 .
*
* @ throws InvalidArgumentException When unable to parse input ( should never happen )
*/
2021-07-05 23:06:30 +00:00
private function tokenize ( string $input ) : array
2021-05-10 00:23:30 +00:00
{
$tokens = [];
$length = \strlen ( $input );
$cursor = 0 ;
while ( $cursor < $length ) {
if ( \preg_match ( '/\\s+/A' , $input , $match , 0 , $cursor )) {
} elseif ( \preg_match ( '/([^="\'\\s]+?)(=?)(' . self :: REGEX_QUOTED_STRING . '+)/A' , $input , $match , 0 , $cursor )) {
2021-07-18 02:11:18 +00:00
$tokens [] = $match [ 1 ] . $match [ 2 ] . \stripcslashes ( \str_replace ([ '"\'' , '\'"' , '\'\'' , '""' ], '' , \substr ( $match [ 3 ], 1 , - 1 )));
2021-05-10 00:23:30 +00:00
} elseif ( \preg_match ( '/' . self :: REGEX_QUOTED_STRING . '/A' , $input , $match , 0 , $cursor )) {
2021-07-18 02:11:18 +00:00
$tokens [] = \stripcslashes ( \substr ( $match [ 0 ], 1 , - 1 ));
2021-05-10 00:23:30 +00:00
} elseif ( \preg_match ( '/' . self :: REGEX_STRING . '/A' , $input , $match , 0 , $cursor )) {
$tokens [] = \stripcslashes ( $match [ 1 ]);
} else {
// should never happen
2021-09-05 00:27:40 +00:00
throw new \RectorPrefix20210905\Symfony\Component\Console\Exception\InvalidArgumentException ( \sprintf ( 'Unable to parse input near "... %s ...".' , \substr ( $input , $cursor , 10 )));
2021-05-10 00:23:30 +00:00
}
$cursor += \strlen ( $match [ 0 ]);
}
return $tokens ;
}
}