1
0
Fork 0

Rename findPackagesByName to findPackages and allow version arg

pull/350/head
Jordi Boggiano 2012-02-21 14:02:08 +01:00
parent afbb9cefa4
commit 5eb333680b
7 changed files with 63 additions and 36 deletions

View File

@ -118,7 +118,7 @@ EOT
// we only have a name, so search for the highest version of the given package
$highestVersion = null;
foreach ($repos->findPackagesByName($input->getArgument('package')) as $package) {
foreach ($repos->findPackages($input->getArgument('package')) as $package) {
if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) {
$highestVersion = $package;
}
@ -164,7 +164,7 @@ EOT
$versions = array();
foreach ($repos->findPackagesByName($package->getName()) as $version) {
foreach ($repos->findPackages($package->getName()) as $version) {
$versions[] = $version->getPrettyVersion();
}

View File

@ -44,14 +44,21 @@ class ArrayRepository implements RepositoryInterface
/**
* {@inheritDoc}
*/
public function findPackagesByName($name)
public function findPackages($name, $version = null)
{
// normalize name
$name = strtolower($name);
// normalize version
if (null !== $version) {
$versionParser = new VersionParser();
$version = $versionParser->normalize($version);
}
$packages = array();
foreach ($this->getPackages() as $package) {
if ($package->getName() === $name) {
if ($package->getName() === $name && (null === $version || $version === $package->getVersion())) {
$packages[] = $package;
}
}

View File

@ -68,12 +68,12 @@ class CompositeRepository implements RepositoryInterface
/**
* {@inheritdoc}
*/
public function findPackagesByName($name)
public function findPackages($name, $version = null)
{
$packages = array();
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$packages[] = $repository->findPackagesByName($name);
$packages[] = $repository->findPackages($name, $version);
}
return call_user_func_array('array_merge', $packages);
}

View File

@ -32,7 +32,7 @@ interface RepositoryInterface extends \Countable
function hasPackage(PackageInterface $package);
/**
* Searches for a package by it's name and version (if has one).
* Searches for the first match of a package by name and version.
*
* @param string $name package name
* @param string $version package version
@ -42,13 +42,14 @@ interface RepositoryInterface extends \Countable
function findPackage($name, $version);
/**
* Searches for packages by it's name.
* Searches for all packages matching a name and optionally a version.
*
* @param string $name package name
* @param string $version package version
*
* @return array
*/
function findPackagesByName($name);
function findPackages($name, $version = null);
/**
* Returns list of registered packages.

View File

@ -50,6 +50,25 @@ class RepositoryManager
}
}
/**
* Searches for all packages matching a name and optionally a version in managed repositories.
*
* @param string $name package name
* @param string $version package version
*
* @return array
*/
public function findPackages($name, $version)
{
$packages = array();
foreach ($this->repositories as $repository) {
$packages = array_merge($packages, $repository->findPackages($name, $version));
}
return $packages;
}
/**
* Adds repository
*

View File

@ -51,18 +51,18 @@ class ArrayRepositoryTest extends TestCase
$this->assertFalse($repo->hasPackage($this->getPackage('bar', '1')));
}
public function testFindPackagesByName()
public function testFindPackages()
{
$repo = new ArrayRepository();
$repo->addPackage($this->getPackage('foo', '1'));
$repo->addPackage($this->getPackage('bar', '2'));
$repo->addPackage($this->getPackage('bar', '3'));
$foo = $repo->findPackagesByName('foo');
$foo = $repo->findPackages('foo');
$this->assertCount(1, $foo);
$this->assertEquals('foo', $foo[0]->getName());
$bar = $repo->findPackagesByName('bar');
$bar = $repo->findPackages('bar');
$this->assertCount(2, $bar);
$this->assertEquals('bar', $bar[0]->getName());
}

View File

@ -22,15 +22,15 @@ class CompositeRepositoryTest extends TestCase
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$this->assertTrue($repo->hasPackage($this->getPackage('foo', '1')), "Should have package 'foo/1'");
$this->assertTrue($repo->hasPackage($this->getPackage('bar', '1')), "Should have package 'bar/1'");
$this->assertFalse($repo->hasPackage($this->getPackage('foo', '2')), "Should not have package 'foo/2'");
$this->assertFalse($repo->hasPackage($this->getPackage('bar', '2')), "Should not have package 'bar/2'");
}
@ -39,12 +39,12 @@ class CompositeRepositoryTest extends TestCase
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$this->assertEquals('foo', $repo->findPackage('foo', '1')->getName(), "Should find package 'foo/1' and get name of 'foo'");
$this->assertEquals('1', $repo->findPackage('foo', '1')->getPrettyVersion(), "Should find package 'foo/1' and get pretty version of '1'");
$this->assertEquals('bar', $repo->findPackage('bar', '1')->getName(), "Should find package 'bar/1' and get name of 'bar'");
@ -52,7 +52,7 @@ class CompositeRepositoryTest extends TestCase
$this->assertNull($repo->findPackage('foo', '2'), "Should not find package 'foo/2'");
}
public function testFindPackagesByName()
public function testFindPackages()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
@ -63,32 +63,32 @@ class CompositeRepositoryTest extends TestCase
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
$arrayRepoTwo->addPackage($this->getPackage('foo', '3'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$bats = $repo->findPackagesByName('bat');
$bats = $repo->findPackages('bat');
$this->assertCount(1, $bats, "Should find one instance of 'bats' (defined in just one repository)");
$this->assertEquals('bat', $bats[0]->getName(), "Should find packages named 'bat'");
$bars = $repo->findPackagesByName('bar');
$bars = $repo->findPackages('bar');
$this->assertCount(2, $bars, "Should find two instances of 'bar' (both defined in the same repository)");
$this->assertEquals('bar', $bars[0]->getName(), "Should find packages named 'bar'");
$foos = $repo->findPackagesByName('foo');
$foos = $repo->findPackages('foo');
$this->assertCount(3, $foos, "Should find three instances of 'foo' (two defined in one repository, the third in the other)");
$this->assertEquals('foo', $foos[0]->getName(), "Should find packages named 'foo'");
}
public function testGetPackages()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$packages = $repo->getPackages();
$this->assertCount(2, $packages, "Should get two packages");
$this->assertEquals("foo", $packages[0]->getName(), "First package should have name of 'foo'");
@ -96,17 +96,17 @@ class CompositeRepositoryTest extends TestCase
$this->assertEquals("bar", $packages[1]->getName(), "Second package should have name of 'bar'");
$this->assertEquals("1", $packages[1]->getPrettyVersion(), "Second package should have pretty version of '1'");
}
public function testAddRepository()
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$arrayRepoTwo->addPackage($this->getPackage('bar', '2'));
$arrayRepoTwo->addPackage($this->getPackage('bar', '3'));
$repo = new CompositeRepository(array($arrayRepoOne));
$this->assertCount(1, $repo, "Composite repository should have just one package before addRepository() is called");
$repo->addRepository($arrayRepoTwo);
@ -117,12 +117,12 @@ class CompositeRepositoryTest extends TestCase
{
$arrayRepoOne = new ArrayRepository;
$arrayRepoOne->addPackage($this->getPackage('foo', '1'));
$arrayRepoTwo = new ArrayRepository;
$arrayRepoTwo->addPackage($this->getPackage('bar', '1'));
$repo = new CompositeRepository(array($arrayRepoOne, $arrayRepoTwo));
$this->assertEquals(2, count($repo), "Should return '2' for count(\$repo)");
}
}