Refactored FilesystemRegistry to FilesystemRepository

main
everzet 13 years ago
parent 5c841187fd
commit 20318f77a0

@ -1,126 +0,0 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* 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 <ever.zet@gmail.com>
*/
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]);
}
}
}

@ -1,67 +0,0 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* 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 <ever.zet@gmail.com>
*/
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);
}

@ -0,0 +1,82 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* 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 <ever.zet@gmail.com>
*/
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));
}
}

@ -1,156 +0,0 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* 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();
}
}

@ -0,0 +1,55 @@
<?php
/*
* This file is part of Composer.
*
* (c) Nils Adermann <naderman@naderman.de>
* Jordi Boggiano <j.boggiano@seld.be>
*
* 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);
}
}
Loading…
Cancel
Save