Browse Source

Cache VCS driver after creation to avoid initializing it several times

Jordi Boggiano 8 years ago
parent
commit
d11eff27d0
1 changed files with 15 additions and 12 deletions
  1. 15 12
      src/Composer/Repository/VcsRepository.php

+ 15 - 12
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;
             }
         }
     }