* 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;
use Symfony\Component\Process\Exception\InvalidArgumentException;
* 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>
* 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
* @deprecated since version 3.3, to be removed in 4.0. Use a command line array or give env vars to the `Process::start/run()` method instead.
public static function escapeArgument($argument)
@trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use a command line array or give env vars to the Process::start/run() method instead.', \E_USER_DEPRECATED);
//Fix for PHP bug #43784 escapeshellarg removes % from given string
//Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
//@see https://bugs.php.net/43784
//@see https://bugs.php.net/49446
if ('\\' === \DIRECTORY_SEPARATOR) {
return escapeshellarg($argument);
foreach (preg_split('/(")/', $argument, -1, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE) as $part) {
$escapedArgument .= '\\"';
} elseif (self::isSurroundedBy($part, '%')) {
// Avoid environment variable expansion
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
// escape trailing backslash
if ('\\' === substr($part, -1)) {
$escapedArgument .= $part;
$escapedArgument = '"'.$escapedArgument.'"';
return "'".str_replace("'", "'\\''", $argument)."'";
* Validates and normalizes a Process input.
* @param string $caller The name of method call that validates the input
* @param mixed $input The input to validate
* @return mixed The validated input
* @throws InvalidArgumentException In case the input is not valid
public static function validateInput($caller, $input)
if (\is_resource($input)) {
if (\is_string($input)) {
if ($input instanceof Process) {
return $input->getIterator($input::ITER_SKIP_ERR);
if ($input instanceof \Iterator) {
if ($input instanceof \Traversable) {
return new \IteratorIterator($input);
throw new InvalidArgumentException(sprintf('"%s" only accepts strings, Traversable objects or stream resources.', $caller));
private static function isSurroundedBy($arg, $char)
return 2 < \strlen($arg) && $char === $arg[0] && $char === $arg[\strlen($arg) - 1];