Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Process; Chris@0: Chris@0: use Symfony\Component\Process\Exception\InvalidArgumentException; Chris@0: Chris@0: /** Chris@0: * ProcessUtils is a bunch of utility methods. Chris@0: * Chris@0: * This class contains static methods only and is not meant to be instantiated. Chris@0: * Chris@0: * @author Martin HasoĊˆ Chris@0: */ Chris@0: class ProcessUtils Chris@0: { Chris@0: /** Chris@0: * This class should not be instantiated. Chris@0: */ Chris@0: private function __construct() Chris@0: { Chris@0: } Chris@0: Chris@0: /** Chris@0: * Escapes a string to be used as a shell argument. Chris@0: * Chris@0: * @param string $argument The argument that will be escaped Chris@0: * Chris@0: * @return string The escaped argument Chris@14: * Chris@14: * @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: */ Chris@0: public static function escapeArgument($argument) Chris@0: { Chris@14: @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: Chris@0: //Fix for PHP bug #43784 escapeshellarg removes % from given string Chris@0: //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows Chris@0: //@see https://bugs.php.net/bug.php?id=43784 Chris@0: //@see https://bugs.php.net/bug.php?id=49446 Chris@17: if ('\\' === \DIRECTORY_SEPARATOR) { Chris@0: if ('' === $argument) { Chris@0: return escapeshellarg($argument); Chris@0: } Chris@0: Chris@0: $escapedArgument = ''; Chris@0: $quote = false; Chris@0: foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) { Chris@0: if ('"' === $part) { Chris@0: $escapedArgument .= '\\"'; Chris@0: } elseif (self::isSurroundedBy($part, '%')) { Chris@0: // Avoid environment variable expansion Chris@0: $escapedArgument .= '^%"'.substr($part, 1, -1).'"^%'; Chris@0: } else { Chris@0: // escape trailing backslash Chris@0: if ('\\' === substr($part, -1)) { Chris@0: $part .= '\\'; Chris@0: } Chris@0: $quote = true; Chris@0: $escapedArgument .= $part; Chris@0: } Chris@0: } Chris@0: if ($quote) { Chris@0: $escapedArgument = '"'.$escapedArgument.'"'; Chris@0: } Chris@0: Chris@0: return $escapedArgument; Chris@0: } Chris@0: Chris@0: return "'".str_replace("'", "'\\''", $argument)."'"; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Validates and normalizes a Process input. Chris@0: * Chris@0: * @param string $caller The name of method call that validates the input Chris@0: * @param mixed $input The input to validate Chris@0: * Chris@0: * @return mixed The validated input Chris@0: * Chris@0: * @throws InvalidArgumentException In case the input is not valid Chris@0: */ Chris@0: public static function validateInput($caller, $input) Chris@0: { Chris@0: if (null !== $input) { Chris@17: if (\is_resource($input)) { Chris@0: return $input; Chris@0: } Chris@17: if (\is_string($input)) { Chris@0: return $input; Chris@0: } Chris@0: if (is_scalar($input)) { Chris@0: return (string) $input; Chris@0: } Chris@0: if ($input instanceof Process) { Chris@0: return $input->getIterator($input::ITER_SKIP_ERR); Chris@0: } Chris@0: if ($input instanceof \Iterator) { Chris@0: return $input; Chris@0: } Chris@0: if ($input instanceof \Traversable) { Chris@0: return new \IteratorIterator($input); Chris@0: } Chris@0: Chris@0: throw new InvalidArgumentException(sprintf('%s only accepts strings, Traversable objects or stream resources.', $caller)); Chris@0: } Chris@0: Chris@0: return $input; Chris@0: } Chris@0: Chris@0: private static function isSurroundedBy($arg, $char) Chris@0: { Chris@17: return 2 < \strlen($arg) && $char === $arg[0] && $char === $arg[\strlen($arg) - 1]; Chris@0: } Chris@0: }