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@14
|
38 *
|
Chris@14
|
39 * @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.
|
Chris@0
|
40 */
|
Chris@0
|
41 public static function escapeArgument($argument)
|
Chris@0
|
42 {
|
Chris@14
|
43 @trigger_error('The '.__METHOD__.'() method is deprecated since Symfony 3.3 and will be removed in 4.0. Use a command line array or give env vars to the Process::start/run() method instead.', E_USER_DEPRECATED);
|
Chris@14
|
44
|
Chris@0
|
45 //Fix for PHP bug #43784 escapeshellarg removes % from given string
|
Chris@0
|
46 //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
|
Chris@0
|
47 //@see https://bugs.php.net/bug.php?id=43784
|
Chris@0
|
48 //@see https://bugs.php.net/bug.php?id=49446
|
Chris@17
|
49 if ('\\' === \DIRECTORY_SEPARATOR) {
|
Chris@0
|
50 if ('' === $argument) {
|
Chris@0
|
51 return escapeshellarg($argument);
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 $escapedArgument = '';
|
Chris@0
|
55 $quote = false;
|
Chris@0
|
56 foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
|
Chris@0
|
57 if ('"' === $part) {
|
Chris@0
|
58 $escapedArgument .= '\\"';
|
Chris@0
|
59 } elseif (self::isSurroundedBy($part, '%')) {
|
Chris@0
|
60 // Avoid environment variable expansion
|
Chris@0
|
61 $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
|
Chris@0
|
62 } else {
|
Chris@0
|
63 // escape trailing backslash
|
Chris@0
|
64 if ('\\' === substr($part, -1)) {
|
Chris@0
|
65 $part .= '\\';
|
Chris@0
|
66 }
|
Chris@0
|
67 $quote = true;
|
Chris@0
|
68 $escapedArgument .= $part;
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|
Chris@0
|
71 if ($quote) {
|
Chris@0
|
72 $escapedArgument = '"'.$escapedArgument.'"';
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 return $escapedArgument;
|
Chris@0
|
76 }
|
Chris@0
|
77
|
Chris@0
|
78 return "'".str_replace("'", "'\\''", $argument)."'";
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * Validates and normalizes a Process input.
|
Chris@0
|
83 *
|
Chris@0
|
84 * @param string $caller The name of method call that validates the input
|
Chris@0
|
85 * @param mixed $input The input to validate
|
Chris@0
|
86 *
|
Chris@0
|
87 * @return mixed The validated input
|
Chris@0
|
88 *
|
Chris@0
|
89 * @throws InvalidArgumentException In case the input is not valid
|
Chris@0
|
90 */
|
Chris@0
|
91 public static function validateInput($caller, $input)
|
Chris@0
|
92 {
|
Chris@0
|
93 if (null !== $input) {
|
Chris@17
|
94 if (\is_resource($input)) {
|
Chris@0
|
95 return $input;
|
Chris@0
|
96 }
|
Chris@17
|
97 if (\is_string($input)) {
|
Chris@0
|
98 return $input;
|
Chris@0
|
99 }
|
Chris@0
|
100 if (is_scalar($input)) {
|
Chris@0
|
101 return (string) $input;
|
Chris@0
|
102 }
|
Chris@0
|
103 if ($input instanceof Process) {
|
Chris@0
|
104 return $input->getIterator($input::ITER_SKIP_ERR);
|
Chris@0
|
105 }
|
Chris@0
|
106 if ($input instanceof \Iterator) {
|
Chris@0
|
107 return $input;
|
Chris@0
|
108 }
|
Chris@0
|
109 if ($input instanceof \Traversable) {
|
Chris@0
|
110 return new \IteratorIterator($input);
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 throw new InvalidArgumentException(sprintf('%s only accepts strings, Traversable objects or stream resources.', $caller));
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 return $input;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 private static function isSurroundedBy($arg, $char)
|
Chris@0
|
120 {
|
Chris@17
|
121 return 2 < \strlen($arg) && $char === $arg[0] && $char === $arg[\strlen($arg) - 1];
|
Chris@0
|
122 }
|
Chris@0
|
123 }
|