classes/phing/types/Commandline.php
public static function translateCommandline(string $toProcess = null): array
    {
        if ($toProcess === null || $toProcess === '') {
            return [];
        }

        // parse with a simple finite state machine

        $normal = 0;
        $inQuote = 1;
        $inDoubleQuote = 2;

        $state = $normal;
        $args = [];
        $current = "";
        $lastTokenHasBeenQuoted = false;

        $tokens = preg_split('/(["\' ])/', $toProcess, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        while (($nextTok = array_shift($tokens)) !== null) {
            switch ($state) {
                case $inQuote:
                    if ("'" === $nextTok) {
                        $lastTokenHasBeenQuoted = true;
                        $state = $normal;
                    } else {
                        $current .= $nextTok;
                    }
                    break;
                case $inDoubleQuote:
                    if ("\"" === $nextTok) {
                        $lastTokenHasBeenQuoted = true;
                        $state = $normal;
                    } else {
                        $current .= $nextTok;
                    }
                    break;
                default:
                    if ("'" === $nextTok) {
                        $state = $inQuote;
                    } elseif ("\"" === $nextTok) {
                        $state = $inDoubleQuote;
                    } elseif (" " === $nextTok) {
                        if ($lastTokenHasBeenQuoted || $current !== '') {
                            $args[] = $current;
                            $current = "";
                        }
                    } else {
                        $current .= $nextTok;
                    }
                    $lastTokenHasBeenQuoted = false;
                    break;
            }
        }

        if ($lastTokenHasBeenQuoted || $current !== '') {
            $args[] = $current;
        }

        if ($state === $inQuote || $state === $inDoubleQuote) {
            throw new BuildException('unbalanced quotes in ' . $toProcess);
        }

        return $args;
    }
public static function translateCommandline(string $toProcess = null): array
    {
        if ($toProcess === null || $toProcess === '') {
            return [];
        }

        // parse with a simple finite state machine

        $normal = 0;
        $inQuote = 1;
        $inDoubleQuote = 2;

        $state = $normal;
        $args = [];
        $current = "";
        $lastTokenHasBeenQuoted = false;

        $tokens = preg_split('/(["\' ])/', $toProcess, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        while (($nextTok = array_shift($tokens)) !== null) {
            switch ($state) {
                case $inQuote:
                    if ("'" === $nextTok) {
                        $lastTokenHasBeenQuoted = true;
                        $state = $normal;
                    } else {
                        $current .= $nextTok;
                    }
                    break;
                case $inDoubleQuote:
                    if ("\"" === $nextTok) {
                        $lastTokenHasBeenQuoted = true;
                        $state = $normal;
                    } else {
                        $current .= $nextTok;
                    }
                    break;
                default:
                    if ("'" === $nextTok) {
                        $state = $inQuote;
                    } elseif ("\"" === $nextTok) {
                        $state = $inDoubleQuote;
                    } elseif (" " === $nextTok) {
                        if ($lastTokenHasBeenQuoted || $current !== '') {
                            $args[] = $current;
                            $current = "";
                        }
                    } else {
                        $current .= $nextTok;
                    }
                    $lastTokenHasBeenQuoted = false;
                    break;
            }
        }

        if ($lastTokenHasBeenQuoted || $current !== '') {
            $args[] = $current;
        }

        if ($state === $inQuote || $state === $inDoubleQuote) {
            throw new BuildException('unbalanced quotes in ' . $toProcess);
        }

        return $args;
    }
public static function translateCommandline ( string $ toProcess = null ) : array { if ( $ toProcess === null || $ toProcess === '' ) { return [ ] ; } // parse with a simple finite state machine $ normal = 0 ; $ inQuote = 1 ; $ inDoubleQuote = 2 ; $ state = $ normal ; $ args = [ ] ; $ current = "" ; $ lastTokenHasBeenQuoted = false ; $ tokens = preg_split ( '/(["\' ])/' , $ toProcess , - 1 , PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ) ; while ( ( $ nextTok = array_shift ( $ tokens ) ) !== null ) { switch ( $ state ) { case $ inQuote : if ( "'" === $ nextTok ) { $ lastTokenHasBeenQuoted = true ; $ state = $ normal ; } else { $ current .= $ nextTok ; } break ; case $ inDoubleQuote : if ( "\"" === $ nextTok ) { $ lastTokenHasBeenQuoted = true ; $ state = $ normal ; } else { $ current .= $ nextTok ; } break ; default : if ( "'" === $ nextTok ) { $ state = $ inQuote ; } elseif ( "\"" === $ nextTok ) { $ state = $ inDoubleQuote ; } elseif ( " " === $ nextTok ) { if ( $ lastTokenHasBeenQuoted || $ current !== '' ) { $ args [ ] = $ current ; $ current = "" ; } } else { $ current .= $ nextTok ; } $ lastTokenHasBeenQuoted = false ; break ; } } if ( $ lastTokenHasBeenQuoted || $ current !== '' ) { $ args [ ] = $ current ; } if ( $ state === $ inQuote || $ state === $ inDoubleQuote ) { throw new BuildException ( 'unbalanced quotes in ' . $ toProcess ) ; } return $ args ; }
Crack a command line.

@param string $toProcess the command line to process.

@return string[] the command line broken into strings.
An empty or null toProcess parameter results in a zero sized array.

@throws \BuildException
Crack a command line .