Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mikehaertl/php-shellcommand
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 2bab79fd541de58c42a10132a6b67460131e48a1
Choose a base ref
..
head repository: mikehaertl/php-shellcommand
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: c2ef7dbdb38a0a477394975097655c00adec97c4
Choose a head ref
Showing with 18 additions and 14 deletions.
  1. +15 −11 src/Command.php
  2. +3 −3 tests/CommandTest.php
26 changes: 15 additions & 11 deletions src/Command.php
Original file line number Diff line number Diff line change
@@ -273,7 +273,7 @@ public function getArgs()
* get escaped if $escapeArgs is true. An array can be passed to add more
* than one value for a key, e.g. `addArg('--exclude',
* array('val1','val2'))` which will create the option `'--exclude' 'val1'
* '--exclude' 'val2'`.
* 'val2'`.
* @param bool|null $escape if set, this overrides the $escapeArgs setting
* and enforces escaping/no escaping
* @return static for method chaining
@@ -290,20 +290,24 @@ public function addArg($key, $value = null, $escape = null)
if ($value === null) {
$this->_args[] = $doEscape ? escapeshellarg($key) : $key;
} else {
if (substr($key, -1) === '=') {
$separator = '=';
$argKey = substr($key, 0, -1);
} else {
$separator = ' ';
$argKey = $key;
}
$argKey = $doEscape ? escapeshellarg($argKey) : $argKey;

if (is_array($value)) {
$params = array();
foreach ($value as $v) {
$this->addArg($key, $v, $escape);
$params[] = $doEscape ? escapeshellarg($v) : $v;
}
$this->_args[] = $argKey . $separator . implode(' ', $params);
} else {
if (substr($key, -1) === '=') {
$this->_args[] = $doEscape ?
escapeshellarg($key . $value) :
$key . $value;
} else {
$this->_args[] = $doEscape ?
escapeshellarg($key) . ' ' . escapeshellarg($value) :
$key . ' ' . $value;
}
$this->_args[] = $argKey . $separator .
($doEscape ? escapeshellarg($value) : $value);
}
}
if ($useLocale) {
6 changes: 3 additions & 3 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
@@ -81,8 +81,8 @@ public function testCanAddArguments()
$command->addArg('-b=', array('v4','v5','v6'));
$command->addArg('-c', '');
$command->addArg('some name', null, true);
$this->assertEquals("--arg1=x '--a' '--a' '中文字äüp' '--a' 'v'\''1' '--a' 'v2' '--a' 'v3' -b=v '-b=v4' '-b=v5' '-b=v6' '-c' '' 'some name'", $command->getArgs());
$this->assertEquals("test --arg1=x '--a' '--a' '中文字äüp' '--a' 'v'\''1' '--a' 'v2' '--a' 'v3' -b=v '-b=v4' '-b=v5' '-b=v6' '-c' '' 'some name'", $command->getExecCommand());
$this->assertEquals("--arg1=x '--a' '--a' '中文字äüp' '--a' 'v'\''1' 'v2' 'v3' -b=v '-b'='v4' 'v5' 'v6' '-c' '' 'some name'", $command->getArgs());
$this->assertEquals("test --arg1=x '--a' '--a' '中文字äüp' '--a' 'v'\''1' 'v2' 'v3' -b=v '-b'='v4' 'v5' 'v6' '-c' '' 'some name'", $command->getExecCommand());
}
public function testCanResetArguments()
{
@@ -102,7 +102,7 @@ public function testCanDisableEscaping()
$command->addArg('-b=','v', true);
$command->addArg('-b=', array('v4','v5','v6'));
$command->addArg('some name', null, true);
$this->assertEquals("--a --a v --a v1 --a v2 --a v3 '-b=v' -b=v4 -b=v5 -b=v6 'some name'", $command->getArgs());
$this->assertEquals("--a --a v --a v1 v2 v3 '-b'='v' -b=v4 v5 v6 'some name'", $command->getArgs());
}
public function testCanPreventCommandInjection()
{