| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
class ProcessUtils |
| 11 |
{ |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public static function escapeArgument($argument) |
| 19 |
{ |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if ('\\' === DIRECTORY_SEPARATOR) { |
| 25 |
if ($argument === '') { |
| 26 |
return '""'; |
| 27 |
} |
| 28 |
|
| 29 |
$escapedArgument = ''; |
| 30 |
$quote = false; |
| 31 |
|
| 32 |
foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) { |
| 33 |
if ($part === '"') { |
| 34 |
$escapedArgument .= '\\"'; |
| 35 |
} elseif (self::isSurroundedBy($part, '%')) { |
| 36 |
|
| 37 |
$escapedArgument .= '^%"'.substr($part, 1, -1).'"^%'; |
| 38 |
} else { |
| 39 |
|
| 40 |
if (str_ends_with($part, '\\')) { |
| 41 |
$part .= '\\'; |
| 42 |
} |
| 43 |
$quote = true; |
| 44 |
$escapedArgument .= $part; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
if ($quote) { |
| 49 |
$escapedArgument = '"'.$escapedArgument.'"'; |
| 50 |
} |
| 51 |
|
| 52 |
return $escapedArgument; |
| 53 |
} |
| 54 |
|
| 55 |
return "'".str_replace("'", "'\\''", $argument)."'"; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected static function isSurroundedBy($arg, $char) |
| 66 |
{ |
| 67 |
return strlen($arg) > 2 && $char === $arg[0] && $char === $arg[strlen($arg) - 1]; |
| 68 |
} |
| 69 |
} |