merged branch hason/process (PR #6796)

This PR was merged into the master branch.

Discussion
----------

[Process] Added ProcessUtils::escapeArgument() to fix the bug in escapeshellarg() function on Windows

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Commits
-------

a557b89 [Process] Added ProcessUtils::escapeArgument() to fix the bug in escapeshellarg() function on Windows
This commit is contained in:
Fabien Potencier 2013-04-21 10:43:42 +02:00
commit de956b6e12
5 changed files with 116 additions and 1 deletions

View File

@ -4,6 +4,7 @@ CHANGELOG
2.3.0
-----
* added ProcessUtils::escapeArgument() to fix the bug in escapeshellarg() function on Windows
* added Process::signal()
* added Process::getPid()
* added support for a TTY mode

View File

@ -161,7 +161,7 @@ class ProcessBuilder
$options = $this->options;
$script = ($this->prefix ? escapeshellarg($this->prefix) . ' ' : '')
. implode(' ', array_map('escapeshellarg', $this->arguments));
.implode(' ', array_map(array(__NAMESPACE__.'\\ProcessUtils', 'escapeArgument'), $this->arguments));
if ($this->inheritEnv) {
$env = $this->env ? $this->env + $_ENV : null;

View File

@ -0,0 +1,60 @@
<?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.
*/
namespace Symfony\Component\Process;
/**
* ProcessUtils is a bunch of utility methods.
*
* This class contains static methods only and is not meant to be instantiated.
*
* @author Martin Hasoň <martin.hason@gmail.com>
*/
class ProcessUtils
{
/**
* This class should not be instantiated
*/
private function __construct()
{
}
/**
* Escapes a string to be used as a shell argument.
*
* @param string $argument The argument that will be escaped
*
* @return string The escaped argument
*/
public static function escapeArgument($argument)
{
//Fix for PHP bug #43784 escapeshellarg removes % from given string
//Fix for PHP bug #49446 escapeshellarg dosn`t work on windows
//@see https://bugs.php.net/bug.php?id=43784
//@see https://bugs.php.net/bug.php?id=49446
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$escapedArgument = '';
foreach(preg_split('/([%"])/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
if ('"' == $part) {
$escapedArgument .= '\\"';
} elseif ('%' == $part) {
$escapedArgument .= '^%';
} else {
$escapedArgument .= escapeshellarg($part);
}
}
return $escapedArgument;
}
return escapeshellarg($argument);
}
}

View File

@ -127,4 +127,16 @@ class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
$proc = $pb->setArguments(array('-i'))->getProcess();
$this->assertEquals("'/usr/bin/php' '-i'", $proc->getCommandLine());
}
public function testShouldEscapeArguments()
{
$pb = new ProcessBuilder(array('%path%', 'foo " bar'));
$proc = $pb->getProcess();
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
$this->assertSame('^%"path"^% "foo "\\"" bar"', $proc->getCommandLine());
} else {
$this->assertSame("'%path%' 'foo \" bar'", $proc->getCommandLine());
}
}
}

View File

@ -0,0 +1,42 @@
<?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.
*/
namespace Symfony\Component\Process\Tests;
use Symfony\Component\Process\ProcessUtils;
class ProcessUtilsTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider dataArguments
*/
public function testEscapeArgument($result, $argument)
{
$this->assertSame($result, ProcessUtils::escapeArgument($argument));
}
public function dataArguments()
{
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
return array(
array('"foo bar"', 'foo bar'),
array('^%"path"^%', '%path%'),
array('"<|>"\\"" "\\""\'f"', '<|>" "\'f'),
);
}
return array(
array("'foo bar'", 'foo bar'),
array("'%path%'", '%path%'),
array("'<|>\" \"'\\''f'", '<|>" "\'f'),
);
}
}