浏览代码

Made env variable parsing in path replacements generic across platforms and replaced old config.php implementation.

Niels Keurentjes 9 年之前
父节点
当前提交
c9534d48c1

+ 4 - 4
doc/05-repositories.md

@@ -668,10 +668,10 @@ Forcing mirroring can be useful when deploying or generating package from a mono
 ```
 ```
 
 
 Leading tildes are expanded to the current user's home folder, and environment
 Leading tildes are expanded to the current user's home folder, and environment
-variables are parsed according to host platform. For example `~/git/mypackage`
-will automatically load the mypackage clone from `/home/<username>/git/mypackage`,
-which is equivalent to `$HOME/git/mypackage` on Linux/Mac or
-`%USERPROFILE%/git/mypackage` on Windows.
+variables are parsed in both Windows and Linux/Mac notations. For example
+`~/git/mypackage` will automatically load the mypackage clone from
+`/home/<username>/git/mypackage`, equivalent to `$HOME/git/mypackage` or
+`%USERPROFILE%/git/mypackage`.
 
 
 > **Note:** Repository paths can also contain wildcards like ``*`` and ``?``.
 > **Note:** Repository paths can also contain wildcards like ``*`` and ``?``.
 > For details, see the [PHP glob function](http://php.net/glob).
 > For details, see the [PHP glob function](http://php.net/glob).

+ 2 - 1
src/Composer/Config.php

@@ -14,6 +14,7 @@ namespace Composer;
 
 
 use Composer\Config\ConfigSourceInterface;
 use Composer\Config\ConfigSourceInterface;
 use Composer\Downloader\TransportException;
 use Composer\Downloader\TransportException;
+use Composer\Util\Platform;
 
 
 /**
 /**
  * @author Jordi Boggiano <j.boggiano@seld.be>
  * @author Jordi Boggiano <j.boggiano@seld.be>
@@ -207,7 +208,7 @@ class Config
                 $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
                 $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
 
 
                 $val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key], $flags), '/\\');
                 $val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key], $flags), '/\\');
-                $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val);
+                $val = Platform::expandPath($val);
 
 
                 if (substr($key, -4) !== '-dir') {
                 if (substr($key, -4) !== '-dir') {
                     return $val;
                     return $val;

+ 2 - 2
src/Composer/Util/Platform.php

@@ -30,8 +30,8 @@ class Platform
         if (preg_match('#^~[/\\\\]#', $path)) {
         if (preg_match('#^~[/\\\\]#', $path)) {
             return self::getUserDirectory() . substr($path, 1);
             return self::getUserDirectory() . substr($path, 1);
         }
         }
-        return preg_replace_callback(self::isWindows() ? '#^(%(\\w+)%)[/\\\\]#' : '#^(\\$(\\w+))/#', function($matches) {
-            return getenv($matches[2]) . '/';
+        return preg_replace_callback('#^([\\$%])(\\w+)\\1?(([/\\\\].*)?)#', function($matches) {
+            return getenv($matches[2]) . $matches[3];
         }, $path);
         }, $path);
     }
     }
 
 

+ 1 - 2
tests/Composer/Test/ConfigTest.php

@@ -13,7 +13,6 @@
 namespace Composer\Test;
 namespace Composer\Test;
 
 
 use Composer\Config;
 use Composer\Config;
-use Composer\Downloader\TransportException;
 
 
 class ConfigTest extends \PHPUnit_Framework_TestCase
 class ConfigTest extends \PHPUnit_Framework_TestCase
 {
 {
@@ -151,7 +150,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
 
 
         $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
         $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/');
         $this->assertEquals('b', $config->get('c'));
         $this->assertEquals('b', $config->get('c'));
-        $this->assertEquals($home.'/', $config->get('bin-dir'));
+        $this->assertEquals($home, $config->get('bin-dir'));
         $this->assertEquals($home.'/foo', $config->get('cache-dir'));
         $this->assertEquals($home.'/foo', $config->get('cache-dir'));
     }
     }
 
 

+ 2 - 5
tests/Composer/Test/Util/PlatformTest.php

@@ -24,11 +24,8 @@ class PlatformTest extends \PHPUnit_Framework_TestCase
     public function testExpandPath()
     public function testExpandPath()
     {
     {
         putenv('TESTENV=/home/test');
         putenv('TESTENV=/home/test');
-        if (Platform::isWindows()) {
-            $this->assertEquals('/home/test/myPath', Platform::expandPath('%TESTENV%/myPath'));
-        } else {
-            $this->assertEquals('/home/test/myPath', Platform::expandPath('$TESTENV/myPath'));
-        }
+        $this->assertEquals('/home/test/myPath', Platform::expandPath('%TESTENV%/myPath'));
+        $this->assertEquals('/home/test/myPath', Platform::expandPath('$TESTENV/myPath'));
         $this->assertEquals((getenv('HOME') ?: getenv('USERPROFILE')) . '/test', Platform::expandPath('~/test'));
         $this->assertEquals((getenv('HOME') ?: getenv('USERPROFILE')) . '/test', Platform::expandPath('~/test'));
     }
     }