2012-04-09 14:10:25 +00:00
< ? php
/*
* This file is part of Composer .
*
* ( c ) Nils Adermann < naderman @ naderman . de >
* Jordi Boggiano < j . boggiano @ seld . be >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Composer ;
2012-10-18 14:39:47 +00:00
use Composer\Config\ConfigSourceInterface ;
2012-04-09 14:10:25 +00:00
/**
* @ author Jordi Boggiano < j . boggiano @ seld . be >
*/
class Config
{
2012-06-23 10:55:05 +00:00
public static $defaultConfig = array (
'process-timeout' => 300 ,
2013-02-12 10:16:52 +00:00
'use-include-path' => false ,
2013-03-05 11:56:09 +00:00
'preferred-install' => 'auto' ,
2012-06-23 10:55:05 +00:00
'notify-on-install' => true ,
2014-02-07 16:11:36 +00:00
'github-protocols' => array ( 'git' , 'https' , 'ssh' ),
2013-02-12 10:16:52 +00:00
'vendor-dir' => 'vendor' ,
'bin-dir' => '{$vendor-dir}/bin' ,
2012-11-14 16:46:09 +00:00
'cache-dir' => '{$home}/cache' ,
'cache-files-dir' => '{$cache-dir}/files' ,
'cache-repo-dir' => '{$cache-dir}/repo' ,
'cache-vcs-dir' => '{$cache-dir}/vcs' ,
2013-02-12 10:16:52 +00:00
'cache-ttl' => 15552000 , // 6 months
'cache-files-ttl' => null , // fallback to cache-ttl
'cache-files-maxsize' => '300MiB' ,
2013-03-01 22:47:24 +00:00
'discard-changes' => false ,
2013-12-26 16:40:52 +00:00
'autoloader-suffix' => null ,
2014-01-17 13:43:54 +00:00
'optimize-autoloader' => false ,
2013-10-14 16:38:30 +00:00
'prepend-autoloader' => true ,
2012-09-07 23:51:33 +00:00
'github-domains' => array ( 'github.com' ),
2014-05-27 11:50:47 +00:00
'store-auths' => 'prompt' ,
// valid keys without defaults (auth config stuff):
// github-oauth
// http-basic
2012-06-23 10:55:05 +00:00
);
public static $defaultRepositories = array (
2012-06-24 11:03:55 +00:00
'packagist' => array (
'type' => 'composer' ,
2012-10-11 18:54:59 +00:00
'url' => 'https?://packagist.org' ,
2013-02-21 16:37:18 +00:00
'allow_ssl_downgrade' => true ,
2012-06-24 11:03:55 +00:00
)
2012-06-23 10:55:05 +00:00
);
2012-04-09 14:10:25 +00:00
private $config ;
2012-06-23 10:55:05 +00:00
private $repositories ;
2012-10-18 14:39:47 +00:00
private $configSource ;
2014-05-27 11:50:47 +00:00
private $authConfigSource ;
2012-04-09 14:10:25 +00:00
public function __construct ()
{
// load defaults
2012-06-23 10:55:05 +00:00
$this -> config = static :: $defaultConfig ;
$this -> repositories = static :: $defaultRepositories ;
2012-04-09 14:10:25 +00:00
}
2012-10-18 14:39:47 +00:00
public function setConfigSource ( ConfigSourceInterface $source )
{
$this -> configSource = $source ;
}
public function getConfigSource ()
{
return $this -> configSource ;
}
2014-05-27 11:50:47 +00:00
public function setAuthConfigSource ( ConfigSourceInterface $source )
{
$this -> authConfigSource = $source ;
}
public function getAuthConfigSource ()
{
return $this -> authConfigSource ;
}
2012-04-09 14:13:46 +00:00
/**
* Merges new config values with the existing ones ( overriding )
*
* @ param array $config
*/
2012-04-09 14:10:25 +00:00
public function merge ( array $config )
{
// override defaults with given config
if ( ! empty ( $config [ 'config' ]) && is_array ( $config [ 'config' ])) {
2012-12-08 20:45:21 +00:00
foreach ( $config [ 'config' ] as $key => $val ) {
2014-05-27 11:50:47 +00:00
if ( in_array ( $key , array ( 'github-oauth' , 'http-basic' )) && isset ( $this -> config [ $key ])) {
2012-12-08 20:45:21 +00:00
$this -> config [ $key ] = array_merge ( $this -> config [ $key ], $val );
} else {
$this -> config [ $key ] = $val ;
}
}
2012-04-09 14:10:25 +00:00
}
2012-06-23 10:55:05 +00:00
if ( ! empty ( $config [ 'repositories' ]) && is_array ( $config [ 'repositories' ])) {
2012-06-23 11:04:23 +00:00
$this -> repositories = array_reverse ( $this -> repositories , true );
2012-06-24 11:03:55 +00:00
$newRepos = array_reverse ( $config [ 'repositories' ], true );
foreach ( $newRepos as $name => $repository ) {
// disable a repository by name
if ( false === $repository ) {
2012-06-23 11:04:23 +00:00
unset ( $this -> repositories [ $name ]);
2012-06-24 11:03:55 +00:00
continue ;
}
// disable a repository with an anonymous {"name": false} repo
2012-06-29 09:45:06 +00:00
if ( 1 === count ( $repository ) && false === current ( $repository )) {
unset ( $this -> repositories [ key ( $repository )]);
continue ;
2012-06-24 11:03:55 +00:00
}
// store repo
if ( is_int ( $name )) {
$this -> repositories [] = $repository ;
} else {
$this -> repositories [ $name ] = $repository ;
2012-06-23 11:04:23 +00:00
}
}
2012-06-24 11:03:55 +00:00
$this -> repositories = array_reverse ( $this -> repositories , true );
2012-06-23 10:55:05 +00:00
}
}
/**
* @ return array
*/
public function getRepositories ()
{
return $this -> repositories ;
2012-04-09 14:10:25 +00:00
}
2012-04-09 14:13:46 +00:00
/**
* Returns a setting
*
2013-06-13 11:28:24 +00:00
* @ param string $key
2013-06-13 00:05:44 +00:00
* @ throws \RuntimeException
2012-04-09 14:13:46 +00:00
* @ return mixed
*/
2012-04-09 14:10:25 +00:00
public function get ( $key )
{
switch ( $key ) {
case 'vendor-dir' :
case 'bin-dir' :
case 'process-timeout' :
2012-11-14 16:46:09 +00:00
case 'cache-dir' :
case 'cache-files-dir' :
case 'cache-repo-dir' :
case 'cache-vcs-dir' :
2012-04-09 14:10:25 +00:00
// convert foo-bar to COMPOSER_FOO_BAR and check if it exists since it overrides the local config
$env = 'COMPOSER_' . strtoupper ( strtr ( $key , '-' , '_' ));
2012-05-22 10:07:08 +00:00
2012-06-24 19:58:51 +00:00
return rtrim ( $this -> process ( getenv ( $env ) ? : $this -> config [ $key ]), '/\\' );
2012-04-09 14:10:25 +00:00
2012-11-11 14:31:50 +00:00
case 'cache-ttl' :
return ( int ) $this -> config [ $key ];
2012-12-16 19:19:16 +00:00
case 'cache-files-maxsize' :
2013-01-05 19:02:51 +00:00
if ( ! preg_match ( '/^\s*([0-9.]+)\s*(?:([kmg])(?:i?b)?)?\s*$/i' , $this -> config [ $key ], $matches )) {
2012-12-16 19:19:16 +00:00
throw new \RuntimeException (
2013-03-01 22:47:24 +00:00
" Could not parse the value of 'cache-files-maxsize': { $this -> config [ $key ] } "
2012-12-16 19:19:16 +00:00
);
}
$size = $matches [ 1 ];
if ( isset ( $matches [ 2 ])) {
switch ( strtolower ( $matches [ 2 ])) {
2013-01-05 19:02:51 +00:00
case 'g' :
2012-12-16 19:19:16 +00:00
$size *= 1024 ;
2013-01-05 19:02:51 +00:00
// intentional fallthrough
case 'm' :
2012-12-16 19:19:16 +00:00
$size *= 1024 ;
2013-01-05 19:02:51 +00:00
// intentional fallthrough
case 'k' :
2012-12-16 19:19:16 +00:00
$size *= 1024 ;
2013-01-05 19:02:51 +00:00
break ;
2012-12-16 19:19:16 +00:00
}
}
2013-01-05 19:01:58 +00:00
2012-12-16 19:19:16 +00:00
return $size ;
2012-11-11 14:31:50 +00:00
case 'cache-files-ttl' :
if ( isset ( $this -> config [ $key ])) {
return ( int ) $this -> config [ $key ];
}
return ( int ) $this -> config [ 'cache-ttl' ];
2012-04-09 14:36:06 +00:00
case 'home' :
return rtrim ( $this -> process ( $this -> config [ $key ]), '/\\' );
2013-02-28 16:10:04 +00:00
case 'discard-changes' :
2013-04-15 17:19:27 +00:00
if ( $env = getenv ( 'COMPOSER_DISCARD_CHANGES' )) {
if ( ! in_array ( $env , array ( 'stash' , 'true' , 'false' , '1' , '0' ), true )) {
throw new \RuntimeException (
2013-04-15 19:55:09 +00:00
" Invalid value for COMPOSER_DISCARD_CHANGES: { $env } . Expected 1, 0, true, false or stash "
2013-04-15 17:19:27 +00:00
);
}
if ( 'stash' === $env ) {
return 'stash' ;
}
// convert string value to bool
return $env !== 'false' && ( bool ) $env ;
}
if ( ! in_array ( $this -> config [ $key ], array ( true , false , 'stash' ), true )) {
2013-02-28 16:10:04 +00:00
throw new \RuntimeException (
2013-04-15 19:55:09 +00:00
" Invalid value for 'discard-changes': { $this -> config [ $key ] } . Expected true, false or stash "
2013-02-28 16:10:04 +00:00
);
}
return $this -> config [ $key ];
2013-06-28 17:16:12 +00:00
case 'github-protocols' :
if ( reset ( $this -> config [ 'github-protocols' ]) === 'http' ) {
2014-02-13 15:23:53 +00:00
throw new \RuntimeException ( 'The http protocol for github is not available anymore, update your config\'s github-protocols to use "https", "git" or "ssh"' );
2013-06-28 17:16:12 +00:00
}
return $this -> config [ $key ];
2012-04-09 14:10:25 +00:00
default :
2012-10-18 14:39:47 +00:00
if ( ! isset ( $this -> config [ $key ])) {
return null ;
}
2012-04-09 14:10:25 +00:00
return $this -> process ( $this -> config [ $key ]);
}
}
2012-11-30 15:21:08 +00:00
public function all ()
{
$all = array (
'repositories' => $this -> getRepositories (),
);
foreach ( array_keys ( $this -> config ) as $key ) {
$all [ 'config' ][ $key ] = $this -> get ( $key );
}
return $all ;
}
2013-02-12 10:16:52 +00:00
public function raw ()
{
return array (
'repositories' => $this -> getRepositories (),
'config' => $this -> config ,
);
}
2012-04-09 14:13:46 +00:00
/**
* Checks whether a setting exists
*
2012-06-23 09:58:18 +00:00
* @ param string $key
* @ return bool
2012-04-09 14:13:46 +00:00
*/
2012-04-09 14:10:25 +00:00
public function has ( $key )
{
return array_key_exists ( $key , $this -> config );
}
/**
* Replaces { $refs } inside a config string
*
2014-06-10 14:02:44 +00:00
* @ param string $value a config string that can contain { $refs - to - other - config }
2012-04-09 14:10:25 +00:00
* @ return string
*/
private function process ( $value )
{
$config = $this ;
2012-05-22 10:07:08 +00:00
2012-10-18 14:39:47 +00:00
if ( ! is_string ( $value )) {
return $value ;
}
2012-04-09 14:10:25 +00:00
return preg_replace_callback ( '#\{\$(.+)\}#' , function ( $match ) use ( $config ) {
return $config -> get ( $match [ 1 ]);
}, $value );
}
}