think\process\Utils::escapeArgument PHP Method

escapeArgument() public static method

转义字符串
public static escapeArgument ( string $argument ) : string
$argument string
return string
    public static function escapeArgument($argument)
    {
        if ('' === $argument) {
            return escapeshellarg($argument);
        }
        $escapedArgument = '';
        $quote = false;
        foreach (preg_split('/(")/i', $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;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * 启动进程并写到 STDIN 输入后返回。
  * @param callable|null $callback
  * @throws \RuntimeException
  * @throws \RuntimeException
  * @throws \LogicException
  */
 public function start($callback = null)
 {
     if ($this->isRunning()) {
         throw new \RuntimeException('Process is already running');
     }
     if ($this->outputDisabled && null !== $callback) {
         throw new \LogicException('Output has been disabled, enable it to allow the use of a callback.');
     }
     $this->resetProcessData();
     $this->starttime = $this->lastOutputTime = microtime(true);
     $this->callback = $this->buildCallback($callback);
     $descriptors = $this->getDescriptors();
     $commandline = $this->commandline;
     if ('\\' === DS && $this->enhanceWindowsCompatibility) {
         $commandline = 'cmd /V:ON /E:ON /C "(' . $commandline . ')';
         foreach ($this->processPipes->getFiles() as $offset => $filename) {
             $commandline .= ' ' . $offset . '>' . Utils::escapeArgument($filename);
         }
         $commandline .= '"';
         if (!isset($this->options['bypass_shell'])) {
             $this->options['bypass_shell'] = true;
         }
     }
     $this->process = proc_open($commandline, $descriptors, $this->processPipes->pipes, $this->cwd, $this->env, $this->options);
     if (!is_resource($this->process)) {
         throw new \RuntimeException('Unable to launch a new process.');
     }
     $this->status = self::STATUS_STARTED;
     if ($this->tty) {
         return;
     }
     $this->updateStatus(false);
     $this->checkTimeout();
 }