1
0
Fork 0

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

pull/5184/head
Niels Keurentjes 2016-04-13 02:02:50 +02:00
parent f5422a441d
commit c9534d48c1
5 changed files with 11 additions and 14 deletions

View File

@ -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/<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 ``?``.
> For details, see the [PHP glob function](http://php.net/glob).

View File

@ -14,6 +14,7 @@ namespace Composer;
use Composer\Config\ConfigSourceInterface;
use Composer\Downloader\TransportException;
use Composer\Util\Platform;
/**
* @author Jordi Boggiano <j.boggiano@seld.be>
@ -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;

View File

@ -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);
}

View File

@ -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'));
}

View File

@ -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'));
}