1
0
Fork 0

Fix relative path fetching when a var uses var replacement, fixes #3564

pull/3567/head
Jordi Boggiano 2014-12-17 21:57:27 +00:00
parent e0291f3a30
commit 60ac971419
2 changed files with 28 additions and 8 deletions

View File

@ -152,6 +152,7 @@ class Config
* Returns a setting * Returns a setting
* *
* @param string $key * @param string $key
* @param int $flags Options (see class constants)
* @throws \RuntimeException * @throws \RuntimeException
* @return mixed * @return mixed
*/ */
@ -168,9 +169,13 @@ class Config
// convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config // convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
$env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_')); $env = 'COMPOSER_' . strtoupper(strtr($key, '-', '_'));
$val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key]), '/\\'); $val = rtrim($this->process($this->getComposerEnv($env) ?: $this->config[$key], $flags), '/\\');
$val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val); $val = preg_replace('#^(\$HOME|~)(/|$)#', rtrim(getenv('HOME') ?: getenv('USERPROFILE'), '/\\') . '/', $val);
if (substr($key, -4) !== '-dir') {
return $val;
}
return ($flags & self::RELATIVE_PATHS == 1) ? $val : $this->realpath($val); return ($flags & self::RELATIVE_PATHS == 1) ? $val : $this->realpath($val);
case 'cache-ttl': case 'cache-ttl':
@ -207,7 +212,7 @@ class Config
return (int) $this->config['cache-ttl']; return (int) $this->config['cache-ttl'];
case 'home': case 'home':
return rtrim($this->process($this->config[$key]), '/\\'); return rtrim($this->process($this->config[$key], $flags), '/\\');
case 'discard-changes': case 'discard-changes':
if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) { if ($env = $this->getComposerEnv('COMPOSER_DISCARD_CHANGES')) {
@ -244,17 +249,17 @@ class Config
return null; return null;
} }
return $this->process($this->config[$key]); return $this->process($this->config[$key], $flags);
} }
} }
public function all() public function all($flags = 0)
{ {
$all = array( $all = array(
'repositories' => $this->getRepositories(), 'repositories' => $this->getRepositories(),
); );
foreach (array_keys($this->config) as $key) { foreach (array_keys($this->config) as $key) {
$all['config'][$key] = $this->get($key); $all['config'][$key] = $this->get($key, $flags);
} }
return $all; return $all;
@ -283,9 +288,10 @@ class Config
* Replaces {$refs} inside a config string * Replaces {$refs} inside a config string
* *
* @param string $value a config string that can contain {$refs-to-other-config} * @param string $value a config string that can contain {$refs-to-other-config}
* @param int $flags Options (see class constants)
* @return string * @return string
*/ */
private function process($value) private function process($value, $flags)
{ {
$config = $this; $config = $this;
@ -293,8 +299,8 @@ class Config
return $value; return $value;
} }
return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config) { return preg_replace_callback('#\{\$(.+)\}#', function ($match) use ($config, $flags) {
return $config->get($match[1]); return $config->get($match[1], $flags);
}, $value); }, $value);
} }

View File

@ -136,6 +136,20 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/baz', $config->get('cache-dir')); $this->assertEquals('/baz', $config->get('cache-dir'));
} }
public function testFetchingRelativePaths()
{
$config = new Config(false, '/foo/bar');
$config->merge(array('config' => array(
'bin-dir' => '{$vendor-dir}/foo',
'vendor-dir' => 'vendor'
)));
$this->assertEquals('/foo/bar/vendor', $config->get('vendor-dir'));
$this->assertEquals('/foo/bar/vendor/foo', $config->get('bin-dir'));
$this->assertEquals('vendor', $config->get('vendor-dir', Config::RELATIVE_PATHS));
$this->assertEquals('vendor/foo', $config->get('bin-dir', Config::RELATIVE_PATHS));
}
public function testOverrideGithubProtocols() public function testOverrideGithubProtocols()
{ {
$config = new Config(false); $config = new Config(false);