diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 2996c50a5..8929481c6 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -38,6 +38,11 @@ class Factory } } + // Protect directory against web access + if (!file_exists($home . '/.htaccess')) { + @mkdir($home, 0777, true) && @file_put_contents($home . '/.htaccess', 'deny from all'); + } + $config = new Config(); $file = new JsonFile($home.'/config.json'); diff --git a/src/Composer/Repository/Vcs/GitBitbucketDriver.php b/src/Composer/Repository/Vcs/GitBitbucketDriver.php index c0132cd10..b5cb8960d 100644 --- a/src/Composer/Repository/Vcs/GitBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/GitBitbucketDriver.php @@ -27,20 +27,14 @@ class GitBitbucketDriver extends VcsDriver implements VcsDriverInterface protected $rootIdentifier; protected $infoCache = array(); - public function __construct($url, IOInterface $io) - { - preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $url, $match); - $this->owner = $match[1]; - $this->repository = $match[2]; - - parent::__construct($url, $io); - } - /** * {@inheritDoc} */ public function initialize() { + preg_match('#^https://bitbucket\.org/([^/]+)/(.+?)\.git$#', $this->url, $match); + $this->owner = $match[1]; + $this->repository = $match[2]; } /** diff --git a/src/Composer/Repository/Vcs/GitDriver.php b/src/Composer/Repository/Vcs/GitDriver.php index 73f9e1aed..94ccb61ce 100644 --- a/src/Composer/Repository/Vcs/GitDriver.php +++ b/src/Composer/Repository/Vcs/GitDriver.php @@ -28,11 +28,6 @@ class GitDriver extends VcsDriver protected $repoDir; protected $infoCache = array(); - public function __construct($url, IOInterface $io, ProcessExecutor $process = null) - { - parent::__construct($url, $io, $process); - } - /** * {@inheritDoc} */ @@ -41,7 +36,7 @@ class GitDriver extends VcsDriver if (static::isLocalUrl($this->url)) { $this->repoDir = str_replace('file://', '', $this->url); } else { - $this->repoDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/'; + $this->repoDir = $this->config->get('home') . '/cache.git/' . preg_replace('{[^a-z0-9.]}i', '-', $this->url) . '/'; // update the repo if it is a valid git repository if (is_dir($this->repoDir) && 0 === $this->process->execute('git remote', $output, $this->repoDir)) { diff --git a/src/Composer/Repository/Vcs/GitHubDriver.php b/src/Composer/Repository/Vcs/GitHubDriver.php index 42633aafe..9091d0c6f 100644 --- a/src/Composer/Repository/Vcs/GitHubDriver.php +++ b/src/Composer/Repository/Vcs/GitHubDriver.php @@ -38,28 +38,15 @@ class GitHubDriver extends VcsDriver */ protected $gitDriver; - /** - * Constructor - * - * @param string $url - * @param IOInterface $io - * @param ProcessExecutor $process - * @param RemoteFilesystem $remoteFilesystem - */ - public function __construct($url, IOInterface $io, ProcessExecutor $process = null, RemoteFilesystem $remoteFilesystem = null) - { - preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $url, $match); - $this->owner = $match[1]; - $this->repository = $match[2]; - - parent::__construct($url, $io, $process, $remoteFilesystem); - } - /** * {@inheritDoc} */ public function initialize() { + preg_match('#^(?:https?|git)://github\.com/([^/]+)/(.+?)(?:\.git)?$#', $this->url, $match); + $this->owner = $match[1]; + $this->repository = $match[2]; + $this->fetchRootIdentifier(); } @@ -248,6 +235,7 @@ class GitHubDriver extends VcsDriver $this->gitDriver = new GitDriver( $this->generateSshUrl(), $this->io, + $this->config, $this->process, $this->remoteFilesystem ); diff --git a/src/Composer/Repository/Vcs/HgBitbucketDriver.php b/src/Composer/Repository/Vcs/HgBitbucketDriver.php index 993ece82d..43fff9aba 100644 --- a/src/Composer/Repository/Vcs/HgBitbucketDriver.php +++ b/src/Composer/Repository/Vcs/HgBitbucketDriver.php @@ -27,20 +27,14 @@ class HgBitbucketDriver extends VcsDriver protected $rootIdentifier; protected $infoCache = array(); - public function __construct($url, IOInterface $io) - { - preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $url, $match); - $this->owner = $match[1]; - $this->repository = $match[2]; - - parent::__construct($url, $io); - } - /** * {@inheritDoc} */ public function initialize() { + preg_match('#^https://bitbucket\.org/([^/]+)/([^/]+)/?$#', $this->url, $match); + $this->owner = $match[1]; + $this->repository = $match[2]; } /** diff --git a/src/Composer/Repository/Vcs/HgDriver.php b/src/Composer/Repository/Vcs/HgDriver.php index 833669cf9..45186da7c 100644 --- a/src/Composer/Repository/Vcs/HgDriver.php +++ b/src/Composer/Repository/Vcs/HgDriver.php @@ -26,24 +26,21 @@ class HgDriver extends VcsDriver protected $rootIdentifier; protected $infoCache = array(); - public function __construct($url, IOInterface $io, ProcessExecutor $process = null) - { - $this->tmpDir = sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9]}i', '-', $url) . '/'; - - parent::__construct($url, $io, $process); - } - /** * {@inheritDoc} */ public function initialize() { - $url = escapeshellarg($this->url); - $tmpDir = escapeshellarg($this->tmpDir); + $this->tmpDir = $this->config->get('home') . '/cache.hg/' . preg_replace('{[^a-z0-9]}i', '-', $this->url) . '/'; + if (is_dir($this->tmpDir)) { - $this->process->execute(sprintf('cd %s && hg pull -u', $tmpDir), $output); + $this->process->execute(sprintf('cd %s && hg pull -u', escapeshellarg($this->tmpDir)), $output); } else { - $this->process->execute(sprintf('cd %s && hg clone %s %s', escapeshellarg(sys_get_temp_dir()), $url, $tmpDir), $output); + $dir = dirname($this->tmpDir); + if (!is_dir($dir)) { + mkdir($dir, 0777, true); + } + $this->process->execute(sprintf('cd %s && hg clone %s %s', escapeshellarg($dir), escapeshellarg($this->url), escapeshellarg($this->tmpDir)), $output); } $this->getTags(); diff --git a/src/Composer/Repository/Vcs/SvnDriver.php b/src/Composer/Repository/Vcs/SvnDriver.php index 6f13296ce..d431cb288 100644 --- a/src/Composer/Repository/Vcs/SvnDriver.php +++ b/src/Composer/Repository/Vcs/SvnDriver.php @@ -33,25 +33,7 @@ class SvnDriver extends VcsDriver /** * @var \Composer\Util\Svn */ - protected $util; - - /** - * @param string $url - * @param IOInterface $io - * @param ProcessExecutor $process - * - * @return $this - */ - public function __construct($url, IOInterface $io, ProcessExecutor $process = null) - { - $url = self::normalizeUrl($url); - parent::__construct($this->baseUrl = rtrim($url, '/'), $io, $process); - - if (false !== ($pos = strrpos($url, '/trunk'))) { - $this->baseUrl = substr($url, 0, $pos); - } - $this->util = new SvnUtil($this->baseUrl, $io, $this->process); - } + private $util; /** * Execute an SVN command and try to fix up the process with credentials @@ -64,6 +46,10 @@ class SvnDriver extends VcsDriver */ protected function execute($command, $url) { + if (null === $this->util) { + $this->util = new SvnUtil($this->baseUrl, $this->io, $this->process); + } + try { return $this->util->execute($command, $url); } catch (\RuntimeException $e) { @@ -78,6 +64,12 @@ class SvnDriver extends VcsDriver */ public function initialize() { + $this->url = rtrim(self::normalizeUrl($this->url), '/'); + + if (false !== ($pos = strrpos($this->url, '/trunk'))) { + $this->baseUrl = substr($this->url, 0, $pos); + } + $this->getBranches(); $this->getTags(); } diff --git a/src/Composer/Repository/Vcs/VcsDriver.php b/src/Composer/Repository/Vcs/VcsDriver.php index 99c705c50..d13b4b4a1 100644 --- a/src/Composer/Repository/Vcs/VcsDriver.php +++ b/src/Composer/Repository/Vcs/VcsDriver.php @@ -13,6 +13,7 @@ namespace Composer\Repository\Vcs; use Composer\Downloader\TransportException; +use Composer\Config; use Composer\IO\IOInterface; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; @@ -26,6 +27,7 @@ abstract class VcsDriver implements VcsDriverInterface { protected $url; protected $io; + protected $config; protected $process; protected $remoteFilesystem; @@ -34,13 +36,15 @@ abstract class VcsDriver implements VcsDriverInterface * * @param string $url The URL * @param IOInterface $io The IO instance + * @param Config $config The composer configuration * @param ProcessExecutor $process Process instance, injectable for mocking * @param callable $remoteFilesystem Remote Filesystem, injectable for mocking */ - public function __construct($url, IOInterface $io, ProcessExecutor $process = null, $remoteFilesystem = null) + final public function __construct($url, IOInterface $io, Config $config, ProcessExecutor $process = null, $remoteFilesystem = null) { $this->url = $url; $this->io = $io; + $this->config = $config; $this->process = $process ?: new ProcessExecutor; $this->remoteFilesystem = $remoteFilesystem ?: new RemoteFilesystem($io); } @@ -58,7 +62,6 @@ abstract class VcsDriver implements VcsDriverInterface return false; } - /** * Get the https or http protocol depending on SSL support. * diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index b70e7c09d..ecc435582 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -30,6 +30,7 @@ class VcsRepository extends ArrayRepository protected $packageName; protected $verbose; protected $io; + protected $config; protected $versionParser; protected $type; @@ -48,20 +49,21 @@ class VcsRepository extends ArrayRepository $this->io = $io; $this->type = isset($repoConfig['type']) ? $repoConfig['type'] : 'vcs'; $this->verbose = $io->isVerbose(); + $this->config = $config; } public function getDriver() { if (isset($this->drivers[$this->type])) { $class = $this->drivers[$this->type]; - $driver = new $class($this->url, $this->io); + $driver = new $class($this->url, $this->io, $this->config); $driver->initialize(); return $driver; } foreach ($this->drivers as $driver) { if ($driver::supports($this->io, $this->url)) { - $driver = new $driver($this->url, $this->io); + $driver = new $driver($this->url, $this->io, $this->config); $driver->initialize(); return $driver; } @@ -69,7 +71,7 @@ class VcsRepository extends ArrayRepository foreach ($this->drivers as $driver) { if ($driver::supports($this->io, $this->url, true)) { - $driver = new $driver($this->url, $this->io); + $driver = new $driver($this->url, $this->io, $this->config); $driver->initialize(); return $driver; } diff --git a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php index 845fb990f..88483bff8 100644 --- a/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/GitHubDriverTest.php @@ -15,6 +15,7 @@ namespace Composer\Test\Repository\Vcs; use Composer\Downloader\TransportException; use Composer\Repository\Vcs\GitHubDriver; use Composer\Util\Filesystem; +use Composer\Config; /** * @author Beau Simensen @@ -64,7 +65,7 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false)) ->will($this->returnValue('{"master_branch": "test_master"}')); - $gitHubDriver = new GitHubDriver($repoUrl, $io, null, $remoteFilesystem); + $gitHubDriver = new GitHubDriver($repoUrl, $io, new Config(), null, $remoteFilesystem); $gitHubDriver->initialize(); $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha)); @@ -114,7 +115,7 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase ->with($this->equalTo($repoUrl), $this->equalTo($repoApiUrl), $this->equalTo(false)) ->will($this->returnValue('{"master_branch": "test_master"}')); - $gitHubDriver = new GitHubDriver($repoUrl, $io, null, $remoteFilesystem); + $gitHubDriver = new GitHubDriver($repoUrl, $io, new Config(), null, $remoteFilesystem); $gitHubDriver->initialize(); $this->setAttribute($gitHubDriver, 'tags', array($identifier => $sha)); @@ -171,7 +172,14 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase // clean local clone if present $fs = new Filesystem(); - $fs->removeDirectory(sys_get_temp_dir() . '/composer-' . preg_replace('{[^a-z0-9.]}i', '-', $repoSshUrl) . '/'); + $fs->removeDirectory(sys_get_temp_dir() . '/composer-test'); + + $config = new Config(); + $config->merge(array( + 'config' => array( + 'home' => sys_get_temp_dir() . '/composer-test', + ), + )); $process->expects($this->at(0)) ->method('execute') @@ -202,7 +210,7 @@ class GitHubDriverTest extends \PHPUnit_Framework_TestCase ->method('splitLines') ->will($this->returnValue(array('* test_master'))); - $gitHubDriver = new GitHubDriver($repoUrl, $io, $process, $remoteFilesystem); + $gitHubDriver = new GitHubDriver($repoUrl, $io, $config, $process, $remoteFilesystem); $gitHubDriver->initialize(); $this->assertEquals('test_master', $gitHubDriver->getRootIdentifier()); diff --git a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php index 5399c394e..6f85faa0c 100644 --- a/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php +++ b/tests/Composer/Test/Repository/Vcs/SvnDriverTest.php @@ -14,6 +14,7 @@ namespace Composer\Test\Repository\Vcs; use Composer\Repository\Vcs\SvnDriver; use Composer\IO\NullIO; +use Composer\Config; class SvnDriverTest extends \PHPUnit_Framework_TestCase { @@ -39,7 +40,7 @@ class SvnDriverTest extends \PHPUnit_Framework_TestCase ->method('getErrorOutput') ->will($this->returnValue($output)); - $svn = new SvnDriver('http://till:secret@corp.svn.local/repo', $console, $process); + $svn = new SvnDriver('http://till:secret@corp.svn.local/repo', $console, new Config(), $process); $svn->getTags(); } diff --git a/tests/Composer/Test/Repository/VcsRepositoryTest.php b/tests/Composer/Test/Repository/VcsRepositoryTest.php index 9c09db0a2..c2060d5b7 100644 --- a/tests/Composer/Test/Repository/VcsRepositoryTest.php +++ b/tests/Composer/Test/Repository/VcsRepositoryTest.php @@ -19,6 +19,7 @@ use Composer\Repository\Vcs\GitDriver; use Composer\Util\Filesystem; use Composer\Util\ProcessExecutor; use Composer\IO\NullIO; +use Composer\Config; class VcsRepositoryTest extends \PHPUnit_Framework_TestCase { @@ -123,7 +124,7 @@ class VcsRepositoryTest extends \PHPUnit_Framework_TestCase 'dev-master' => true, ); - $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO); + $repo = new VcsRepository(array('url' => self::$gitRepo, 'type' => 'vcs'), new NullIO, new Config()); $packages = $repo->getPackages(); $dumper = new ArrayDumper();