Browse Source

Fix up version parsing

Jordi Boggiano 14 năm trước cách đây
mục cha
commit
e09f6900da

+ 12 - 5
src/Composer/Package/Version/VersionParser.php

@@ -34,7 +34,7 @@ class VersionParser
     {
         $version = trim($version);
 
-        if (in_array($version, array('master', 'trunk'))) {
+        if (preg_match('{^(?:master|trunk)(?:[.-]?dev)?$}i', $version)) {
             return '9999999-dev';
         }
 
@@ -66,6 +66,12 @@ class VersionParser
             return $version;
         }
 
+        if (preg_match('{(.*?)[.-]?dev$}i', $version, $match)) {
+            try {
+                return $this->normalizeBranch($match[1]);
+            } catch (\Exception $e) {}
+        }
+
         throw new \UnexpectedValueException('Invalid version string '.$version);
     }
 
@@ -146,10 +152,11 @@ class VersionParser
         }
 
         // match operators constraints
-        if (preg_match('{^(>=?|<=?|==?)?\s*(\d+.*)}', $constraint, $matches)) {
-            $version = $this->normalize($matches[2]);
-
-            return array(new VersionConstraint($matches[1] ?: '=', $version));
+        if (preg_match('{^(>=?|<=?|==?)?\s*(.*)}', $constraint, $matches)) {
+            try {
+                $version = $this->normalize($matches[2]);
+                return array(new VersionConstraint($matches[1] ?: '=', $version));
+            } catch (\Exception $e) {}
         }
 
         throw new \UnexpectedValueException('Could not parse version constraint '.$constraint);

+ 3 - 0
tests/Composer/Test/Package/Version/VersionParserTest.php

@@ -51,6 +51,7 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
             'parses dt+patch'   => array('20100102-203040-p1',  '20100102-203040-patch1'),
             'parses master'     => array('master',              '9999999-dev'),
             'parses trunk'      => array('trunk',               '9999999-dev'),
+            'parses trunk/2'    => array('trunk-dev',           '9999999-dev'),
         );
     }
 
@@ -119,6 +120,8 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
             'no op means eq'    => array('1.2.3',       new VersionConstraint('=', '1.2.3.0')),
             'completes version' => array('=1.0',        new VersionConstraint('=', '1.0.0.0')),
             'accepts spaces'    => array('>= 1.2.3',    new VersionConstraint('>=', '1.2.3.0')),
+            'accepts master'    => array('>=master-dev',    new VersionConstraint('>=', '9999999-dev')),
+            'accepts master/2'  => array('master-dev',      new VersionConstraint('=', '9999999-dev')),
         );
     }