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