From d11eff27d07dcbbbfe3a6ae2fa4fd5e24f7ed807 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 9 May 2016 21:45:46 +0100 Subject: [PATCH] Cache VCS driver after creation to avoid initializing it several times --- src/Composer/Repository/VcsRepository.php | 27 +++++++++++++---------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/Composer/Repository/VcsRepository.php b/src/Composer/Repository/VcsRepository.php index 765e73f4f..0a7ac10a9 100644 --- a/src/Composer/Repository/VcsRepository.php +++ b/src/Composer/Repository/VcsRepository.php @@ -39,6 +39,8 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt protected $repoConfig; protected $branchErrorOccurred = false; private $drivers; + /** @var VcsDriverInterface */ + private $driver; public function __construct(array $repoConfig, IOInterface $io, Config $config, EventDispatcher $dispatcher = null, array $drivers = null) { @@ -75,32 +77,33 @@ class VcsRepository extends ArrayRepository implements ConfigurableRepositoryInt public function getDriver() { + if ($this->driver) { + return $this->driver; + } + if (isset($this->drivers[$this->type])) { $class = $this->drivers[$this->type]; - $driver = new $class($this->repoConfig, $this->io, $this->config); - /** @var VcsDriverInterface $driver */ - $driver->initialize(); + $this->driver = new $class($this->repoConfig, $this->io, $this->config); + $this->driver->initialize(); - return $driver; + return $this->driver; } foreach ($this->drivers as $driver) { if ($driver::supports($this->io, $this->config, $this->url)) { - $driver = new $driver($this->repoConfig, $this->io, $this->config); - /** @var VcsDriverInterface $driver */ - $driver->initialize(); + $this->driver = new $driver($this->repoConfig, $this->io, $this->config); + $this->driver->initialize(); - return $driver; + return $this->driver; } } foreach ($this->drivers as $driver) { if ($driver::supports($this->io, $this->config, $this->url, true)) { - $driver = new $driver($this->repoConfig, $this->io, $this->config); - /** @var VcsDriverInterface $driver */ - $driver->initialize(); + $this->driver = new $driver($this->repoConfig, $this->io, $this->config); + $this->driver->initialize(); - return $driver; + return $this->driver; } } }