Browse Source

Refactored LibraryInstaller to use WritableRepository instead of Registry

everzet 13 years ago
parent
commit
6133108710

+ 25 - 35
src/Composer/Installer/LibraryInstaller.php

@@ -13,8 +13,7 @@
 namespace Composer\Installer;
 
 use Composer\Downloader\DownloadManager;
-use Composer\Installer\Registry\RegistryInterface;
-use Composer\Installer\Registry\FilesystemRegistry;
+use Composer\Repository\WritableRepositoryInterface;
 use Composer\DependencyResolver\Operation\OperationInterface;
 use Composer\Package\PackageInterface;
 
@@ -28,16 +27,16 @@ class LibraryInstaller implements InstallerInterface
 {
     private $dir;
     private $dm;
-    private $registry;
+    private $repository;
 
     /**
      * Initializes library installer.
      *
-     * @param   string              $dir        relative path for packages home
-     * @param   DownloadManager     $dm         download manager
-     * @param   RegistryInterface   $registry   registry controller
+     * @param   string                      $dir        relative path for packages home
+     * @param   DownloadManager             $dm         download manager
+     * @param   WritableRepositoryInterface $repository repository controller
      */
-    public function __construct($dir, DownloadManager $dm, RegistryInterface $registry = null)
+    public function __construct($dir, DownloadManager $dm, WritableRepositoryInterface $repository)
     {
         $this->dir = $dir;
         $this->dm  = $dm;
@@ -55,20 +54,7 @@ class LibraryInstaller implements InstallerInterface
             }
         }
 
-        if (null === $registry) {
-            $registry = new FilesystemRegistry('.composer', str_replace('/', '_', $dir));
-        }
-
-        $this->registry = $registry;
-        $this->registry->open();
-    }
-
-    /**
-     * Closes packages registry.
-     */
-    public function __destruct()
-    {
-        $this->registry->close();
+        $this->repository = $repository;
     }
 
     /**
@@ -96,7 +82,7 @@ class LibraryInstaller implements InstallerInterface
      */
     public function isInstalled(PackageInterface $package)
     {
-        return $this->registry->isPackageRegistered($package);
+        return $this->repository->hasPackage($package);
     }
 
     /**
@@ -108,8 +94,10 @@ class LibraryInstaller implements InstallerInterface
      */
     public function install(PackageInterface $package)
     {
-        $type = $this->dm->download($package, $this->dir.'/'.$package->getName());
-        $this->registry->registerPackage($package, $type);
+        $downloadPath = $this->dir.DIRECTORY_SEPARATOR.$package->getName();
+
+        $this->dm->download($package, $downloadPath);
+        $this->repository->addPackage($package);
     }
 
     /**
@@ -122,14 +110,15 @@ class LibraryInstaller implements InstallerInterface
      */
     public function update(PackageInterface $initial, PackageInterface $target)
     {
-        if (!$this->registry->isPackageRegistered($initial)) {
-            throw new \UnexpectedValueException('Package is not installed: '.$initial);
+        if (!$this->repository->hasPackage($initial)) {
+            throw new \InvalidArgumentException('Package is not installed: '.$initial);
         }
 
-        $type = $this->registry->getRegisteredPackageInstallerType($initial);
-        $this->dm->update($initial, $target, $this->dir.'/'.$initial->getName(), $type);
-        $this->registry->unregisterPackage($initial);
-        $this->registry->registerPackage($target, $type);
+        $downloadPath = $this->dir.DIRECTORY_SEPARATOR.$initial->getName();
+
+        $this->dm->update($initial, $target, $downloadPath);
+        $this->repository->removePackage($initial);
+        $this->repository->addPackage($target);
     }
 
     /**
@@ -141,12 +130,13 @@ class LibraryInstaller implements InstallerInterface
      */
     public function uninstall(PackageInterface $package)
     {
-        if (!$this->registry->isPackageRegistered($package)) {
-            throw new \UnexpectedValueException('Package is not installed: '.$package);
+        if (!$this->repository->hasPackage($package)) {
+            throw new \InvalidArgumentException('Package is not installed: '.$package);
         }
 
-        $type = $this->registry->getRegisteredPackageInstallerType($package);
-        $this->dm->remove($package, $this->dir.'/'.$package->getName(), $type);
-        $this->registry->unregisterPackage($package);
+        $downloadPath = $this->dir.DIRECTORY_SEPARATOR.$package->getName();
+
+        $this->dm->remove($package, $downloadPath);
+        $this->repository->removePackage($package);
     }
 }

+ 30 - 51
tests/Composer/Test/Installer/LibraryInstallerTest.php

@@ -19,7 +19,7 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
 {
     private $dir;
     private $dm;
-    private $registry;
+    private $repository;
     private $library;
 
     protected function setUp()
@@ -33,35 +33,27 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
             ->disableOriginalConstructor()
             ->getMock();
 
-        $this->registry = $this->getMockBuilder('Composer\Installer\Registry\RegistryInterface')
+        $this->repository = $this->getMockBuilder('Composer\Repository\WritableRepositoryInterface')
             ->disableOriginalConstructor()
             ->getMock();
     }
 
     public function testInstallerCreation()
     {
-        $this->registry
-            ->expects($this->once())
-            ->method('open');
-
-        $this->registry
-            ->expects($this->once())
-            ->method('close');
-
-        $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
+        $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
         $this->assertTrue(is_dir($this->dir));
 
         $file = sys_get_temp_dir().'/file';
         touch($file);
 
         $this->setExpectedException('UnexpectedValueException');
-        $library = new LibraryInstaller($file, $this->dm, $this->registry);
+        $library = new LibraryInstaller($file, $this->dm, $this->repository);
     }
 
     public function testExecuteOperation()
     {
         $library = $this->getMockBuilder('Composer\Installer\LibraryInstaller')
-            ->setConstructorArgs(array($this->dir, $this->dm, $this->registry))
+            ->setConstructorArgs(array($this->dir, $this->dm, $this->repository))
             ->setMethods(array('install', 'update', 'uninstall'))
             ->getMock();
 
@@ -92,12 +84,12 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
 
     public function testIsInstalled()
     {
-        $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
+        $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
         $package = $this->createPackageMock();
 
-        $this->registry
+        $this->repository
             ->expects($this->exactly(2))
-            ->method('isPackageRegistered')
+            ->method('hasPackage')
             ->with($package)
             ->will($this->onConsecutiveCalls(true, false));
 
@@ -107,7 +99,7 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
 
     public function testInstall()
     {
-        $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
+        $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
         $package = $this->createPackageMock();
 
         $package
@@ -118,20 +110,19 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
         $this->dm
             ->expects($this->once())
             ->method('download')
-            ->with($package, $this->dir.'/some/package')
-            ->will($this->returnValue('source'));
+            ->with($package, $this->dir.'/some/package');
 
-        $this->registry
+        $this->repository
             ->expects($this->once())
-            ->method('registerPackage')
-            ->with($package, 'source');
+            ->method('addPackage')
+            ->with($package);
 
         $library->install($package);
     }
 
     public function testUpdate()
     {
-        $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
+        $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
         $initial = $this->createPackageMock();
         $target  = $this->createPackageMock();
 
@@ -140,43 +131,37 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
             ->method('getName')
             ->will($this->returnValue('package1'));
 
-        $this->registry
+        $this->repository
             ->expects($this->exactly(2))
-            ->method('isPackageRegistered')
+            ->method('hasPackage')
             ->with($initial)
             ->will($this->onConsecutiveCalls(true, false));
 
-        $this->registry
-            ->expects($this->once())
-            ->method('getRegisteredPackageInstallerType')
-            ->with($initial)
-            ->will($this->returnValue('dist'));
-
         $this->dm
             ->expects($this->once())
             ->method('update')
-            ->with($initial, $target, $this->dir.'/package1', 'dist');
+            ->with($initial, $target, $this->dir.'/package1');
 
-        $this->registry
+        $this->repository
             ->expects($this->once())
-            ->method('unregisterPackage')
+            ->method('removePackage')
             ->with($initial);
 
-        $this->registry
+        $this->repository
             ->expects($this->once())
-            ->method('registerPackage')
-            ->with($target, 'dist');
+            ->method('addPackage')
+            ->with($target);
 
         $library->update($initial, $target);
 
-        $this->setExpectedException('UnexpectedValueException');
+        $this->setExpectedException('InvalidArgumentException');
 
         $library->update($initial, $target);
     }
 
     public function testUninstall()
     {
-        $library = new LibraryInstaller($this->dir, $this->dm, $this->registry);
+        $library = new LibraryInstaller($this->dir, $this->dm, $this->repository);
         $package = $this->createPackageMock();
 
         $package
@@ -184,31 +169,25 @@ class LibraryInstallerTest extends \PHPUnit_Framework_TestCase
             ->method('getName')
             ->will($this->returnValue('pkg'));
 
-        $this->registry
+        $this->repository
             ->expects($this->exactly(2))
-            ->method('isPackageRegistered')
+            ->method('hasPackage')
             ->with($package)
             ->will($this->onConsecutiveCalls(true, false));
 
-        $this->registry
-            ->expects($this->once())
-            ->method('getRegisteredPackageInstallerType')
-            ->with($package)
-            ->will($this->returnValue('source'));
-
         $this->dm
             ->expects($this->once())
             ->method('remove')
-            ->with($package, $this->dir.'/pkg', 'source');
+            ->with($package, $this->dir.'/pkg');
 
-        $this->registry
+        $this->repository
             ->expects($this->once())
-            ->method('unregisterPackage')
+            ->method('removePackage')
             ->with($package);
 
         $library->uninstall($package);
 
-        $this->setExpectedException('UnexpectedValueException');
+        $this->setExpectedException('InvalidArgumentException');
 
         $library->uninstall($package);
     }