Skip to content

Commit 659eb66

Browse files
committed
[Console] Add ArgvInput::__toString and ArrayInput::__toString, fixes #7257
1 parent 09fd2af commit 659eb66

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed

src/Symfony/Component/Console/Input/ArgvInput.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,23 @@ public function getParameterOption($values, $default = false)
312312

313313
return $default;
314314
}
315+
316+
/**
317+
* Returns a stringified representation of the args passed to the command
318+
*
319+
* @return string
320+
*/
321+
public function __toString()
322+
{
323+
$tokens = array_map(function ($token) {
324+
$token = addcslashes($token, '"');
325+
if (false !== strpos($token, ' ')) {
326+
return '"'.$token.'"';
327+
}
328+
329+
return $token;
330+
}, $this->tokens);
331+
332+
return implode(' ', $tokens);
333+
}
315334
}

src/Symfony/Component/Console/Input/ArrayInput.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,29 @@ public function getParameterOption($values, $default = false)
110110
return $default;
111111
}
112112

113+
/**
114+
* Returns a stringified representation of the args passed to the command
115+
*
116+
* @return string
117+
*/
118+
public function __toString()
119+
{
120+
$params = array();
121+
foreach ($this->parameters as $param => $val) {
122+
$val = addcslashes($val, '"');
123+
if (false !== strpos($val, ' ')) {
124+
$val = '"'.$val.'"';
125+
}
126+
if ($param && '-' === $param[0]) {
127+
$params[] = $param . ('' != $val ? ' '.$val : $val);
128+
} else {
129+
$params[] = $val;
130+
}
131+
}
132+
133+
return implode(' ', $params);
134+
}
135+
113136
/**
114137
* Processes command line arguments.
115138
*/

src/Symfony/Component/Console/Tests/Input/ArgvInputTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ public function testHasParameterOption()
255255
$this->assertFalse($input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input');
256256
}
257257

258+
public function testToString()
259+
{
260+
$input = new ArgvInput(array('cli.php', '-f', 'foo'));
261+
$this->assertEquals('-f foo', (string) $input);
262+
263+
$input = new ArgvInput(array('cli.php', '-f', '--bar=foo', 'a b c d'));
264+
$this->assertEquals('-f --bar=foo "a b c d"', (string) $input);
265+
}
266+
258267
/**
259268
* @dataProvider provideGetParameterOptionValues
260269
*/

src/Symfony/Component/Console/Tests/Input/ArrayInputTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,10 @@ public function provideInvalidInput()
120120
)
121121
);
122122
}
123+
124+
public function testToString()
125+
{
126+
$input = new ArrayInput(array('-f' => null, '-b' => 'bar', '--foo' => 'b a z', '--lala' => null, 'test' => 'Foo'));
127+
$this->assertEquals('-f -b bar --foo "b a z" --lala Foo', (string) $input);
128+
}
123129
}

src/Symfony/Component/Console/Tests/Input/StringInputTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,16 @@ public function getTokenizeData()
7373
array('foo -a -ffoo --long bar', array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'),
7474
);
7575
}
76+
77+
public function testToString()
78+
{
79+
$input = new StringInput('-f foo');
80+
$this->assertEquals('-f foo', (string) $input);
81+
82+
$input = new StringInput('-f --bar=foo "a b c d"');
83+
$this->assertEquals('-f --bar=foo "a b c d"', (string) $input);
84+
85+
$input = new StringInput('-f --bar=foo \'a b c d\'');
86+
$this->assertEquals('-f --bar=foo "a b c d"', (string) $input);
87+
}
7688
}

0 commit comments

Comments
 (0)