From 449f8165ef64829dc34be836235ceb74d9227dd7 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 29 Jun 2014 14:47:43 +0200 Subject: [PATCH] Allow $HOME and ~ usage in all *-dir config values, fixes #3060 --- doc/04-schema.md | 3 ++- src/Composer/Config.php | 5 ++++- tests/Composer/Test/ConfigTest.php | 12 ++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/doc/04-schema.md b/doc/04-schema.md index 8ff2db7f2..e0db3c9af 100644 --- a/doc/04-schema.md +++ b/doc/04-schema.md @@ -760,7 +760,8 @@ The following options are supported: against them. For example using `{"example.org": {"username": "alice", "password": "foo"}` as the value of this option will let composer authenticate against example.org. * **vendor-dir:** Defaults to `vendor`. You can install dependencies into a - different directory if you want to. + different directory if you want to. `$HOME` and `~` will be replaced by your + home directory's path in vendor-dir and all `*-dir` options below. * **bin-dir:** Defaults to `vendor/bin`. If a project includes binaries, they will be symlinked into this directory. * **cache-dir:** Defaults to `$home/cache` on unix systems and diff --git a/src/Composer/Config.php b/src/Composer/Config.php index f22604ee3..d5b41efe2 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -158,7 +158,10 @@ class Config // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); - return rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\'); + $val = rtrim($this->process(getenv($env) ?: $this->config[$key]), '/\\'); + $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val); + + return $val; case 'cache-ttl': return (int) $this->config[$key]; diff --git a/tests/Composer/Test/ConfigTest.php b/tests/Composer/Test/ConfigTest.php index c2cf82ca7..5afbabffa 100644 --- a/tests/Composer/Test/ConfigTest.php +++ b/tests/Composer/Test/ConfigTest.php @@ -109,6 +109,18 @@ class ConfigTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('foo' => 'bar', 'bar' => 'baz'), $config->get('github-oauth')); } + public function testVarReplacement() + { + $config = new Config(); + $config->merge(array('config' => array('a' => 'b', 'c' => '{$a}'))); + $config->merge(array('config' => array('bin-dir' => '$HOME', 'cache-dir' => '~/foo/'))); + + $home = rtrim(getenv('HOME'), '\\/'); + $this->assertEquals('b', $config->get('c')); + $this->assertEquals($home.'/', $config->get('bin-dir')); + $this->assertEquals($home.'/foo', $config->get('cache-dir')); + } + public function testOverrideGithubProtocols() { $config = new Config();