Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ProcessExecutor.php

function ProcessExecutor::escapeArgument

Escapes a string to be used as a shell argument for Symfony Process.

This method expects cmd.exe to be started with the /V:ON option, which enables delayed environment variable expansion using ! as the delimiter. If this is not the case, any escaped ^^!var^^! will be transformed to ^!var^! and introduce two unintended carets.

Modified from https://github.com/johnstevenson/winbox-args MIT Licensed (c) John Stevenson <john-stevenson@blueyonder.co.uk>

Parameters

string|false|null $argument:

1 call to ProcessExecutor::escapeArgument()
ProcessExecutor::escape in vendor/composer/composer/src/Composer/Util/ProcessExecutor.php
Escapes a string to be used as a shell argument.

File

vendor/composer/composer/src/Composer/Util/ProcessExecutor.php, line 508

Class

ProcessExecutor
@author Robert Schönthal <seroscho@googlemail.com> @author Jordi Boggiano <j.boggiano@seld.be>

Namespace

Composer\Util

Code

private static function escapeArgument($argument) : string {
    if ('' === ($argument = (string) $argument)) {
        return escapeshellarg($argument);
    }
    if (!Platform::isWindows()) {
        return "'" . str_replace("'", "'\\''", $argument) . "'";
    }
    // New lines break cmd.exe command parsing
    // and special chars like the fullwidth quote can be used to break out
    // of parameter encoding via "Best Fit" encoding conversion
    $argument = strtr($argument, [
        "\n" => ' ',
        """ => '"',
        "ʺ" => '"',
        "〝" => '"',
        "〞" => '"',
        "̎" => '"',
        ":" => ':',
        "։" => ':',
        "∶" => ':',
        "/" => '/',
        "⁄" => '/',
        "∕" => '/',
        "´" => '/',
    ]);
    // In addition to whitespace, commas need quoting to preserve paths
    $quote = strpbrk($argument, " \t,") !== false;
    $argument = Preg::replace('/(\\\\*)"/', '$1$1\\"', $argument, -1, $dquotes);
    $meta = $dquotes > 0 || Preg::isMatch('/%[^%]+%|![^!]+!/', $argument);
    if (!$meta && !$quote) {
        $quote = strpbrk($argument, '^&|<>()') !== false;
    }
    if ($quote) {
        $argument = '"' . Preg::replace('/(\\\\*)$/', '$1$1', $argument) . '"';
    }
    if ($meta) {
        $argument = Preg::replace('/(["^&|<>()%])/', '^$1', $argument);
        $argument = Preg::replace('/(!)/', '^^$1', $argument);
    }
    return $argument;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal