浏览代码

Merge pull request #131 from Seldaek/versions

Versions
Nils Adermann 13 年之前
父节点
当前提交
bd1c18d934

+ 6 - 3
src/Composer/Package/Loader/ArrayLoader.php

@@ -140,7 +140,7 @@ class ArrayLoader
             if (isset($config[$type])) {
                 $method = 'set'.ucfirst($description);
                 $package->{$method}(
-                    $this->loadLinksFromConfig($package->getName(), $description, $config[$type])
+                    $this->loadLinksFromConfig($package, $description, $config[$type])
                 );
             }
         }
@@ -152,12 +152,15 @@ class ArrayLoader
         return $package;
     }
 
-    private function loadLinksFromConfig($srcPackageName, $description, array $linksSpecs)
+    private function loadLinksFromConfig($package, $description, array $linksSpecs)
     {
         $links = array();
         foreach ($linksSpecs as $packageName => $constraint) {
+            if ('self.version' === $constraint) {
+                $constraint = $package->getVersion();
+            }
             $constraint = $this->versionParser->parseConstraints($constraint);
-            $links[]    = new Package\Link($srcPackageName, $packageName, $constraint, $description, $constraint);
+            $links[]    = new Package\Link($package->getName(), $packageName, $constraint, $description, $constraint);
         }
 
         return $links;

+ 16 - 4
src/Composer/Package/Version/VersionParser.php

@@ -135,18 +135,30 @@ class VersionParser
         // match wildcard constraints
         if (preg_match('{^(\d+)(?:\.(\d+))?(?:\.(\d+))?\.\*$}', $constraint, $matches)) {
             if (isset($matches[3])) {
-                $lowVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.0';
                 $highVersion = $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.9999999';
+                if ($matches[3] === '0') {
+                    $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
+                } else {
+                    $lowVersion = $matches[1] . '.' . $matches[2] . '.' . ($matches[3] - 1). '.9999999';
+                }
             } elseif (isset($matches[2])) {
-                $lowVersion = $matches[1] . '.' . $matches[2] . '.0.0';
                 $highVersion = $matches[1] . '.' . $matches[2] . '.9999999.9999999';
+                if ($matches[2] === '0') {
+                    $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
+                } else {
+                    $lowVersion = $matches[1] . '.' . ($matches[2] - 1) . '.9999999.9999999';
+                }
             } else {
-                $lowVersion = $matches[1] . '.0.0.0';
                 $highVersion = $matches[1] . '.9999999.9999999.9999999';
+                if ($matches[1] === '0') {
+                    return array(new VersionConstraint('<', $highVersion));
+                } else {
+                    $lowVersion = ($matches[1] - 1) . '.9999999.9999999.9999999';
+                }
             }
 
             return array(
-                new VersionConstraint('>=', $lowVersion),
+                new VersionConstraint('>', $lowVersion),
                 new VersionConstraint('<', $highVersion),
             );
         }

+ 39 - 0
tests/Composer/Test/Package/Loader/ArrayLoaderTest.php

@@ -0,0 +1,39 @@
+<?php
+
+/*
+ * This file is part of Composer.
+ *
+ * (c) Nils Adermann <naderman@naderman.de>
+ *     Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Composer\Test\Package\Loader;
+
+use Composer\Package\Loader\ArrayLoader;
+
+class ArrayLoaderTest extends \PHPUnit_Framework_TestCase
+{
+    public function setUp()
+    {
+        $this->manager = $this->getMock('Composer\Repository\RepositoryManager');
+        $this->loader = new ArrayLoader($this->manager);
+    }
+
+    public function testSelfVersion()
+    {
+        $config = array(
+            'name' => 'A',
+            'version' => '1.2.3.4',
+            'replace' => array(
+                'foo' => 'self.version',
+            ),
+        );
+
+        $package = $this->loader->load($config);
+        $replaces = $package->getReplaces();
+        $this->assertEquals('== 1.2.3.4', (string) $replaces[0]->getConstraint());
+    }
+}

+ 12 - 7
tests/Composer/Test/Package/Version/VersionParserTest.php

@@ -132,7 +132,11 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
     public function testParseConstraintsWildcard($input, $min, $max)
     {
         $parser = new VersionParser;
-        $expected = new MultiConstraint(array($min, $max));
+        if ($min) {
+            $expected = new MultiConstraint(array($min, $max));
+        } else {
+            $expected = $max;
+        }
 
         $this->assertSame((string) $expected, (string) $parser->parseConstraints($input));
     }
@@ -140,12 +144,13 @@ class VersionParserTest extends \PHPUnit_Framework_TestCase
     public function wildcardConstraints()
     {
         return array(
-            array('2.*',     new VersionConstraint('>=', '2.0.0.0'), new VersionConstraint('<', '2.9999999.9999999.9999999')),
-            array('20.*',    new VersionConstraint('>=', '20.0.0.0'), new VersionConstraint('<', '20.9999999.9999999.9999999')),
-            array('2.0.*',   new VersionConstraint('>=', '2.0.0.0'), new VersionConstraint('<', '2.0.9999999.9999999')),
-            array('2.2.*',   new VersionConstraint('>=', '2.2.0.0'), new VersionConstraint('<', '2.2.9999999.9999999')),
-            array('2.10.*',  new VersionConstraint('>=', '2.10.0.0'), new VersionConstraint('<', '2.10.9999999.9999999')),
-            array('2.1.3.*', new VersionConstraint('>=', '2.1.3.0'), new VersionConstraint('<', '2.1.3.9999999')),
+            array('2.*',     new VersionConstraint('>', '1.9999999.9999999.9999999'), new VersionConstraint('<', '2.9999999.9999999.9999999')),
+            array('20.*',    new VersionConstraint('>', '19.9999999.9999999.9999999'), new VersionConstraint('<', '20.9999999.9999999.9999999')),
+            array('2.0.*',   new VersionConstraint('>', '1.9999999.9999999.9999999'), new VersionConstraint('<', '2.0.9999999.9999999')),
+            array('2.2.*',   new VersionConstraint('>', '2.1.9999999.9999999'), new VersionConstraint('<', '2.2.9999999.9999999')),
+            array('2.10.*',  new VersionConstraint('>', '2.9.9999999.9999999'), new VersionConstraint('<', '2.10.9999999.9999999')),
+            array('2.1.3.*', new VersionConstraint('>', '2.1.2.9999999'), new VersionConstraint('<', '2.1.3.9999999')),
+            array('0.*',     null, new VersionConstraint('<', '0.9999999.9999999.9999999')),
         );
     }