1
0
Fork 0

Expose default repositories in system config file

pull/824/merge
Jordi Boggiano 2012-06-23 12:55:05 +02:00
parent 4a6ae454c2
commit 3ac11b932c
10 changed files with 68 additions and 63 deletions

View File

@ -91,7 +91,7 @@ EOT
$config = Factory::createConfig();
if (null === $repositoryUrl) {
$sourceRepo = new CompositeRepository(Factory::createComposerRepositories($io, $config));
$sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config));
} elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) {
$sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io)));
} elseif (0 === strpos($repositoryUrl, 'http')) {

View File

@ -16,7 +16,6 @@ use Composer\Json\JsonFile;
use Composer\Factory;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\ComposerRepository;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
@ -236,7 +235,7 @@ EOT
if (!$this->repos) {
$this->repos = new CompositeRepository(array_merge(
array(new PlatformRepository),
Factory::createComposerRepositories($this->getIO(), Factory::createConfig())
Factory::createDefaultRepositories($this->getIO(), Factory::createConfig())
));
}

View File

@ -17,7 +17,6 @@ use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\ComposerRepository;
use Composer\Package\PackageInterface;
use Composer\Package\AliasPackage;
use Composer\Factory;
@ -53,8 +52,8 @@ EOT
$installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
} else {
$defaultRepos = Factory::createComposerRepositories($this->getIO(), Factory::createConfig());
$output->writeln('No composer.json found in the current directory, showing packages from ' . str_replace('http://', '', implode(', ', array_keys($defaultRepos))));
$defaultRepos = Factory::createDefaultRepositories($this->getIO(), Factory::createConfig());
$output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
$installedRepo = $platformRepo;
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
}

View File

@ -21,7 +21,6 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\ComposerRepository;
use Composer\Repository\RepositoryInterface;
/**
@ -64,8 +63,8 @@ EOT
$installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
} else {
$defaultRepos = Factory::createComposerRepositories($this->getIO(), Factory::createConfig());
$output->writeln('No composer.json found in the current directory, showing packages from ' . str_replace('http://', '', implode(', ', array_keys($defaultRepos))));
$defaultRepos = Factory::createDefaultRepositories($this->getIO(), Factory::createConfig());
$output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
$installedRepo = $platformRepo;
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
}

View File

@ -17,17 +17,25 @@ namespace Composer;
*/
class Config
{
private $config;
public function __construct()
{
// load defaults
$this->config = array(
public static $defaultConfig = array(
'process-timeout' => 300,
'vendor-dir' => 'vendor',
'bin-dir' => '{$vendor-dir}/bin',
'notify-on-install' => true,
);
public static $defaultRepositories = array(
'packagist' => 'http://packagist.org',
);
private $config;
private $repositories;
public function __construct()
{
// load defaults
$this->config = static::$defaultConfig;
$this->repositories = static::$defaultRepositories;
}
/**
@ -41,6 +49,18 @@ class Config
if (!empty($config['config']) && is_array($config['config'])) {
$this->config = array_replace_recursive($this->config, $config['config']);
}
if (!empty($config['repositories']) && is_array($config['repositories'])) {
$this->repositories = $config['repositories'];
}
}
/**
* @return array
*/
public function getRepositories()
{
return $this->repositories;
}
/**

View File

@ -28,10 +28,6 @@ use Composer\Util\RemoteFilesystem;
*/
class Factory
{
public static $defaultComposerRepositories = array(
'packagist' => 'http://packagist.org',
);
/**
* @return Config
*/
@ -72,12 +68,12 @@ class Factory
return getenv('COMPOSER') ?: 'composer.json';
}
public static function createComposerRepositories(IOInterface $io, Config $config)
public static function createDefaultRepositories(IOInterface $io, Config $config)
{
$repos = array();
foreach (static::$defaultComposerRepositories as $url) {
$repos[$url] = new ComposerRepository(array('url' => $url), $io, $config);
foreach ($config->getRepositories() as $repo => $url) {
$repos[preg_replace('{^https?://}i', '', $url)] = new ComposerRepository(array('url' => $url), $io, $config);
}
return $repos;
@ -131,13 +127,13 @@ class Factory
$rm = $this->createRepositoryManager($io, $config);
// load default composer repositories unless they're explicitly disabled
$localConfig = $this->addComposerRepositories($localConfig);
$localConfig = $this->addDefaultRepositories($config, $localConfig);
// load local repository
$this->addLocalRepository($rm, $vendorDir);
// load package
$loader = new Package\Loader\RootPackageLoader($rm);
$loader = new Package\Loader\RootPackageLoader($rm, $config);
$package = $loader->load($localConfig);
// initialize download manager
@ -202,9 +198,9 @@ class Factory
* @param array $localConfig
* @return array
*/
protected function addComposerRepositories(array $localConfig)
protected function addDefaultRepositories(Config $config, array $localConfig)
{
$defaults = static::$defaultComposerRepositories;
$defaults = $config->getRepositories();
if (isset($localConfig['repositories'])) {
foreach ($localConfig['repositories'] as $key => $repo) {

View File

@ -13,7 +13,7 @@
namespace Composer\Package\Loader;
use Composer\Package\BasePackage;
use Composer\Factory;
use Composer\Config;
use Composer\Package\Version\VersionParser;
use Composer\Repository\RepositoryManager;
use Composer\Util\ProcessExecutor;
@ -28,11 +28,13 @@ use Composer\Util\ProcessExecutor;
class RootPackageLoader extends ArrayLoader
{
private $manager;
private $config;
private $process;
public function __construct(RepositoryManager $manager, VersionParser $parser = null, ProcessExecutor $process = null)
public function __construct(RepositoryManager $manager, Config $config, VersionParser $parser = null, ProcessExecutor $process = null)
{
$this->manager = $manager;
$this->config = $config;
$this->process = $process ?: new ProcessExecutor();
parent::__construct($parser);
}
@ -80,7 +82,7 @@ class RootPackageLoader extends ArrayLoader
$package->setMinimumStability(VersionParser::normalizeStability($config['minimum-stability']));
}
$defaultRepositories = array_keys(Factory::$defaultComposerRepositories);
$defaultRepositories = array_keys($this->config->getRepositories());
if (isset($config['repositories'])) {
foreach ($config['repositories'] as $index => $repo) {

View File

@ -13,42 +13,30 @@
namespace Composer\Test;
use Composer\Factory;
use Composer\Config;
class FactoryTest extends \PHPUnit_Framework_TestCase
{
protected $defaultComposerRepositories;
protected function setUp()
{
$this->defaultComposerRepositories = Factory::$defaultComposerRepositories;
}
protected function tearDown()
{
Factory::$defaultComposerRepositories = $this->defaultComposerRepositories;
unset($this->defaultComposerRepositories);
}
/**
* @dataProvider dataAddPackagistRepository
*/
public function testAddPackagistRepository($expected, $config, $defaults = null)
public function testAddPackagistRepository($expected, $composerConfig, $defaults = null)
{
if (null !== $defaults) {
Factory::$defaultComposerRepositories = $defaults;
$factory = new Factory();
$config = new Config();
if ($defaults) {
$config->merge(array('repositories' => $defaults));
}
$factory = new Factory();
$ref = new \ReflectionMethod($factory, 'addComposerRepositories');
$ref = new \ReflectionMethod($factory, 'addDefaultRepositories');
$ref->setAccessible(true);
$this->assertEquals($expected, $ref->invoke($factory, $config));
$this->assertEquals($expected, $ref->invoke($factory, $config, $composerConfig));
}
public function dataAddPackagistRepository()
{
$f = function() {
$repos = function() {
$repositories = func_get_args();
return array('repositories' => $repositories);
@ -56,22 +44,22 @@ class FactoryTest extends \PHPUnit_Framework_TestCase
$data = array();
$data[] = array(
$f(array('type' => 'composer', 'url' => 'http://packagist.org')),
$f()
$repos(array('type' => 'composer', 'url' => 'http://packagist.org')),
$repos()
);
$data[] = array(
$f(array('packagist' => false)),
$f(array('packagist' => false))
$repos(array('packagist' => false)),
$repos(array('packagist' => false))
);
$data[] = array(
$f(
$repos(
array('type' => 'vcs', 'url' => 'git://github.com/composer/composer.git'),
array('type' => 'composer', 'url' => 'http://packagist.org'),
array('type' => 'pear', 'url' => 'http://pear.composer.org')
),
$f(
$repos(
array('type' => 'vcs', 'url' => 'git://github.com/composer/composer.git'),
array('packagist' => true),
array('type' => 'pear', 'url' => 'http://pear.composer.org')
@ -84,20 +72,20 @@ class FactoryTest extends \PHPUnit_Framework_TestCase
);
$data[] = array(
$f(
$repos(
array('type' => 'composer', 'url' => 'http://example.com'),
array('type' => 'composer', 'url' => 'http://packagist.org')
),
$f(),
$repos(),
$multirepo,
);
$data[] = array(
$f(
$repos(
array('type' => 'composer', 'url' => 'http://packagist.org'),
array('type' => 'composer', 'url' => 'http://example.com')
),
$f(array('packagist' => true)),
$repos(array('packagist' => true)),
$multirepo,
);

View File

@ -34,7 +34,7 @@ class FactoryMock extends Factory
{
}
protected function addPackagistRepository(array $localConfig)
protected function addDefaultRepositories(Config $config, array $localConfig)
{
return $localConfig;
}

View File

@ -12,6 +12,7 @@
namespace Composer\Test\Package\Loader;
use Composer\Config;
use Composer\Package\Loader\RootPackageLoader;
use Composer\Test\Mock\ProcessExecutorMock;
use Composer\Repository\RepositoryManager;
@ -41,7 +42,7 @@ class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase
return 0;
});
$loader = new RootPackageLoader($manager, null, $processExecutor);
$loader = new RootPackageLoader($manager, new Config, null, $processExecutor);
$package = $loader->load(array());
$this->assertEquals("dev-$commitHash", $package->getVersion());
@ -53,7 +54,8 @@ class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase
new RepositoryManager(
$this->getMock('Composer\\IO\\IOInterface'),
$this->getMock('Composer\\Config')
)
),
new Config()
);
$repos = array(array('packagist' => false));