1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is derived from part of the Symfony package, which is |
5
|
|
|
* (c) Fabien Potencier <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Robo\Common; |
9
|
|
|
|
10
|
|
|
use Symfony\Component\Process\Exception\InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
/** |
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. |
16
|
|
|
*/ |
17
|
|
|
class ProcessUtils |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* This class should not be instantiated. |
21
|
|
|
*/ |
22
|
|
|
private function __construct() |
23
|
|
|
{ |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Escapes a string to be used as a shell argument. |
28
|
|
|
* |
29
|
|
|
* @param string $argument |
30
|
|
|
* The argument that will be escaped. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
* The escaped argument. |
34
|
|
|
* |
35
|
|
|
* @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. |
36
|
|
|
*/ |
37
|
|
|
public static function escapeArgument($argument) |
38
|
|
|
{ |
39
|
|
|
@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); |
|
|
|
|
40
|
|
|
|
41
|
|
|
//Fix for PHP bug #43784 escapeshellarg removes % from given string |
42
|
|
|
//Fix for PHP bug #49446 escapeshellarg doesn't work on Windows |
43
|
|
|
//@see https://bugs.php.net/bug.php?id=43784 |
44
|
|
|
//@see https://bugs.php.net/bug.php?id=49446 |
45
|
|
|
if ('\\' === DIRECTORY_SEPARATOR) { |
46
|
|
|
if ('' === $argument) { |
47
|
|
|
return escapeshellarg($argument); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$escapedArgument = ''; |
51
|
|
|
$quote = false; |
52
|
|
|
foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) { |
53
|
|
|
if ('"' === $part) { |
54
|
|
|
$escapedArgument .= '\\"'; |
55
|
|
|
} elseif (self::isSurroundedBy($part, '%')) { |
56
|
|
|
// Avoid environment variable expansion |
57
|
|
|
$escapedArgument .= '^%"' . substr($part, 1, -1) . '"^%'; |
58
|
|
|
} else { |
59
|
|
|
// escape trailing backslash |
60
|
|
|
if ('\\' === substr($part, -1)) { |
61
|
|
|
$part .= '\\'; |
62
|
|
|
} |
63
|
|
|
$quote = true; |
64
|
|
|
$escapedArgument .= $part; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
if ($quote) { |
68
|
|
|
$escapedArgument = '"' . $escapedArgument . '"'; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $escapedArgument; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return "'" . str_replace("'", "'\\''", $argument) . "'"; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private static function isSurroundedBy($arg, $char) |
78
|
|
|
{ |
79
|
|
|
return 2 < strlen($arg) && $char === $arg[0] && $char === $arg[strlen($arg) - 1]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: