Installers FilesystemRegistry implemented
parent
067007656b
commit
45cab9fe8c
|
@ -0,0 +1,126 @@
|
||||||
|
<?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]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
<?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,156 @@
|
||||||
|
<?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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue