Browse Source

Merge pull request #3365 from WouterJ/patch-1

Updated validator
Jordi Boggiano 10 years ago
parent
commit
ab3622dff1
1 changed files with 19 additions and 8 deletions
  1. 19 8
      src/Composer/Command/InitCommand.php

+ 19 - 8
src/Composer/Command/InitCommand.php

@@ -329,6 +329,7 @@ EOT
             return $result;
         }
 
+        $versionParser = new VersionParser();
         while (null !== $package = $dialog->ask($output, $prompt)) {
             $matches = $this->findPackages($package);
 
@@ -354,22 +355,32 @@ EOT
                     $this->getIO()->writeError($choices);
                     $this->getIO()->writeError('');
 
-                    $validator = function ($selection) use ($matches) {
+                    $validator = function ($selection) use ($matches, $versionParser) {
                         if ('' === $selection) {
                             return false;
                         }
 
-                        if (!is_numeric($selection) && preg_match('{^\s*(\S+)\s+(\S.*)\s*$}', $selection, $matches)) {
-                            return $matches[1].' '.$matches[2];
-                        }
+                        if (is_numeric($selection) && isset($matches[(int) $selection])) {
+                            $package = $matches[(int) $selection];
 
-                        if (!isset($matches[(int) $selection])) {
-                            throw new \Exception('Not a valid selection');
+                            return $package['name'];
                         }
 
-                        $package = $matches[(int) $selection];
+                        if (preg_match('{^\s*(?P<name>[\S/]+)(?:\s+(?P<version>\S+))?\s*$}', $selection, $packageMatches)) {
+                            if (isset($packageMatches['version'])) {
+                                // parsing `acme/example ~2.3`
+
+                                // validate version constraint
+                                $versionParser->parseConstraints($packageMatches['version']);
+
+                                return $packageMatches['name'].' '.$packageMatches['version'];
+                            }
+
+                            // parsing `acme/example`
+                            return $packageMatches['name'];
+                        }
 
-                        return $package['name'];
+                        throw new \Exception('Not a valid selection');
                     };
 
                     $package = $dialog->askAndValidate($output, $dialog->getQuestion('Enter package # to add, or the complete package name if it is not listed', false, ':'), $validator, 3);