4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process;
14 use Symfony\Component\Process\Exception\InvalidArgumentException;
17 * ProcessUtils is a bunch of utility methods.
19 * This class contains static methods only and is not meant to be instantiated.
21 * @author Martin HasoĊ <martin.hason@gmail.com>
26 * This class should not be instantiated.
28 private function __construct()
33 * Escapes a string to be used as a shell argument.
35 * @param string $argument The argument that will be escaped
37 * @return string The escaped argument
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.
41 public static function escapeArgument($argument)
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);
45 //Fix for PHP bug #43784 escapeshellarg removes % from given string
46 //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows
47 //@see https://bugs.php.net/bug.php?id=43784
48 //@see https://bugs.php.net/bug.php?id=49446
49 if ('\\' === \DIRECTORY_SEPARATOR) {
50 if ('' === $argument) {
51 return escapeshellarg($argument);
54 $escapedArgument = '';
56 foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
58 $escapedArgument .= '\\"';
59 } elseif (self::isSurroundedBy($part, '%')) {
60 // Avoid environment variable expansion
61 $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%';
63 // escape trailing backslash
64 if ('\\' === substr($part, -1)) {
68 $escapedArgument .= $part;
72 $escapedArgument = '"'.$escapedArgument.'"';
75 return $escapedArgument;
78 return "'".str_replace("'", "'\\''", $argument)."'";
82 * Validates and normalizes a Process input.
84 * @param string $caller The name of method call that validates the input
85 * @param mixed $input The input to validate
87 * @return mixed The validated input
89 * @throws InvalidArgumentException In case the input is not valid
91 public static function validateInput($caller, $input)
93 if (null !== $input) {
94 if (\is_resource($input)) {
97 if (\is_string($input)) {
100 if (is_scalar($input)) {
101 return (string) $input;
103 if ($input instanceof Process) {
104 return $input->getIterator($input::ITER_SKIP_ERR);
106 if ($input instanceof \Iterator) {
109 if ($input instanceof \Traversable) {
110 return new \IteratorIterator($input);
113 throw new InvalidArgumentException(sprintf('%s only accepts strings, Traversable objects or stream resources.', $caller));
119 private static function isSurroundedBy($arg, $char)
121 return 2 < \strlen($arg) && $char === $arg[0] && $char === $arg[\strlen($arg) - 1];