From 3b6bd761ecd86ed7b9ccd4618e9e28fc2d4a7891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Fri, 7 Sep 2012 15:02:39 +0200 Subject: [PATCH] Set protocols for github in composer config file --- src/Composer/Command/CreateProjectCommand.php | 9 ++-- src/Composer/Config.php | 1 + src/Composer/Downloader/GitDownloader.php | 16 +++++- src/Composer/Factory.php | 6 +-- .../Test/Downloader/GitDownloaderTest.php | 54 ++++++++++++++++--- 5 files changed, 69 insertions(+), 17 deletions(-) diff --git a/src/Composer/Command/CreateProjectCommand.php b/src/Composer/Command/CreateProjectCommand.php index 22ae23f73bad..315bc8d254c8 100644 --- a/src/Composer/Command/CreateProjectCommand.php +++ b/src/Composer/Command/CreateProjectCommand.php @@ -89,12 +89,13 @@ protected function execute(InputInterface $input, OutputInterface $output) public function installProject(IOInterface $io, $packageName, $directory = null, $packageVersion = null, $preferSource = false, $installDevPackages = false, $repositoryUrl = null, $disableCustomInstallers = false, $noScripts = false) { - $dm = $this->createDownloadManager($io); + $config = Factory::createConfig(); + + $dm = $this->createDownloadManager($io, $config); if ($preferSource) { $dm->setPreferSource(true); } - $config = Factory::createConfig(); if (null === $repositoryUrl) { $sourceRepo = new CompositeRepository(Factory::createDefaultRepositories($io, $config)); } elseif ("json" === pathinfo($repositoryUrl, PATHINFO_EXTENSION)) { @@ -183,10 +184,10 @@ public function installProject(IOInterface $io, $packageName, $directory = null, $installer->run(); } - protected function createDownloadManager(IOInterface $io) + protected function createDownloadManager(IOInterface $io, Config $config) { $factory = new Factory(); - return $factory->createDownloadManager($io); + return $factory->createDownloadManager($io, $config); } } diff --git a/src/Composer/Config.php b/src/Composer/Config.php index 73e9cea810bd..39e95fd0378f 100644 --- a/src/Composer/Config.php +++ b/src/Composer/Config.php @@ -22,6 +22,7 @@ class Config 'vendor-dir' => 'vendor', 'bin-dir' => '{$vendor-dir}/bin', 'notify-on-install' => true, + 'github-protocols' => array('git', 'https', 'http'), ); public static $defaultRepositories = array( diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index d765f2e9f3e7..2bddc201cbcc 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -12,6 +12,10 @@ namespace Composer\Downloader; +use Composer\IO\IOInterface; +use Composer\Config; +use Composer\Util\Filesystem; +use Composer\Util\ProcessExecutor; use Composer\Package\PackageInterface; /** @@ -19,6 +23,14 @@ */ class GitDownloader extends VcsDownloader { + private $config; + + public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null) + { + parent::__construct($io, $process, $fs); + $this->config = $config; + } + /** * {@inheritDoc} */ @@ -150,8 +162,8 @@ protected function runCommand($commandCallable, $url, $path = null) $handler = array($this, 'outputHandler'); // public github, autoswitch protocols - if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) { - $protocols = array('git', 'https', 'http'); + if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match) && $this->config->has('github-protocols')) { + $protocols = (array) $this->config->get('github-protocols'); $messages = array(); foreach ($protocols as $protocol) { $url = $protocol . $match[1]; diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 6a52e4e10ac7..f3954c9f6689 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -155,7 +155,7 @@ public function createComposer(IOInterface $io, $localConfig = null) $package = $loader->load($localConfig); // initialize download manager - $dm = $this->createDownloadManager($io); + $dm = $this->createDownloadManager($io, $config); // initialize installation manager $im = $this->createInstallationManager($config); @@ -219,10 +219,10 @@ protected function addLocalRepository(RepositoryManager $rm, $vendorDir) * @param IO\IOInterface $io * @return Downloader\DownloadManager */ - public function createDownloadManager(IOInterface $io) + public function createDownloadManager(IOInterface $io, Config $config) { $dm = new Downloader\DownloadManager(); - $dm->setDownloader('git', new Downloader\GitDownloader($io)); + $dm->setDownloader('git', new Downloader\GitDownloader($io, $config)); $dm->setDownloader('svn', new Downloader\SvnDownloader($io)); $dm->setDownloader('hg', new Downloader\HgDownloader($io)); $dm->setDownloader('zip', new Downloader\ZipDownloader($io)); diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index fb5a5f388f8b..c928cf9593bf 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -13,16 +13,23 @@ namespace Composer\Test\Downloader; use Composer\Downloader\GitDownloader; +use Composer\Config; class GitDownloaderTest extends \PHPUnit_Framework_TestCase { - protected function getDownloaderMock($io = null, $executor = null, $filesystem = null) + protected function getDownloaderMock($io = null, $config = null, $executor = null, $filesystem = null) { $io = $io ?: $this->getMock('Composer\IO\IOInterface'); $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor'); $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem'); + if (!$config) { + $config = $this->getMock('Composer\Config'); + $config->expects($this->any()) + ->method('has') + ->will($this->returnValue(false)); + } - return new GitDownloader($io, $executor, $filesystem); + return new GitDownloader($io, $config, $executor, $filesystem); } /** @@ -64,7 +71,7 @@ public function testDownload() ->with($this->equalTo($this->getCmd("git checkout '1234567890123456789012345678901234567890' && git reset --hard '1234567890123456789012345678901234567890'")), $this->equalTo(null), $this->equalTo('composerPath')) ->will($this->returnValue(0)); - $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader->download($packageMock, 'composerPath'); } @@ -116,7 +123,38 @@ public function testDownloadUsesVariousProtocolsAndSetsPushUrlForGithub() ->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath')) ->will($this->returnValue(0)); - $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); + $downloader->download($packageMock, 'composerPath'); + } + + public function testDownloadUsesCustomVariousProtocolsForGithub() + { + $packageMock = $this->getMock('Composer\Package\PackageInterface'); + $packageMock->expects($this->any()) + ->method('getSourceReference') + ->will($this->returnValue('ref')); + $packageMock->expects($this->any()) + ->method('getSourceUrl') + ->will($this->returnValue('https://github.com/composer/composer')); + $packageMock->expects($this->any()) + ->method('getPrettyVersion') + ->will($this->returnValue('1.0.0')); + $processExecutor = $this->getMock('Composer\Util\ProcessExecutor'); + + $expectedGitCommand = $this->getCmd("git clone 'http://github.com/composer/composer' 'composerPath' && cd 'composerPath' && git remote add composer 'http://github.com/composer/composer' && git fetch composer"); + $processExecutor->expects($this->at(0)) + ->method('execute') + ->with($this->equalTo($expectedGitCommand)) + ->will($this->returnValue(0)); + + $processExecutor->expects($this->exactly(4)) + ->method('execute') + ->will($this->returnValue(0)); + + $config = new Config(); + $config->merge(array('config' => array('github-protocols' => 'http'))); + + $downloader = $this->getDownloaderMock(null, $config, $processExecutor); $downloader->download($packageMock, 'composerPath'); } @@ -139,7 +177,7 @@ public function testDownloadThrowsRuntimeExceptionIfGitCommandFails() ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(1)); - $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader->download($packageMock, 'composerPath'); } @@ -195,7 +233,7 @@ public function testUpdate() ->with($this->equalTo($this->getCmd("git checkout 'ref' && git reset --hard 'ref'")), $this->equalTo(null), $this->equalTo('composerPath')) ->will($this->returnValue(0)); - $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); $downloader->update($packageMock, $packageMock, 'composerPath'); } @@ -228,7 +266,7 @@ public function testUpdateThrowsRuntimeExceptionIfGitCommandFails() ->with($this->equalTo($expectedGitUpdateCommand)) ->will($this->returnValue(1)); - $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader = $this->getDownloaderMock(null, new Config(), $processExecutor); $downloader->update($packageMock, $packageMock, 'composerPath'); } @@ -248,7 +286,7 @@ public function testRemove() ->with($this->equalTo('composerPath')) ->will($this->returnValue(true)); - $downloader = $this->getDownloaderMock(null, $processExecutor, $filesystem); + $downloader = $this->getDownloaderMock(null, null, $processExecutor, $filesystem); $downloader->remove($packageMock, 'composerPath'); }