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\Installer\ProjectInstaller;
use Composer\IO\IOInterface; use Composer\IO\IOInterface;
use Composer\Repository\ComposerRepository; use Composer\Repository\ComposerRepository;
use Composer\Repository\CompositeRepository;
use Composer\Repository\FilesystemRepository; use Composer\Repository\FilesystemRepository;
use Composer\Repository\InstalledFilesystemRepository; use Composer\Repository\InstalledFilesystemRepository;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
@ -85,7 +86,7 @@ EOT
$config = Factory::createConfig(); $config = Factory::createConfig();
if (null === $repositoryUrl) { 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)) { } 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

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

View File

@ -53,10 +53,10 @@ 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 {
$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; $installedRepo = $platformRepo;
$packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig()); $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
$repos = new CompositeRepository(array($installedRepo, $packagist));
} }
$tokens = $input->getArgument('tokens'); $tokens = $input->getArgument('tokens');

View File

@ -64,10 +64,10 @@ 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 {
$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; $installedRepo = $platformRepo;
$packagist = new ComposerRepository(array('url' => 'http://packagist.org'), $this->getIO(), Factory::createConfig()); $repos = new CompositeRepository(array_merge(array($installedRepo), $defaultRepos));
$repos = new CompositeRepository(array($installedRepo, $packagist));
} }
// show single package or single version // show single package or single version

View File

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

View File

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