diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index 617191be7..ae59aafa4 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -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')) { diff --git a/src/Composer/Command/InitCommand.php b/src/Composer/Command/InitCommand.php index 1753edae8..e1c70d0c2 100644 --- a/src/Composer/Command/InitCommand.php +++ b/src/Composer/Command/InitCommand.php @@ -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()) )); } diff --git a/src/Composer/Command/SearchCommand.php b/src/Composer/Command/SearchCommand.php index 73c601876..d61c2dc8d 100644 --- a/src/Composer/Command/SearchCommand.php +++ b/src/Composer/Command/SearchCommand.php @@ -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)); } diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index 26c1671ff..314fcfa34 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -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)); } diff --git a/src/Composer/Config.php b/src/Composer/Config.php index b4ea02af6..d202f2e10 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -17,17 +17,25 @@ namespace Composer; */ 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 $repositories; public function __construct() { // load defaults - $this->config = array( - 'process-timeout' => 300, - 'vendor-dir' => 'vendor', - 'bin-dir' => '{$vendor-dir}/bin', - 'notify-on-install' => true, - ); + $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; } /** diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 25ca8c17c..de2f67e0f 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -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) { diff --git a/src/Composer/Package/Loader/RootPackageLoader.php b/src/Composer/Package/Loader/RootPackageLoader.php index 743fdea1b..dfc59fc17 100644 --- a/src/Composer/Package/Loader/RootPackageLoader.php +++ b/src/Composer/Package/Loader/RootPackageLoader.php @@ -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) { diff --git a/tests/Composer/Test/FactoryTest.php b/tests/Composer/Test/FactoryTest.php index 90d6a4ec1..7ed92846b 100644 --- a/tests/Composer/Test/FactoryTest.php +++ b/tests/Composer/Test/FactoryTest.php @@ -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, ); diff --git a/tests/Composer/Test/Mock/FactoryMock.php b/tests/Composer/Test/Mock/FactoryMock.php index b2c087239..85644f54c 100644 --- a/tests/Composer/Test/Mock/FactoryMock.php +++ b/tests/Composer/Test/Mock/FactoryMock.php @@ -34,7 +34,7 @@ class FactoryMock extends Factory { } - protected function addPackagistRepository(array $localConfig) + protected function addDefaultRepositories(Config $config, array $localConfig) { return $localConfig; } diff --git a/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php index 13be4c7af..d5d3a0b03 100644 --- a/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php +++ b/tests/Composer/Test/Package/Loader/RootPackageLoaderTest.php @@ -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));