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

Breadcrumb

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

function Process::prepareWindowsCommandLine

1 call to Process::prepareWindowsCommandLine()
Process::start in vendor/symfony/process/Process.php
Starts the process and returns after writing the input to STDIN.

File

vendor/symfony/process/Process.php, line 1556

Class

Process
Process is a thin wrapper around proc_* functions to easily start independent PHP processes.

Namespace

Symfony\Component\Process

Code

private function prepareWindowsCommandLine(string|array $cmd, array &$env) : string {
    $cmd = $this->buildShellCommandline($cmd);
    $uid = bin2hex(random_bytes(4));
    $cmd = preg_replace_callback('/"(?:(
                [^"%!^]*+
                (?:
                    (?: !LF! | "(?:\\^[%!^])?+" )
                    [^"%!^]*+
                )++
            ) | [^"]*+ )"/x', function ($m) use (&$env, $uid) {
        static $varCount = 0;
        static $varCache = [];
        if (!isset($m[1])) {
            return $m[0];
        }
        if (isset($varCache[$m[0]])) {
            return $varCache[$m[0]];
        }
        if (str_contains($value = $m[1], "\x00")) {
            $value = str_replace("\x00", '?', $value);
        }
        if (false === strpbrk($value, "\"%!\n")) {
            return '"' . $value . '"';
        }
        $value = str_replace([
            '!LF!',
            '"^!"',
            '"^%"',
            '"^^"',
            '""',
        ], [
            "\n",
            '!',
            '%',
            '^',
            '"',
        ], $value);
        $value = '"' . preg_replace('/(\\\\*)"/', '$1$1\\"', $value) . '"';
        $var = $uid . ++$varCount;
        $env[$var] = $value;
        return $varCache[$m[0]] = '!' . $var . '!';
    }, $cmd);
    static $comSpec;
    if (!$comSpec && ($comSpec = (new ExecutableFinder())->find('cmd.exe'))) {
        // Escape according to CommandLineToArgvW rules
        $comSpec = '"' . preg_replace('{(\\\\*+)"}', '$1$1\\"', $comSpec) . '"';
    }
    $cmd = ($comSpec ?? 'cmd') . ' /V:ON /E:ON /D /C (' . str_replace("\n", ' ', $cmd) . ')';
    foreach ($this->processPipes
        ->getFiles() as $offset => $filename) {
        $cmd .= ' ' . $offset . '>"' . $filename . '"';
    }
    return $cmd;
}
RSS feed
Powered by Drupal