| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <fabien@symfony.com> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
/** |
| 13 |
* ProcessUtils is a bunch of utility methods. |
| 14 |
* |
| 15 |
* This class contains static methods only and is not meant to be instantiated. |
| 16 |
* |
| 17 |
* @author Martin Hasoň <martin.hason@gmail.com> |
| 18 |
*/ |
| 19 |
class Symfony_Process_ProcessUtils |
| 20 |
{ |
| 21 |
/** |
| 22 |
* This class should not be instantiated. |
| 23 |
*/ |
| 24 |
private function __construct() |
| 25 |
{ |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Escapes a string to be used as a shell argument. |
| 30 |
* |
| 31 |
* @param string $argument The argument that will be escaped |
| 32 |
* |
| 33 |
* @return string The escaped argument |
| 34 |
*/ |
| 35 |
public static function escapeArgument($argument) |
| 36 |
{ |
| 37 |
//Fix for PHP bug #43784 escapeshellarg removes % from given string |
| 38 |
//Fix for PHP bug #49446 escapeshellarg doesn't work on Windows |
| 39 |
//@see https://bugs.php.net/bug.php?id=43784 |
| 40 |
//@see https://bugs.php.net/bug.php?id=49446 |
| 41 |
if (self::isWindows()) { |
| 42 |
if ('' === $argument) { |
| 43 |
return escapeshellarg($argument); |
| 44 |
} |
| 45 |
|
| 46 |
$escapedArgument = ''; |
| 47 |
$quote = false; |
| 48 |
foreach (preg_split('/(")/i', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) { |
| 49 |
if ('"' === $part) { |
| 50 |
$escapedArgument .= '\\"'; |
| 51 |
} elseif (self::isSurroundedBy($part, '%')) { |
| 52 |
// Avoid environment variable expansion |
| 53 |
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%'; |
| 54 |
} else { |
| 55 |
// escape trailing backslash |
| 56 |
if ('\\' === substr($part, -1)) { |
| 57 |
$part .= '\\'; |
| 58 |
} |
| 59 |
$quote = true; |
| 60 |
$escapedArgument .= $part; |
| 61 |
} |
| 62 |
} |
| 63 |
if ($quote) { |
| 64 |
$escapedArgument = '"'.$escapedArgument.'"'; |
| 65 |
} |
| 66 |
|
| 67 |
return $escapedArgument; |
| 68 |
} |
| 69 |
|
| 70 |
return escapeshellarg($argument); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Validates and normalizes a Process input. |
| 75 |
* |
| 76 |
* @param string $caller The name of method call that validates the input |
| 77 |
* @param mixed $input The input to validate |
| 78 |
* |
| 79 |
* @return string The validated input |
| 80 |
* |
| 81 |
* @throws InvalidArgumentException In case the input is not valid |
| 82 |
*/ |
| 83 |
public static function validateInput($caller, $input) |
| 84 |
{ |
| 85 |
if (null !== $input) { |
| 86 |
if (is_resource($input)) { |
| 87 |
return $input; |
| 88 |
} |
| 89 |
if (is_scalar($input)) { |
| 90 |
return (string) $input; |
| 91 |
} |
| 92 |
|
| 93 |
throw new Symfony_Process_Exception_InvalidArgumentException(sprintf('%s only accepts strings or stream resources.', $caller)); |
| 94 |
} |
| 95 |
|
| 96 |
return $input; |
| 97 |
} |
| 98 |
|
| 99 |
private static function isSurroundedBy($arg, $char) |
| 100 |
{ |
| 101 |
return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1]; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* @return bool |
| 106 |
*/ |
| 107 |
public static function isWindows() |
| 108 |
{ |
| 109 |
return '\\' === DIRECTORY_SEPARATOR; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Backport of PHP >=5.3 function array_replace |
| 114 |
* |
| 115 |
* @link http://www.php.net/manual/en/function.array-replace.php#94458 |
| 116 |
*/ |
| 117 |
public static function arrayReplace(array $array, array $array1) |
| 118 |
{ |
| 119 |
$args = func_get_args(); |
| 120 |
$count = func_num_args(); |
| 121 |
|
| 122 |
for ($i = 0; $i < $count; ++$i) { |
| 123 |
if (is_array($args[$i])) { |
| 124 |
foreach ($args[$i] as $key => $val) { |
| 125 |
$array[$key] = $val; |
| 126 |
} |
| 127 |
} else { |
| 128 |
trigger_error( |
| 129 |
__FUNCTION__.'(): Argument #'.($i + 1).' is not an array', |
| 130 |
E_USER_WARNING |
| 131 |
); |
| 132 |
|
| 133 |
return null; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
return $array; |
| 138 |
} |
| 139 |
} |
| 140 |
|