1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-09 16:42:57 +00:00

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

This commit is contained in:
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 Leading tildes are expanded to the current user's home folder, and environment
variables are parsed according to host platform. For example `~/git/mypackage` variables are parsed in both Windows and Linux/Mac notations. For example
will automatically load the mypackage clone from `/home/<username>/git/mypackage`, `~/git/mypackage` will automatically load the mypackage clone from
which is equivalent to `$HOME/git/mypackage` on Linux/Mac or `/home/<username>/git/mypackage`, equivalent to `$HOME/git/mypackage` or
`%USERPROFILE%/git/mypackage` on Windows. `%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).

View file

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

View file

@ -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 preg_replace_callback('#^([\\$%])(\\w+)\\1?(([/\\\\].*)?)#', function($matches) {
return getenv($matches[2]) . '/'; return getenv($matches[2]) . $matches[3];
}, $path); }, $path);
} }

View file

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

View file

@ -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'));
$this->assertEquals('/home/test/myPath', Platform::expandPath('%TESTENV%/myPath')); $this->assertEquals('/home/test/myPath', Platform::expandPath('$TESTENV/myPath'));
} else {
$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'));
} }