diff --git a/src/Composer/Installer/Registry/FilesystemRegistry.php b/src/Composer/Installer/Registry/FilesystemRegistry.php deleted file mode 100644 index 755e034b2..000000000 --- a/src/Composer/Installer/Registry/FilesystemRegistry.php +++ /dev/null @@ -1,126 +0,0 @@ - - * 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 deleted file mode 100644 index 7fdf63f8a..000000000 --- a/src/Composer/Installer/Registry/RegistryInterface.php +++ /dev/null @@ -1,67 +0,0 @@ - - * 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/src/Composer/Repository/FilesystemRepository.php b/src/Composer/Repository/FilesystemRepository.php new file mode 100644 index 000000000..e5de4e5cb --- /dev/null +++ b/src/Composer/Repository/FilesystemRepository.php @@ -0,0 +1,82 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Repository; + +use Composer\Package\PackageInterface; +use Composer\Package\Loader\ArrayLoader; +use Composer\Package\Dumper\ArrayDumper; + +/** + * Filesystem repository. + * + * @author Konstantin Kudryashov + */ +class FilesystemRepository extends ArrayRepository implements WritableRepositoryInterface +{ + private $file; + + /** + * Initializes filesystem repository. + * + * @param string $group registry (installer) group + */ + public function __construct($repositoryFile) + { + $this->file = $repositoryFile; + $path = dirname($this->file); + + if (!is_dir($path)) { + if (file_exists($path)) { + throw new \UnexpectedValueException( + $path.' exists and is not a directory.' + ); + } + if (!mkdir($path, 0777, true)) { + throw new \UnexpectedValueException( + $path.' does not exist and could not be created.' + ); + } + } + } + + /** + * Initializes repository (reads file, or remote address). + */ + protected function initialize() + { + parent::initialize(); + + $packages = @json_decode(file_get_contents($this->file), true); + + if (is_array($packages)) { + $loader = new ArrayLoader(); + foreach ($packages as $package) { + $this->addPackage($loader->load($package)); + } + } + } + + /** + * Writes writable repository. + */ + public function write() + { + $packages = array(); + $dumper = new ArrayDumper(); + foreach ($this->getPackages() as $package) { + $packages[] = $dumper->dump($package); + } + + file_put_contents($this->file, json_encode($packages)); + } +} diff --git a/tests/Composer/Test/Installer/Registry/FilesystemRegistryTest.php b/tests/Composer/Test/Installer/Registry/FilesystemRegistryTest.php deleted file mode 100644 index d3aaf7fc6..000000000 --- a/tests/Composer/Test/Installer/Registry/FilesystemRegistryTest.php +++ /dev/null @@ -1,156 +0,0 @@ - - * 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(); - } -} diff --git a/tests/Composer/Test/Repository/FilesystemRepositoryTest.php b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php new file mode 100644 index 000000000..ee777b52f --- /dev/null +++ b/tests/Composer/Test/Repository/FilesystemRepositoryTest.php @@ -0,0 +1,55 @@ + + * Jordi Boggiano + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Composer\Repository; + +use Composer\Repository\FilesystemRepository; + +class FilesystemRepositoryTest extends \PHPUnit_Framework_TestCase +{ + private $dir; + private $repositoryFile; + + protected function setUp() + { + $this->dir = sys_get_temp_dir().'/.composer'; + $this->repositoryFile = $this->dir.'/some_registry-reg.json'; + + if (file_exists($this->repositoryFile)) { + unlink($this->repositoryFile); + } + } + + public function testRepositoryReadWrite() + { + $this->assertFileNotExists($this->repositoryFile); + $repository = new FilesystemRepository($this->repositoryFile); + + $repository->getPackages(); + $repository->write(); + $this->assertFileExists($this->repositoryFile); + + file_put_contents($this->repositoryFile, json_encode(array( + array('name' => 'package1', 'version' => '1.0.0-beta', 'type' => 'vendor') + ))); + + $repository = new FilesystemRepository($this->repositoryFile); + $repository->getPackages(); + $repository->write(); + $this->assertFileExists($this->repositoryFile); + + $data = json_decode(file_get_contents($this->repositoryFile), true); + $this->assertEquals(array( + array('name' => 'package1', 'type' => 'vendor', 'version' => '1.0.0', 'releaseType' => 'beta', 'names' => array('package1')) + ), $data); + } +}