From cc2f2b79cac840cfa2bd900a4e1f3585955dc987 Mon Sep 17 00:00:00 2001 From: everzet Date: Sat, 1 Oct 2011 15:33:25 +0300 Subject: [PATCH] added findPackage() method to the RepositoryManager and RepositoryInterface --- src/Composer/Repository/ArrayRepository.php | 17 +++++++++++++++++ src/Composer/Repository/RepositoryInterface.php | 10 ++++++++++ src/Composer/Repository/RepositoryManager.php | 17 +++++++++++++++++ 3 files changed, 44 insertions(+) diff --git a/src/Composer/Repository/ArrayRepository.php b/src/Composer/Repository/ArrayRepository.php index fcffc72af..153f87caa 100644 --- a/src/Composer/Repository/ArrayRepository.php +++ b/src/Composer/Repository/ArrayRepository.php @@ -23,6 +23,23 @@ class ArrayRepository implements RepositoryInterface { protected $packages; + /** + * Searches for a package by it's name and version (if has one). + * + * @param string $name package name + * @param string $version package version + * + * @return PackageInterface|null + */ + public function findPackage($name, $version) + { + foreach ($this->getPackages() as $package) { + if ($name === $package->getName() && $version === $package->getVersion()) { + return $package; + } + } + } + /** * Checks if specified package in this repository. * diff --git a/src/Composer/Repository/RepositoryInterface.php b/src/Composer/Repository/RepositoryInterface.php index 96c06656f..a94ecea4c 100644 --- a/src/Composer/Repository/RepositoryInterface.php +++ b/src/Composer/Repository/RepositoryInterface.php @@ -31,6 +31,16 @@ interface RepositoryInterface extends \Countable */ function hasPackage(PackageInterface $package); + /** + * Searches for a package by it's name and version (if has one). + * + * @param string $name package name + * @param string $version package version + * + * @return PackageInterface|null + */ + function findPackage($name, $version); + /** * Returns list of registered packages. * diff --git a/src/Composer/Repository/RepositoryManager.php b/src/Composer/Repository/RepositoryManager.php index 7d9022b2f..4535d4945 100644 --- a/src/Composer/Repository/RepositoryManager.php +++ b/src/Composer/Repository/RepositoryManager.php @@ -22,6 +22,23 @@ class RepositoryManager private $localRepository; private $repositories = array(); + /** + * Searches for a package by it's name and version in managed repositories. + * + * @param string $name package name + * @param string $version package version + * + * @return PackageInterface|null + */ + public function findPackage($name, $version) + { + foreach ($this->repositories as $repository) { + if ($package = $repository->findPackage($name, $version)) { + return $package; + } + } + } + /** * Sets repository with specific name. *