From c9534d48c16102799c716ba924fc36acb560309a Mon Sep 17 00:00:00 2001 From: Niels Keurentjes Date: Wed, 13 Apr 2016 02:02:50 +0200 Subject: [PATCH] Made env variable parsing in path replacements generic across platforms and replaced old config.php implementation. --- doc/05-repositories.md | 8 ++++---- src/Composer/Config.php | 3 ++- src/Composer/Util/Platform.php | 4 ++-- tests/Composer/Test/ConfigTest.php | 3 +-- tests/Composer/Test/Util/PlatformTest.php | 7 ++----- 5 files changed, 11 insertions(+), 14 deletions(-) diff --git a/doc/05-repositories.md b/doc/05-repositories.md index 9887fd9fc..703d26266 100644 --- a/doc/05-repositories.md +++ b/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 -variables are parsed according to host platform. For example `~/git/mypackage` -will automatically load the mypackage clone from `/home//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//git/mypackage`, equivalent to `$HOME/git/mypackage` or +`%USERPROFILE%/git/mypackage`. > **Note:** Repository paths can also contain wildcards like ``*`` and ``?``. > For details, see the [PHP glob function](http://php.net/glob). diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 4c16913cf..13b456d40 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -14,6 +14,7 @@ namespace Composer; use Composer\Config\ConfigSourceInterface; use Composer\Downloader\TransportException; +use Composer\Util\Platform; /** * @author Jordi Boggiano @@ -207,7 +208,7 @@ class Config $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); $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') { return $val; diff --git a/src/Composer/Util/Platform.php b/src/Composer/Util/Platform.php index ada67c635..71aa028c5 100644 --- a/src/Composer/Util/Platform.php +++ b/src/Composer/Util/Platform.php @@ -30,8 +30,8 @@ class Platform if (preg_match('#^~[/\\\\]#', $path)) { 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); } diff --git a/tests/Composer/Test/ConfigTest.php b/tests/Composer/Test/ConfigTest.php index 9fd693122..0d4f6cb6f 100644 --- a/tests/Composer/Test/ConfigTest.php +++ b/tests/Composer/Test/ConfigTest.php @@ -13,7 +13,6 @@ namespace Composer\Test; use Composer\Config; -use Composer\Downloader\TransportException; class ConfigTest extends \PHPUnit_Framework_TestCase { @@ -151,7 +150,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $home = rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '\\/'); $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')); } diff --git a/tests/Composer/Test/Util/PlatformTest.php b/tests/Composer/Test/Util/PlatformTest.php index 985e25cd1..129410d95 100644 --- a/tests/Composer/Test/Util/PlatformTest.php +++ b/tests/Composer/Test/Util/PlatformTest.php @@ -24,11 +24,8 @@ class PlatformTest extends \PHPUnit_Framework_TestCase public function testExpandPath() { 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')); }