From 45cab9fe8c8c16121f74a077864417ba538b922a Mon Sep 17 00:00:00 2001 From: everzet Date: Sat, 24 Sep 2011 00:24:19 +0300 Subject: [PATCH] Installers FilesystemRegistry implemented --- .../Installer/Registry/FilesystemRegistry.php | 126 ++++++++++++++ .../Installer/Registry/RegistryInterface.php | 67 ++++++++ .../Registry/FilesystemRegistryTests.php | 156 ++++++++++++++++++ 3 files changed, 349 insertions(+) create mode 100644 src/Composer/Installer/Registry/FilesystemRegistry.php create mode 100644 src/Composer/Installer/Registry/RegistryInterface.php create mode 100644 tests/Composer/Test/Installer/Registry/FilesystemRegistryTests.php diff --git a/src/Composer/Installer/Registry/FilesystemRegistry.php b/src/Composer/Installer/Registry/FilesystemRegistry.php new file mode 100644 index 000000000..755e034b2 --- /dev/null +++ b/src/Composer/Installer/Registry/FilesystemRegistry.php @@ -0,0 +1,126 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Installer\Registry; + +use Composer\Package\PackageInterface; + +/** + * Filesystem registry. + * + * @author Konstantin Kudryashov + */ +class FilesystemRegistry implements RegistryInterface +{ + private $registryFile; + private $registry = array(); + + /** + * Initializes filesystem registry. + * + * @param string $group registry (installer) group + */ + public function __construct($composerCachePath, $group) + { + $this->registryFile = rtrim($composerCachePath, '/').'/'.$group.'-reg.json'; + $registryPath = dirname($this->registryFile); + + if (!is_dir($registryPath)) { + if (file_exists($registryPath)) { + throw new \UnexpectedValueException( + $registryPath.' exists and is not a directory.' + ); + } + if (!mkdir($registryPath, 0777, true)) { + throw new \UnexpectedValueException( + $registryPath.' does not exist and could not be created.' + ); + } + } + } + + /** + * Opens registry (read file / opens connection). + */ + public function open() + { + if (is_file($this->registryFile)) { + $this->registry = json_decode(file_get_contents($this->registryFile), true); + } + } + + /** + * Closes registry (writes file / closes connection). + */ + public function close() + { + file_put_contents($this->registryFile, json_encode($this->registry)); + } + + /** + * Checks if specified package registered (installed). + * + * @param PackageInterface $package package instance + * + * @return Boolean + */ + public function isPackageRegistered(PackageInterface $package) + { + $packageId = $package->getUniqueName(); + + return isset($this->registry[$packageId]); + } + + /** + * Returns installer type for the registered package. + * + * @param PackageInterface $package package instance + * + * @return string + */ + public function getRegisteredPackageInstallerType(PackageInterface $package) + { + $packageId = $package->getUniqueName(); + + if (isset($this->registry[$packageId])) { + return $this->registry[$packageId]; + } + } + + /** + * Registers package in registry. + * + * @param PackageInterface $package package instance + * @param string $type installer type with which package were been + * installed + */ + public function registerPackage(PackageInterface $package, $type) + { + $packageId = $package->getUniqueName(); + + $this->registry[$packageId] = $type; + } + + /** + * Removes package from registry. + * + * @param PackageInterface $package package instance + */ + public function unregisterPackage(PackageInterface $package) + { + $packageId = $package->getUniqueName(); + + if (isset($this->registry[$packageId])) { + unset($this->registry[$packageId]); + } + } +} diff --git a/src/Composer/Installer/Registry/RegistryInterface.php b/src/Composer/Installer/Registry/RegistryInterface.php new file mode 100644 index 000000000..7fdf63f8a --- /dev/null +++ b/src/Composer/Installer/Registry/RegistryInterface.php @@ -0,0 +1,67 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Installer\Registry; + +use Composer\Package\PackageInterface; + +/** + * Installer registry interface. + * + * @author Konstantin Kudryashov + */ +interface RegistryInterface +{ + /** + * Opens registry (read file / opens connection). + */ + function open(); + + /** + * Closes registry (writes file / closes connection). + */ + function close(); + + /** + * Checks if specified package registered (installed). + * + * @param PackageInterface $package package instance + * + * @return Boolean + */ + function isPackageRegistered(PackageInterface $package); + + /** + * Returns installer type for the registered package. + * + * @param PackageInterface $package package instance + * + * @return string + */ + function getRegisteredPackageInstallerType(PackageInterface $package); + + /** + * Registers package in registry. + * + * @param PackageInterface $package package instance + * @param string $type installer type with which package were been + * installed + */ + function registerPackage(PackageInterface $package, $type); + + /** + * Removes package from registry. + * + * @param PackageInterface $package package instance + */ + function unregisterPackage(PackageInterface $package); +} diff --git a/tests/Composer/Test/Installer/Registry/FilesystemRegistryTests.php b/tests/Composer/Test/Installer/Registry/FilesystemRegistryTests.php new file mode 100644 index 000000000..d3aaf7fc6 --- /dev/null +++ b/tests/Composer/Test/Installer/Registry/FilesystemRegistryTests.php @@ -0,0 +1,156 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Test\Installer\Registry; + +use Composer\Installer\Registry\FilesystemRegistry; + +class FilesystemRegistryTest extends \PHPUnit_Framework_TestCase +{ + private $dir; + private $registryFile; + + protected function setUp() + { + $this->dir = sys_get_temp_dir().'/.composer'; + $this->registryFile = $this->dir.'/some_registry-reg.json'; + + if (file_exists($this->registryFile)) { + unlink($this->registryFile); + } + } + + public function testRegistryCreation() + { + $this->assertFileNotExists($this->registryFile); + $registry = new FilesystemRegistry($this->dir, 'some_registry'); + + $registry->open(); + $registry->close(); + $this->assertFileExists($this->registryFile); + + file_put_contents($this->registryFile, json_encode(array( + 'package1-1.0.0-beta' => 'library' + ))); + + $registry->open(); + $registry->close(); + $this->assertFileExists($this->registryFile); + + $data = json_decode(file_get_contents($this->registryFile), true); + $this->assertEquals(array('package1-1.0.0-beta' => 'library'), $data); + } + + public function testIsPackageRegistered() + { + file_put_contents($this->registryFile, json_encode(array( + 'package1-1.0.0-beta' => 'library' + ))); + + $registry = new FilesystemRegistry($this->dir, 'some_registry'); + $registry->open(); + + $package1 = $this->createPackageMock(); + $package1 + ->expects($this->once()) + ->method('getUniqueName') + ->will($this->returnValue('package1-1.0.0-beta')); + $package2 = $this->createPackageMock(); + $package2 + ->expects($this->once()) + ->method('getUniqueName') + ->will($this->returnValue('package2-1.1.0-stable')); + + $this->assertTrue($registry->isPackageRegistered($package1)); + $this->assertFalse($registry->isPackageRegistered($package2)); + + $registry->close(); + } + + public function testGetRegisteredPackageInstallerType() + { + $package1 = $this->createPackageMock(); + $package1 + ->expects($this->once()) + ->method('getUniqueName') + ->will($this->returnValue('package1-1.0.0-beta')); + $package2 = $this->createPackageMock(); + $package2 + ->expects($this->once()) + ->method('getUniqueName') + ->will($this->returnValue('package2-1.1.0-stable')); + + file_put_contents($this->registryFile, json_encode(array( + 'package1-1.0.0-beta' => 'library', + 'package2-1.1.0-stable' => 'bundle' + ))); + + $registry = new FilesystemRegistry($this->dir, 'some_registry'); + $registry->open(); + + $this->assertSame('library', $registry->getRegisteredPackageInstallerType($package1)); + $this->assertSame('bundle', $registry->getRegisteredPackageInstallerType($package2)); + + $registry->close(); + } + + public function testRegisterPackage() + { + $package = $this->createPackageMock(); + $package + ->expects($this->once()) + ->method('getUniqueName') + ->will($this->returnValue('package1-1.0.0-beta')); + + $registry = new FilesystemRegistry($this->dir, 'some_registry'); + $registry->open(); + + $registry->registerPackage($package, 'library'); + + $registry->close(); + + $data = json_decode(file_get_contents($this->registryFile), true); + + $this->assertEquals(array('package1-1.0.0-beta' => 'library'), $data); + } + + public function testUnregisterPackage() + { + $package = $this->createPackageMock(); + $package + ->expects($this->once()) + ->method('getUniqueName') + ->will($this->returnValue('package1-1.0.0-beta')); + + file_put_contents($this->registryFile, json_encode(array( + 'package1-1.0.0-beta' => 'library', + 'package2-1.1.0-stable' => 'bundle' + ))); + + $registry = new FilesystemRegistry($this->dir, 'some_registry'); + $registry->open(); + + $registry->unregisterPackage($package); + + $registry->close(); + + $data = json_decode(file_get_contents($this->registryFile), true); + + $this->assertEquals(array('package2-1.1.0-stable' => 'bundle'), $data); + } + + private function createPackageMock() + { + return $this->getMockBuilder('Composer\Package\PackageInterface') + ->getMock(); + } +}