4 * This file is derived from part of the Symfony package, which is
5 * (c) Fabien Potencier <fabien@symfony.com>
10 use Symfony\Component\Process\Exception\InvalidArgumentException;
13 * ProcessUtils is a bunch of utility methods. We want to allow Robo 1.x
14 * to work with Symfony 4.x while remaining backwards compatibility. This
15 * requires us to replace some deprecated functionality removed in Symfony.
20 * This class should not be instantiated.
22 private function __construct()
27 * Escapes a string to be used as a shell argument.
29 * @param string $argument The argument that will be escaped
31 * @return string The escaped argument
33 * @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.
35 public static function escapeArgument($argument)
37 @trigger_error('The '.__METHOD__.'() method is a copy of a method that was deprecated by Symfony 3.3 and removed in Symfony 4; it will be removed in Robo 2.0.', E_USER_DEPRECATED);
39 //Fix for PHP bug #43784 escapeshellarg removes % from given string
40 //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
41 //@see https://bugs.php.net/bug.php?id=43784
42 //@see https://bugs.php.net/bug.php?id=49446
43 if ('\\' === DIRECTORY_SEPARATOR) {
44 if ('' === $argument) {
45 return escapeshellarg($argument);
48 $escapedArgument = '';
50 foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
52 $escapedArgument .= '\\"';
53 } elseif (self::isSurroundedBy($part, '%')) {
54 // Avoid environment variable expansion
55 $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
57 // escape trailing backslash
58 if ('\\' === substr($part, -1)) {
62 $escapedArgument .= $part;
66 $escapedArgument = '"'.$escapedArgument.'"';
69 return $escapedArgument;
72 return "'".str_replace("'", "'\\''", $argument)."'";
75 private static function isSurroundedBy($arg, $char)
77 return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1];