diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index cf45d0ab9..6f79e266e 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -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')) { diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index b1e778daa..c78ceffde 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -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()) )); } diff --git a/src/Composer/Command/SearchCommand.php b/src/Composer/Command/SearchCommand.php index 659d70904..fefdf4b9e 100644 --- a/src/Composer/Command/SearchCommand.php +++ b/src/Composer/Command/SearchCommand.php @@ -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'); diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index f9f211e46..f7695d9c9 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -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 diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 2996c50a5..96e0d52e7 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -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; diff --git a/src/Composer/Package/Loader/RootPackageLoader.php b/src/Composer/Package/Loader/RootPackageLoader.php index 734e28c1a..ae1d46f5d 100644 --- a/src/Composer/Package/Loader/RootPackageLoader.php +++ b/src/Composer/Package/Loader/RootPackageLoader.php @@ -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'); diff --git a/tests/Composer/Test/FactoryTest.php b/tests/Composer/Test/FactoryTest.php index 2bb838fef..146095c59 100644 --- a/tests/Composer/Test/FactoryTest.php +++ b/tests/Composer/Test/FactoryTest.php @@ -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; } } diff --git a/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php new file mode 100644 index 000000000..674c79382 --- /dev/null +++ b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php @@ -0,0 +1,34 @@ + + * Jordi Boggiano + * + * 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()); + } +}