You are here

public static function ProcessUtils::escapeArgument in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/process/ProcessUtils.php \Symfony\Component\Process\ProcessUtils::escapeArgument()

Escapes a string to be used as a shell argument.

Parameters

string $argument The argument that will be escaped:

Return value

string The escaped argument

2 calls to ProcessUtils::escapeArgument()
Process::start in vendor/symfony/process/Process.php
Starts the process and returns after writing the input to STDIN.
ProcessUtilsTest::testEscapeArgument in vendor/symfony/process/Tests/ProcessUtilsTest.php
@dataProvider dataArguments

File

vendor/symfony/process/ProcessUtils.php, line 39

Class

ProcessUtils
ProcessUtils is a bunch of utility methods.

Namespace

Symfony\Component\Process

Code

public static function escapeArgument($argument) {

  //Fix for PHP bug #43784 escapeshellarg removes % from given string

  //Fix for PHP bug #49446 escapeshellarg doesn't work on Windows

  //@see https://bugs.php.net/bug.php?id=43784

  //@see https://bugs.php.net/bug.php?id=49446
  if ('\\' === DIRECTORY_SEPARATOR) {
    if ('' === $argument) {
      return escapeshellarg($argument);
    }
    $escapedArgument = '';
    $quote = false;
    foreach (preg_split('/(")/', $argument, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE) as $part) {
      if ('"' === $part) {
        $escapedArgument .= '\\"';
      }
      elseif (self::isSurroundedBy($part, '%')) {

        // Avoid environment variable expansion
        $escapedArgument .= '^%"' . substr($part, 1, -1) . '"^%';
      }
      else {

        // escape trailing backslash
        if ('\\' === substr($part, -1)) {
          $part .= '\\';
        }
        $quote = true;
        $escapedArgument .= $part;
      }
    }
    if ($quote) {
      $escapedArgument = '"' . $escapedArgument . '"';
    }
    return $escapedArgument;
  }
  return escapeshellarg($argument);
}