From 5eb333680b027d3d86f9230e78800b21831290cb Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Tue, 21 Feb 2012 14:02:08 +0100 Subject: [PATCH] Rename findPackagesByName to findPackages and allow version arg --- src/Composer/Command/ShowCommand.php | 4 +- src/Composer/Repository/ArrayRepository.php | 11 ++++- .../Repository/CompositeRepository.php | 4 +- .../Repository/RepositoryInterface.php | 7 +-- src/Composer/Repository/RepositoryManager.php | 19 ++++++++ .../Test/Repository/ArrayRepositoryTest.php | 6 +-- .../Repository/CompositeRepositoryTest.php | 48 +++++++++---------- 7 files changed, 63 insertions(+), 36 deletions(-) diff --git a/src/Composer/Command/ShowCommand.php b/src/Composer/Command/ShowCommand.php index 4450966b3..e376dad32 100644 --- a/src/Composer/Command/ShowCommand.php +++ b/src/Composer/Command/ShowCommand.php @@ -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(); } diff --git a/src/Composer/Repository/ArrayRepository.php b/src/Composer/Repository/ArrayRepository.php index 36ed56b0b..a46bfd20d 100644 --- a/src/Composer/Repository/ArrayRepository.php +++ b/src/Composer/Repository/ArrayRepository.php @@ -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; } } diff --git a/src/Composer/Repository/CompositeRepository.php b/src/Composer/Repository/CompositeRepository.php index eccf5f230..e000d97e8 100644 --- a/src/Composer/Repository/CompositeRepository.php +++ b/src/Composer/Repository/CompositeRepository.php @@ -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); } diff --git a/src/Composer/Repository/RepositoryInterface.php b/src/Composer/Repository/RepositoryInterface.php index 2c1f7d43c..e4a79695d 100644 --- a/src/Composer/Repository/RepositoryInterface.php +++ b/src/Composer/Repository/RepositoryInterface.php @@ -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. diff --git a/src/Composer/Repository/RepositoryManager.php b/src/Composer/Repository/RepositoryManager.php index 1ed0f18d0..4935ef68c 100644 --- a/src/Composer/Repository/RepositoryManager.php +++ b/src/Composer/Repository/RepositoryManager.php @@ -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 * diff --git a/tests/Composer/Test/Repository/ArrayRepositoryTest.php b/tests/Composer/Test/Repository/ArrayRepositoryTest.php index 5e29f5ed6..ed05819b6 100644 --- a/tests/Composer/Test/Repository/ArrayRepositoryTest.php +++ b/tests/Composer/Test/Repository/ArrayRepositoryTest.php @@ -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()); } diff --git a/tests/Composer/Test/Repository/CompositeRepositoryTest.php b/tests/Composer/Test/Repository/CompositeRepositoryTest.php index 49e017d18..c71196729 100644 --- a/tests/Composer/Test/Repository/CompositeRepositoryTest.php +++ b/tests/Composer/Test/Repository/CompositeRepositoryTest.php @@ -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)"); } }