From 3bf1ee939b750b976e53097a2fca919529914094 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Sat, 18 Oct 2014 12:46:13 +0200 Subject: [PATCH] Fixed validator to accept 'a/b' and 'a/b ~2.3' --- src/Composer/Command/InitCommand.php | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 462b6d438..d4cd56b57 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -328,6 +328,7 @@ EOT return $result; } + $versionParser = new VersionParser(); while (null !== $package = $dialog->ask($output, $prompt)) { $matches = $this->findPackages($package); @@ -353,22 +354,32 @@ EOT $output->writeln($choices); $output->writeln(''); - $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]; + + return $package['name']; } - if (!isset($matches[(int) $selection])) { - throw new \Exception('Not a valid selection'); + if (preg_match('{^\s*(?P[\S/]+)(?:\s+(?P\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']; } - $package = $matches[(int) $selection]; - - 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);