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(); $config = Factory::createConfig();
if (null === $repositoryUrl) { if (null === $repositoryUrl) {
$sourceRepo = new CompositeRepository(Factory::createComposerRepositories($io, $config)); $sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config));
} elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) { } elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) {
$sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io))); $sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io)));
} elseif (0 === strpos($repositoryUrl, 'http')) { } elseif (0 === strpos($repositoryUrl, 'http')) {

View File

@ -16,7 +16,6 @@ use Composer\Json\JsonFile;
use Composer\Factory; use Composer\Factory;
use Composer\Repository\CompositeRepository; use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository; use Composer\Repository\PlatformRepository;
use Composer\Repository\ComposerRepository;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
@ -236,7 +235,7 @@ EOT
if (!$this->repos) { if (!$this->repos) {
$this->repos = new CompositeRepository(array_merge( $this->repos = new CompositeRepository(array_merge(
array(new PlatformRepository), 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 Symfony\Component\Console\Output\OutputInterface;
use Composer\Repository\CompositeRepository; use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository; use Composer\Repository\PlatformRepository;
use Composer\Repository\ComposerRepository;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Package\AliasPackage; use Composer\Package\AliasPackage;
use Composer\Factory; use Composer\Factory;
@ -53,8 +52,8 @@ EOT
$installedRepo = new CompositeRepository(array($localRepo, $platformRepo)); $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories())); $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
} else { } else {
$defaultRepos = Factory::createComposerRepositories($this->getIO(), Factory::createConfig()); $defaultRepos = Factory::createDefaultRepositories($this->getIO(), Factory::createConfig());
$output->writeln('No composer.json found in the current directory, showing packages from ' . str_replace('http://', '', implode(', ', array_keys($defaultRepos)))); $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
$installedRepo = $platformRepo; $installedRepo = $platformRepo;
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos)); $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 Symfony\Component\Console\Output\OutputInterface;
use Composer\Repository\CompositeRepository; use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository; use Composer\Repository\PlatformRepository;
use Composer\Repository\ComposerRepository;
use Composer\Repository\RepositoryInterface; use Composer\Repository\RepositoryInterface;
/** /**
@ -64,8 +63,8 @@ EOT
$installedRepo = new CompositeRepository(array($localRepo, $platformRepo)); $installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories())); $repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
} else { } else {
$defaultRepos = Factory::createComposerRepositories($this->getIO(), Factory::createConfig()); $defaultRepos = Factory::createDefaultRepositories($this->getIO(), Factory::createConfig());
$output->writeln('No composer.json found in the current directory, showing packages from ' . str_replace('http://', '', implode(', ', array_keys($defaultRepos)))); $output->writeln('No composer.json found in the current directory, showing packages from ' . implode(', ', array_keys($defaultRepos)));
$installedRepo = $platformRepo; $installedRepo = $platformRepo;
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos)); $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
} }

View File

@ -17,17 +17,25 @@ namespace Composer;
*/ */
class Config class Config
{ {
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 $config;
private $repositories;
public function __construct() public function __construct()
{ {
// load defaults // load defaults
$this->config = array( $this->config = static::$defaultConfig;
'process-timeout' => 300, $this->repositories = static::$defaultRepositories;
'vendor-dir' => 'vendor',
'bin-dir' => '{$vendor-dir}/bin',
'notify-on-install' => true,
);
} }
/** /**
@ -41,6 +49,18 @@ class Config
if (!empty($config['config']) && is_array($config['config'])) { if (!empty($config['config']) && is_array($config['config'])) {
$this->config = array_replace_recursive($this->config, $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 class Factory
{ {
public static $defaultComposerRepositories = array(
'packagist' => 'http://packagist.org',
);
/** /**
* @return Config * @return Config
*/ */
@ -72,12 +68,12 @@ class Factory
return getenv('COMPOSER') ?: 'composer.json'; return getenv('COMPOSER') ?: 'composer.json';
} }
public static function createComposerRepositories(IOInterface $io, Config $config) public static function createDefaultRepositories(IOInterface $io, Config $config)
{ {
$repos = array(); $repos = array();
foreach (static::$defaultComposerRepositories as $url) { foreach ($config->getRepositories() as $repo => $url) {
$repos[$url] = new ComposerRepository(array('url' => $url), $io, $config); $repos[preg_replace('{^https?://}i', '', $url)] = new ComposerRepository(array('url' => $url), $io, $config);
} }
return $repos; return $repos;
@ -131,13 +127,13 @@ class Factory
$rm = $this->createRepositoryManager($io, $config); $rm = $this->createRepositoryManager($io, $config);
// load default composer repositories unless they're explicitly disabled // load default composer repositories unless they're explicitly disabled
$localConfig = $this->addComposerRepositories($localConfig); $localConfig = $this->addDefaultRepositories($config, $localConfig);
// load local repository // load local repository
$this->addLocalRepository($rm, $vendorDir); $this->addLocalRepository($rm, $vendorDir);
// load package // load package
$loader = new Package\Loader\RootPackageLoader($rm); $loader = new Package\Loader\RootPackageLoader($rm, $config);
$package = $loader->load($localConfig); $package = $loader->load($localConfig);
// initialize download manager // initialize download manager
@ -202,9 +198,9 @@ class Factory
* @param array $localConfig * @param array $localConfig
* @return array * @return array
*/ */
protected function addComposerRepositories(array $localConfig) protected function addDefaultRepositories(Config $config, array $localConfig)
{ {
$defaults = static::$defaultComposerRepositories; $defaults = $config->getRepositories();
if (isset($localConfig['repositories'])) { if (isset($localConfig['repositories'])) {
foreach ($localConfig['repositories'] as $key => $repo) { foreach ($localConfig['repositories'] as $key => $repo) {

View File

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

View File

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

View File

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