1
0
Fork 0

Add internal support for multiple default composer repositories.

pull/628/head
Danny Berger 2012-04-26 20:29:43 -04:00
parent 762f469cd9
commit 8d6d155153
8 changed files with 130 additions and 30 deletions

View File

@ -17,6 +17,7 @@ use Composer\Installer;
use Composer\Installer\ProjectInstaller;
use Composer\IO\IOInterface;
use Composer\Repository\ComposerRepository;
use Composer\Repository\CompositeRepository;
use Composer\Repository\FilesystemRepository;
use Composer\Repository\InstalledFilesystemRepository;
use Symfony\Component\Console\Input\InputArgument;
@ -85,7 +86,7 @@ EOT
$config = Factory::createConfig();
if (null === $repositoryUrl) {
$sourceRepo = new ComposerRepository(array('url' => 'http://packagist.org'), $io, $config);
$sourceRepo = new CompositeRepository(Factory::createComposerRepositories($io, $config));
} elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) {
$sourceRepo = new FilesystemRepository(new JsonFile($repositoryUrl, new RemoteFilesystem($io)));
} elseif (0 === strpos($repositoryUrl, 'http')) {

View File

@ -228,9 +228,9 @@ EOT
// init repos
if (!$this->repos) {
$this->repos = new CompositeRepository(array(
new PlatformRepository,
new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig())
$this->repos = new CompositeRepository(array_merge(
array(new PlatformRepository),
Factory::createComposerRepositories($this->getIO(), Factory::createConfig())
));
}

View File

@ -53,10 +53,10 @@ EOT
$installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
} else {
$output->writeln('No composer.json found in the current directory, showing packages from packagist.org');
$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))));
$installedRepo = $platformRepo;
$packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig());
$repos = new CompositeRepository(array($installedRepo, $packagist));
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
}
$tokens = $input->getArgument('tokens');

View File

@ -64,10 +64,10 @@ EOT
$installedRepo = new CompositeRepository(array($localRepo, $platformRepo));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
} else {
$output->writeln('No composer.json found in the current directory, showing packages from packagist.org');
$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))));
$installedRepo = $platformRepo;
$packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig());
$repos = new CompositeRepository(array($installedRepo, $packagist));
$repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
}
// show single package or single version

View File

@ -14,6 +14,7 @@ namespace Composer;
use Composer\Json\JsonFile;
use Composer\IO\IOInterface;
use Composer\Repository\ComposerRepository;
use Composer\Repository\RepositoryManager;
use Composer\Util\ProcessExecutor;
use Composer\Util\RemoteFilesystem;
@ -27,6 +28,10 @@ use Composer\Util\RemoteFilesystem;
*/
class Factory
{
public static $defaultComposerRepositories = array(
'packagist' => 'http://packagist.org',
);
public static function createConfig()
{
// load main Composer configuration
@ -51,6 +56,17 @@ class Factory
return $config;
}
public static function createComposerRepositories(IOInterface $io, Config $config)
{
$repos = array();
foreach (static::$defaultComposerRepositories as $url) {
$repos[$url] = new ComposerRepository(array('url' => $url), $io, $config);
}
return $repos;
}
/**
* Creates a Composer instance
*
@ -96,8 +112,8 @@ class Factory
// initialize repository manager
$rm = $this->createRepositoryManager($io, $config);
// load default repository unless it's explicitly disabled
$localConfig = $this->addPackagistRepository($localConfig);
// load default composer repositories unless they're explicitly disabled
$localConfig = $this->addComposerRepositories($localConfig);
// load local repository
$this->addLocalRepository($rm, $vendorDir);
@ -172,31 +188,35 @@ class Factory
$rm->setLocalDevRepository(new Repository\InstalledFilesystemRepository(new JsonFile($vendorDir.'/composer/installed_dev.json')));
}
protected function addPackagistRepository(array $localConfig)
protected function addComposerRepositories(array $localConfig)
{
$loadPackagist = true;
$packagistConfig = array(
'type' => 'composer',
'url' => 'http://packagist.org'
);
$defaults = static::$defaultComposerRepositories;
if (isset($localConfig['repositories'])) {
foreach ($localConfig['repositories'] as $key => $repo) {
if (isset($repo['packagist'])) {
if (true === $repo['packagist']) {
$localConfig['repositories'][$key] = $packagistConfig;
}
foreach ($defaults as $name => $url) {
if (isset($repo[$name])) {
if (true === $repo[$name]) {
$localConfig['repositories'][$key] = array(
'type' => 'composer',
'url' => $url,
);
}
$loadPackagist = false;
break;
unset($defaults[$name]);
break;
}
}
}
} else {
$localConfig['repositories'] = array();
}
if ($loadPackagist) {
$localConfig['repositories'][] = $packagistConfig;
foreach ($defaults as $name => $url) {
$localConfig['repositories'][] = array(
'type' => 'composer',
'url' => $url,
);
}
return $localConfig;

View File

@ -12,6 +12,7 @@
namespace Composer\Package\Loader;
use Composer\Factory;
use Composer\Package\Version\VersionParser;
use Composer\Repository\RepositoryManager;
@ -59,10 +60,14 @@ class RootPackageLoader extends ArrayLoader
$package->setAliases($aliases);
}
$defaultRepositories = array_keys(Factory::$defaultComposerRepositories);
if (isset($config['repositories'])) {
foreach ($config['repositories'] as $index => $repo) {
if (isset($repo['packagist']) && $repo['packagist'] === false) {
continue;
foreach ($defaultRepositories as $name) {
if (isset($repo[$name]) && $repo[$name] === false) {
continue 2;
}
}
if (!is_array($repo)) {
throw new \UnexpectedValueException('Repository '.$index.' should be an array, '.gettype($repo).' given');

View File

@ -16,14 +16,31 @@ use Composer\Factory;
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)
public function testAddPackagistRepository($expected, $config, $defaults = null)
{
if (null !== $defaults) {
Factory::$defaultComposerRepositories = $defaults;
}
$factory = new Factory();
$ref = new \ReflectionMethod($factory, 'addPackagistRepository');
$ref = new \ReflectionMethod($factory, 'addComposerRepositories');
$ref->setAccessible(true);
$this->assertEquals($expected, $ref->invoke($factory, $config));
@ -60,6 +77,29 @@ class FactoryTest extends \PHPUnit_Framework_TestCase
)
);
$multirepo = array(
'example.com' => 'http://example.com',
'packagist' => 'http://packagist.org',
);
$data[] = array(
$f(
array('type' => 'composer', 'url' => 'http://example.com'),
array('type' => 'composer', 'url' => 'http://packagist.org')
),
$f(),
$multirepo,
);
$data[] = array(
$f(
array('type' => 'composer', 'url' => 'http://packagist.org'),
array('type' => 'composer', 'url' => 'http://example.com')
),
$f(array('packagist' => true)),
$multirepo,
);
return $data;
}
}

View File

@ -0,0 +1,34 @@
<?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\Test\Package\Loader;
use Composer\Package\Loader\RootPackageLoader;
use Composer\Repository\RepositoryManager;
class RootPackageLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testAllowsDisabledDefaultRepository()
{
$loader = new RootPackageLoader(
new RepositoryManager(
$this->getMock('Composer\\IO\\IOInterface'),
$this->getMock('Composer\\Config')
)
);
$repos = array(array('packagist' => false));
$package = $loader->load(array('repositories' => $repos));
$this->assertEquals($repos, $package->getRepositories());
}
}