From 056dc5d690337c871064ea2d54cd271cfda8c5ed Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 8 Sep 2012 00:45:18 +0200 Subject: [PATCH] Move config to all VcsDownloaders, enforce array for github-protocols --- src/Composer/Downloader/GitDownloader.php | 19 +++++-------------- src/Composer/Downloader/VcsDownloader.php | 5 ++++- src/Composer/Factory.php | 4 ++-- .../Test/Downloader/GitDownloaderTest.php | 2 +- .../Test/Downloader/HgDownloaderTest.php | 11 ++++++----- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Composer/Downloader/GitDownloader.php b/src/Composer/Downloader/GitDownloader.php index 2bddc201c..a882002ff 100644 --- a/src/Composer/Downloader/GitDownloader.php +++ b/src/Composer/Downloader/GitDownloader.php @@ -12,10 +12,6 @@ namespace Composer\Downloader; -use Composer\IO\IOInterface; -use Composer\Config; -use Composer\Util\Filesystem; -use Composer\Util\ProcessExecutor; use Composer\Package\PackageInterface; /** @@ -23,14 +19,6 @@ use Composer\Package\PackageInterface; */ 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} */ @@ -162,8 +150,11 @@ class GitDownloader extends VcsDownloader $handler = array($this, 'outputHandler'); // public github, autoswitch protocols - if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match) && $this->config->has('github-protocols')) { - $protocols = (array) $this->config->get('github-protocols'); + if (preg_match('{^(?:https?|git)(://github.com/.*)}', $url, $match)) { + $protocols = $this->config->get('github-protocols'); + if (!is_array($protocols)) { + throw new \RuntimeException('Config value "github-protocols" must be an array, got '.gettype($protocols)); + } $messages = array(); foreach ($protocols as $protocol) { $url = $protocol . $match[1]; diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php index 0713a843d..3c2f58a67 100644 --- a/src/Composer/Downloader/VcsDownloader.php +++ b/src/Composer/Downloader/VcsDownloader.php @@ -12,6 +12,7 @@ namespace Composer\Downloader; +use Composer\Config; use Composer\Package\PackageInterface; use Composer\Package\Version\VersionParser; use Composer\Util\ProcessExecutor; @@ -24,12 +25,14 @@ use Composer\Util\Filesystem; abstract class VcsDownloader implements DownloaderInterface { protected $io; + protected $config; protected $process; protected $filesystem; - public function __construct(IOInterface $io, ProcessExecutor $process = null, Filesystem $fs = null) + public function __construct(IOInterface $io, Config $config, ProcessExecutor $process = null, Filesystem $fs = null) { $this->io = $io; + $this->config = $config; $this->process = $process ?: new ProcessExecutor; $this->filesystem = $fs ?: new Filesystem; } diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index f3954c9f6..685f62364 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -223,8 +223,8 @@ class Factory { $dm = new Downloader\DownloadManager(); $dm->setDownloader('git', new Downloader\GitDownloader($io, $config)); - $dm->setDownloader('svn', new Downloader\SvnDownloader($io)); - $dm->setDownloader('hg', new Downloader\HgDownloader($io)); + $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config)); + $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config)); $dm->setDownloader('zip', new Downloader\ZipDownloader($io)); $dm->setDownloader('tar', new Downloader\TarDownloader($io)); $dm->setDownloader('phar', new Downloader\PharDownloader($io)); diff --git a/tests/Composer/Test/Downloader/GitDownloaderTest.php b/tests/Composer/Test/Downloader/GitDownloaderTest.php index c928cf959..d9e2c4f29 100644 --- a/tests/Composer/Test/Downloader/GitDownloaderTest.php +++ b/tests/Composer/Test/Downloader/GitDownloaderTest.php @@ -152,7 +152,7 @@ class GitDownloaderTest extends \PHPUnit_Framework_TestCase ->will($this->returnValue(0)); $config = new Config(); - $config->merge(array('config' => array('github-protocols' => 'http'))); + $config->merge(array('config' => array('github-protocols' => array('http')))); $downloader = $this->getDownloaderMock(null, $config, $processExecutor); $downloader->download($packageMock, 'composerPath'); diff --git a/tests/Composer/Test/Downloader/HgDownloaderTest.php b/tests/Composer/Test/Downloader/HgDownloaderTest.php index ad4f4fd9d..4624b8e73 100644 --- a/tests/Composer/Test/Downloader/HgDownloaderTest.php +++ b/tests/Composer/Test/Downloader/HgDownloaderTest.php @@ -16,13 +16,14 @@ use Composer\Downloader\HgDownloader; class HgDownloaderTest 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'); + $config = $config ?: $this->getMock('Composer\Config'); $executor = $executor ?: $this->getMock('Composer\Util\ProcessExecutor'); $filesystem = $filesystem ?: $this->getMock('Composer\Util\Filesystem'); - return new HgDownloader($io, $executor, $filesystem); + return new HgDownloader($io, $config, $executor, $filesystem); } /** @@ -55,7 +56,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo($expectedGitCommand)) ->will($this->returnValue(0)); - $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader->download($packageMock, 'composerPath'); } @@ -95,7 +96,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo($expectedUpdateCommand)) ->will($this->returnValue(0)); - $downloader = $this->getDownloaderMock(null, $processExecutor); + $downloader = $this->getDownloaderMock(null, null, $processExecutor); $downloader->update($packageMock, $packageMock, 'composerPath'); } @@ -114,7 +115,7 @@ class HgDownloaderTest extends \PHPUnit_Framework_TestCase ->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'); }