Consolidation\Cgr\Application::separateProjectsFromArgs PHP Method

separateProjectsFromArgs() public method

After our options are removed by parseOutOurOptions, those items remaining in $argv will be separated into a list of projects and versions, and anything else that is not a project:version. Returns an array of two items containing: - An associative array, where the key is the project name and the value is the version (or an empty string, if no version was specified) - The remaining $argv items not used to build the projects array.
public separateProjectsFromArgs ( array $argv, $options ) : array
$argv array The $argv array from parseOutOurOptions()
return array
    public function separateProjectsFromArgs($argv, $options)
    {
        $cgrCommands = array('require', 'update', 'remove');
        $command = 'require';
        $composerArgs = array();
        $projects = array();
        $globalMode = !$options['composer'];
        foreach ($argv as $arg) {
            if ($arg[0] == '-') {
                // Any flags (first character is '-') will just be passed
                // through to to composer. Flags interpreted by cgr have
                // already been removed from $argv.
                $composerArgs[] = $arg;
            } elseif (strpos($arg, '/') !== false) {
                // Arguments containing a '/' name projects.  We will split
                // the project from its version, allowing the separator
                // character to be either a '=' or a ':', and then store the
                // result in the $projects array.
                $projectAndVersion = explode(':', strtr($arg, '=', ':'), 2) + array('', '');
                list($project, $version) = $projectAndVersion;
                $projects[$project] = $version;
            } elseif ($this->isComposerVersion($arg)) {
                // If an argument is a composer version, then we will alter
                // the last project we saw, attaching this version to it.
                // This allows us to handle 'a/b:1.0' and 'a/b 1.0' equivalently.
                $keys = array_keys($projects);
                $lastProject = array_pop($keys);
                unset($projects[$lastProject]);
                $projects[$lastProject] = $arg;
            } elseif ($arg == 'global') {
                // Make note if we see the 'global' command.
                $globalMode = true;
            } else {
                // If we see any command other than 'global [require|update|remove]',
                // then we will pass *all* of the arguments through to
                // composer unchanged. We return an empty projects array
                // to indicate that this should be a pass-through call
                // to composer, rather than one or more calls to
                // 'composer require' to install global projects.
                if (!$globalMode || !in_array($arg, $cgrCommands)) {
                    return array('', array(), $argv);
                }
                // Remember which command we saw
                $command = $arg;
            }
        }
        return array($command, $projects, $composerArgs);
    }