Browse Source

Removed duplication of logic of an installation manager in a plugin manager

Martin Hasoň 11 years ago
parent
commit
e949038c0f

+ 6 - 21
src/Composer/Plugin/PluginManager.php

@@ -232,28 +232,13 @@ class PluginManager
      */
     public function getInstallPath(PackageInterface $package, $global = false)
     {
-        $targetDir = $package->getTargetDir();
+        if (!$global) {
+            return $this->composer->getInstallationManager()->getInstallPath($package);
+        }
 
-        return $this->getPackageBasePath($package, $global) . ($targetDir ? '/'.$targetDir : '');
-    }
+        $targetDir = $package->getTargetDir();
+        $vendorDir = $this->composer->getConfig()->get('home').'/vendor';
 
-    /**
-     * Retrieves the base path a package gets installed into.
-     *
-     * Does not take targetDir into account.
-     *
-     * @param PackageInterface $package
-     * @param bool             $global  Whether this is a global package
-     *
-     * @return string Base path
-     */
-    protected function getPackageBasePath(PackageInterface $package, $global = false)
-    {
-        if ($global) {
-            $vendorDir = $this->composer->getConfig()->get('home').'/vendor';
-        } else {
-            $vendorDir = rtrim($this->composer->getConfig()->get('vendor-dir'), '/');
-        }
-        return ($vendorDir ? $vendorDir.'/' : '') . $package->getPrettyName();
+        return ($vendorDir ? $vendorDir.'/' : '').$package->getPrettyName().($targetDir ? '/'.$targetDir : '');
     }
 }

+ 8 - 0
tests/Composer/Test/Plugin/PluginInstallerTest.php

@@ -52,6 +52,13 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase
             ->method('getLocalRepository')
             ->will($this->returnValue($this->repository));
 
+        $im = $this->getMock('Composer\Installer\InstallationManager');
+        $im->expects($this->any())
+            ->method('getInstallPath')
+            ->will($this->returnCallback(function ($package) {
+                return __DIR__.'/Fixtures/'.$package->getPrettyName();
+            }));
+
         $this->io = $this->getMock('Composer\IO\IOInterface');
 
         $dispatcher = $this->getMockBuilder('Composer\EventDispatcher\EventDispatcher')->disableOriginalConstructor()->getMock();
@@ -62,6 +69,7 @@ class PluginInstallerTest extends \PHPUnit_Framework_TestCase
         $this->composer->setConfig($config);
         $this->composer->setDownloadManager($dm);
         $this->composer->setRepositoryManager($rm);
+        $this->composer->setInstallationManager($im);
         $this->composer->setAutoloadGenerator($this->autoloadGenerator);
 
         $this->pm = new PluginManager($this->composer, $this->io);