[Console] Add ArgvInput::__toString and ArrayInput::__toString, fixes #7257
This commit is contained in:
parent
09fd2af1cd
commit
659eb663c6
@ -312,4 +312,23 @@ class ArgvInput extends Input
|
|||||||
|
|
||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a stringified representation of the args passed to the command
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
$tokens = array_map(function ($token) {
|
||||||
|
$token = addcslashes($token, '"');
|
||||||
|
if (false !== strpos($token, ' ')) {
|
||||||
|
return '"'.$token.'"';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $token;
|
||||||
|
}, $this->tokens);
|
||||||
|
|
||||||
|
return implode(' ', $tokens);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -110,6 +110,29 @@ class ArrayInput extends Input
|
|||||||
return $default;
|
return $default;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a stringified representation of the args passed to the command
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
$params = array();
|
||||||
|
foreach ($this->parameters as $param => $val) {
|
||||||
|
$val = addcslashes($val, '"');
|
||||||
|
if (false !== strpos($val, ' ')) {
|
||||||
|
$val = '"'.$val.'"';
|
||||||
|
}
|
||||||
|
if ($param && '-' === $param[0]) {
|
||||||
|
$params[] = $param . ('' != $val ? ' '.$val : $val);
|
||||||
|
} else {
|
||||||
|
$params[] = $val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(' ', $params);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Processes command line arguments.
|
* Processes command line arguments.
|
||||||
*/
|
*/
|
||||||
|
@ -255,6 +255,15 @@ class ArgvInputTest extends \PHPUnit_Framework_TestCase
|
|||||||
$this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
|
$this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testToString()
|
||||||
|
{
|
||||||
|
$input = new ArgvInput(array('cli.php', '-f', 'foo'));
|
||||||
|
$this->assertEquals('-f foo', (string) $input);
|
||||||
|
|
||||||
|
$input = new ArgvInput(array('cli.php', '-f', '--bar=foo', 'a b c d'));
|
||||||
|
$this->assertEquals('-f --bar=foo "a b c d"', (string) $input);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider provideGetParameterOptionValues
|
* @dataProvider provideGetParameterOptionValues
|
||||||
*/
|
*/
|
||||||
|
@ -120,4 +120,10 @@ class ArrayInputTest extends \PHPUnit_Framework_TestCase
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testToString()
|
||||||
|
{
|
||||||
|
$input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo'));
|
||||||
|
$this->assertEquals('-f -b bar --foo "b a z" --lala Foo', (string) $input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,4 +73,16 @@ class StringInputTest extends \PHPUnit_Framework_TestCase
|
|||||||
array('foo -a -ffoo --long bar', array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'),
|
array('foo -a -ffoo --long bar', array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testToString()
|
||||||
|
{
|
||||||
|
$input = new StringInput('-f foo');
|
||||||
|
$this->assertEquals('-f foo', (string) $input);
|
||||||
|
|
||||||
|
$input = new StringInput('-f --bar=foo "a b c d"');
|
||||||
|
$this->assertEquals('-f --bar=foo "a b c d"', (string) $input);
|
||||||
|
|
||||||
|
$input = new StringInput('-f --bar=foo \'a b c d\'');
|
||||||
|
$this->assertEquals('-f --bar=foo "a b c d"', (string) $input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user