From c1cc6bfecffe2e0b4859b2ffc328c4636890a022 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 10 Jan 2016 20:06:10 +0000 Subject: [PATCH] Avoid doing too many loads of the remotefilesystem --- src/Composer/Downloader/GzipDownloader.php | 5 +- src/Composer/Downloader/RarDownloader.php | 5 +- src/Composer/Downloader/VcsDownloader.php | 2 +- src/Composer/Downloader/XzDownloader.php | 5 +- src/Composer/Downloader/ZipDownloader.php | 5 +- src/Composer/Factory.php | 46 +++++++++++-------- .../Repository/ComposerRepository.php | 4 +- src/Composer/Repository/RepositoryManager.php | 7 ++- tests/Composer/Test/Mock/FactoryMock.php | 2 +- 9 files changed, 49 insertions(+), 32 deletions(-) diff --git a/src/Composer/Downloader/GzipDownloader.php b/src/Composer/Downloader/GzipDownloader.php index f8624ab24..ae7d7f17a 100644 --- a/src/Composer/Downloader/GzipDownloader.php +++ b/src/Composer/Downloader/GzipDownloader.php @@ -17,6 +17,7 @@ use Composer\Cache; use Composer\EventDispatcher\EventDispatcher; use Composer\Package\PackageInterface; use Composer\Util\ProcessExecutor; +use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; /** @@ -28,10 +29,10 @@ class GzipDownloader extends ArchiveDownloader { protected $process; - public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null) + public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null) { $this->process = $process ?: new ProcessExecutor($io); - parent::__construct($io, $config, $eventDispatcher, $cache); + parent::__construct($io, $config, $eventDispatcher, $cache, $rfs); } protected function extract($file, $path) diff --git a/src/Composer/Downloader/RarDownloader.php b/src/Composer/Downloader/RarDownloader.php index 12823422d..81e11785e 100644 --- a/src/Composer/Downloader/RarDownloader.php +++ b/src/Composer/Downloader/RarDownloader.php @@ -16,6 +16,7 @@ use Composer\Config; use Composer\Cache; use Composer\EventDispatcher\EventDispatcher; use Composer\Util\ProcessExecutor; +use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; use RarArchive; @@ -30,10 +31,10 @@ class RarDownloader extends ArchiveDownloader { protected $process; - public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null) + public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null) { $this->process = $process ?: new ProcessExecutor($io); - parent::__construct($io, $config, $eventDispatcher, $cache); + parent::__construct($io, $config, $eventDispatcher, $cache, $rfs); } protected function extract($file, $path) diff --git a/src/Composer/Downloader/VcsDownloader.php b/src/Composer/Downloader/VcsDownloader.php index 2d021e3a6..fe6c9b40f 100644 --- a/src/Composer/Downloader/VcsDownloader.php +++ b/src/Composer/Downloader/VcsDownloader.php @@ -37,7 +37,7 @@ abstract class VcsDownloader implements DownloaderInterface, ChangeReportInterfa $this->io = $io; $this->config = $config; $this->process = $process ?: new ProcessExecutor($io); - $this->filesystem = $fs ?: new Filesystem; + $this->filesystem = $fs ?: new Filesystem($this->process); } /** diff --git a/src/Composer/Downloader/XzDownloader.php b/src/Composer/Downloader/XzDownloader.php index 81c15af9a..4a9b854d3 100644 --- a/src/Composer/Downloader/XzDownloader.php +++ b/src/Composer/Downloader/XzDownloader.php @@ -17,6 +17,7 @@ use Composer\Cache; use Composer\EventDispatcher\EventDispatcher; use Composer\Package\PackageInterface; use Composer\Util\ProcessExecutor; +use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; /** @@ -29,11 +30,11 @@ class XzDownloader extends ArchiveDownloader { protected $process; - public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null) + public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null) { $this->process = $process ?: new ProcessExecutor($io); - parent::__construct($io, $config, $eventDispatcher, $cache); + parent::__construct($io, $config, $eventDispatcher, $cache, $rfs); } protected function extract($file, $path) diff --git a/src/Composer/Downloader/ZipDownloader.php b/src/Composer/Downloader/ZipDownloader.php index dea34aedd..6faaaaa4f 100644 --- a/src/Composer/Downloader/ZipDownloader.php +++ b/src/Composer/Downloader/ZipDownloader.php @@ -16,6 +16,7 @@ use Composer\Config; use Composer\Cache; use Composer\EventDispatcher\EventDispatcher; use Composer\Util\ProcessExecutor; +use Composer\Util\RemoteFilesystem; use Composer\IO\IOInterface; use ZipArchive; @@ -26,10 +27,10 @@ class ZipDownloader extends ArchiveDownloader { protected $process; - public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null) + public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, Cache $cache = null, ProcessExecutor $process = null, RemoteFilesystem $rfs = null) { $this->process = $process ?: new ProcessExecutor($io); - parent::__construct($io, $config, $eventDispatcher, $cache); + parent::__construct($io, $config, $eventDispatcher, $cache, $rfs); } protected function extract($file, $path) diff --git a/src/Composer/Factory.php b/src/Composer/Factory.php index 9eaf0a3f2..41c281378 100644 --- a/src/Composer/Factory.php +++ b/src/Composer/Factory.php @@ -19,6 +19,7 @@ use Composer\Package\Archiver; use Composer\Package\Version\VersionGuesser; use Composer\Repository\RepositoryManager; use Composer\Repository\WritableRepositoryInterface; +use Composer\Util\Filesystem; use Composer\Util\ProcessExecutor; use Composer\Util\RemoteFilesystem; use Symfony\Component\Console\Formatter\OutputFormatterStyle; @@ -162,7 +163,7 @@ class Factory throw new \InvalidArgumentException('This function requires either an IOInterface or a RepositoryManager'); } $factory = new static; - $rm = $factory->createRepositoryManager($io, $config); + $rm = $factory->createRepositoryManager($io, $config, null, self::createRemoteFilesystem($io, $config)); } foreach ($config->getRepositories() as $index => $repo) { @@ -262,12 +263,14 @@ class Factory $io->loadConfiguration($config); } + $rfs = self::createRemoteFilesystem($io, $config); + // initialize event dispatcher $dispatcher = new EventDispatcher($composer, $io); $composer->setEventDispatcher($dispatcher); // initialize repository manager - $rm = $this->createRepositoryManager($io, $config, $dispatcher); + $rm = $this->createRepositoryManager($io, $config, $dispatcher, $rfs); $composer->setRepositoryManager($rm); // load local repository @@ -292,7 +295,7 @@ class Factory if ($fullLoad) { // initialize download manager - $dm = $this->createDownloadManager($io, $config, $dispatcher); + $dm = $this->createDownloadManager($io, $config, $dispatcher, $rfs); $composer->setDownloadManager($dm); // initialize autoload generator @@ -336,9 +339,9 @@ class Factory * @param EventDispatcher $eventDispatcher * @return Repository\RepositoryManager */ - protected function createRepositoryManager(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null) + protected function createRepositoryManager(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null) { - $rm = new RepositoryManager($io, $config, $eventDispatcher); + $rm = new RepositoryManager($io, $config, $eventDispatcher, $rfs); $rm->setRepositoryClass('composer', 'Composer\Repository\ComposerRepository'); $rm->setRepositoryClass('vcs', 'Composer\Repository\VcsRepository'); $rm->setRepositoryClass('package', 'Composer\Repository\PackageRepository'); @@ -391,7 +394,7 @@ class Factory * @param EventDispatcher $eventDispatcher * @return Downloader\DownloadManager */ - public function createDownloadManager(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null) + public function createDownloadManager(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null) { $cache = null; if ($config->get('cache-files-ttl') > 0) { @@ -412,18 +415,21 @@ class Factory break; } - $dm->setDownloader('git', new Downloader\GitDownloader($io, $config)); - $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config)); - $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config)); + $executor = new ProcessExecutor($io); + $fs = new Filesystem($executor); + + $dm->setDownloader('git', new Downloader\GitDownloader($io, $config, $executor, $fs)); + $dm->setDownloader('svn', new Downloader\SvnDownloader($io, $config, $executor, $fs)); + $dm->setDownloader('hg', new Downloader\HgDownloader($io, $config, $executor, $fs)); $dm->setDownloader('perforce', new Downloader\PerforceDownloader($io, $config)); - $dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $eventDispatcher, $cache)); - $dm->setDownloader('rar', new Downloader\RarDownloader($io, $config, $eventDispatcher, $cache)); - $dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $eventDispatcher, $cache)); - $dm->setDownloader('gzip', new Downloader\GzipDownloader($io, $config, $eventDispatcher, $cache)); - $dm->setDownloader('xz', new Downloader\XzDownloader($io, $config, $eventDispatcher, $cache)); - $dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $eventDispatcher, $cache)); - $dm->setDownloader('file', new Downloader\FileDownloader($io, $config, $eventDispatcher, $cache)); - $dm->setDownloader('path', new Downloader\PathDownloader($io, $config, $eventDispatcher, $cache)); + $dm->setDownloader('zip', new Downloader\ZipDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs)); + $dm->setDownloader('rar', new Downloader\RarDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs)); + $dm->setDownloader('tar', new Downloader\TarDownloader($io, $config, $eventDispatcher, $cache, $rfs)); + $dm->setDownloader('gzip', new Downloader\GzipDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs)); + $dm->setDownloader('xz', new Downloader\XzDownloader($io, $config, $eventDispatcher, $cache, $executor, $rfs)); + $dm->setDownloader('phar', new Downloader\PharDownloader($io, $config, $eventDispatcher, $cache, $rfs)); + $dm->setDownloader('file', new Downloader\FileDownloader($io, $config, $eventDispatcher, $cache, $rfs)); + $dm->setDownloader('path', new Downloader\PathDownloader($io, $config, $eventDispatcher, $cache, $rfs)); return $dm; } @@ -515,9 +521,13 @@ class Factory */ public static function createRemoteFilesystem(IOInterface $io, Config $config = null, $options = array()) { + static $warned = false; $disableTls = false; if (isset($config) && $config->get('disable-tls') === true) { - $io->write('You are running Composer with SSL/TLS protection disabled.'); + if (!$warned) { + $io->write('You are running Composer with SSL/TLS protection disabled.'); + } + $warned = true; $disableTls = true; } elseif (!extension_loaded('openssl')) { throw new \RuntimeException('The openssl extension is required for SSL/TLS protection but is not available. ' diff --git a/src/Composer/Repository/ComposerRepository.php b/src/Composer/Repository/ComposerRepository.php index 1efc3cae4..548f06aed 100644 --- a/src/Composer/Repository/ComposerRepository.php +++ b/src/Composer/Repository/ComposerRepository.php @@ -59,7 +59,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito private $degradedMode = false; private $rootData; - public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null) + public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null) { if (!preg_match('{^[\w.]+\??://}', $repoConfig['url'])) { // assume http as the default protocol @@ -90,7 +90,7 @@ class ComposerRepository extends ArrayRepository implements ConfigurableReposito $this->io = $io; $this->cache = new Cache($io, $config->get('cache-repo-dir').'/'.preg_replace('{[^a-z0-9.]}i', '-', $this->url), 'a-z0-9.$'); $this->loader = new ArrayLoader(); - $this->rfs = Factory::createRemoteFilesystem($this->io, $this->config, $this->options); + $this->rfs = $rfs ?: Factory::createRemoteFilesystem($this->io, $this->config, $this->options); $this->eventDispatcher = $eventDispatcher; $this->repoConfig = $repoConfig; } diff --git a/src/Composer/Repository/RepositoryManager.php b/src/Composer/Repository/RepositoryManager.php index 372dc8979..42520ebd4 100644 --- a/src/Composer/Repository/RepositoryManager.php +++ b/src/Composer/Repository/RepositoryManager.php @@ -16,6 +16,7 @@ use Composer\IO\IOInterface; use Composer\Config; use Composer\EventDispatcher\EventDispatcher; use Composer\Package\PackageInterface; +use Composer\Util\RemoteFilesystem; /** * Repositories manager. @@ -32,12 +33,14 @@ class RepositoryManager private $io; private $config; private $eventDispatcher; + private $rfs; - public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null) + public function __construct(IOInterface $io, Config $config, EventDispatcher $eventDispatcher = null, RemoteFilesystem $rfs = null) { $this->io = $io; $this->config = $config; $this->eventDispatcher = $eventDispatcher; + $this->rfs = $rfs; } /** @@ -102,7 +105,7 @@ class RepositoryManager $class = $this->repositoryClasses[$type]; - return new $class($config, $this->io, $this->config, $this->eventDispatcher); + return new $class($config, $this->io, $this->config, $this->eventDispatcher, $this->rfs); } /** diff --git a/tests/Composer/Test/Mock/FactoryMock.php b/tests/Composer/Test/Mock/FactoryMock.php index cb4c5ee25..43d41c5a8 100644 --- a/tests/Composer/Test/Mock/FactoryMock.php +++ b/tests/Composer/Test/Mock/FactoryMock.php @@ -33,7 +33,7 @@ class FactoryMock extends Factory return $config; } - protected function addLocalRepository(RepositoryManager $rm, $vendorDir) + protected function addLocalRepository(IOInterface $io, RepositoryManager $rm, $vendorDir) { }