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

@ -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'));
@ -66,15 +66,15 @@ class CompositeRepositoryTest extends TestCase
$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'");
}