From 6dbec8718d48bbf31b99ef91c7a01c349fc8f606 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 17 Apr 2011 22:59:23 +0200 Subject: [PATCH] Remove contains() from RepositoryInterface --- .../DependencyResolver/ArrayRepository.php | 19 ++++++------------- .../RepositoryInterface.php | 1 - src/Composer/DependencyResolver/Solver.php | 16 ++++++++-------- src/Composer/Package/BasePackage.php | 15 +++++++++++++++ src/Composer/Package/PackageInterface.php | 15 +++++++++++++++ 5 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/Composer/DependencyResolver/ArrayRepository.php b/src/Composer/DependencyResolver/ArrayRepository.php index 7b662b77d..716fb27ed 100644 --- a/src/Composer/DependencyResolver/ArrayRepository.php +++ b/src/Composer/DependencyResolver/ArrayRepository.php @@ -12,6 +12,8 @@ namespace Composer\DependencyResolver; +use Composer\Package\PackageInterface; + /** * A repository implementation that simply stores packages in an array * @@ -24,11 +26,12 @@ class ArrayRepository implements RepositoryInterface /** * Adds a new package to the repository * - * @param Package $package + * @param PackageInterface $package */ - public function addPackage(Package $package) + public function addPackage(PackageInterface $package) { - $this->packages[$package->getId()] = $package; + $package->setRepository($this); + $this->packages[] = $package; } /** @@ -41,16 +44,6 @@ class ArrayRepository implements RepositoryInterface return $this->packages; } - /** - * Checks if a package is contained in this repository - * - * @return bool - */ - public function contains(Package $package) - { - return isset($this->packages[$package->getId()]); - } - /** * Returns the number of packages in this repository * diff --git a/src/Composer/DependencyResolver/RepositoryInterface.php b/src/Composer/DependencyResolver/RepositoryInterface.php index 10f9c2c3b..5a054a1a8 100644 --- a/src/Composer/DependencyResolver/RepositoryInterface.php +++ b/src/Composer/DependencyResolver/RepositoryInterface.php @@ -18,5 +18,4 @@ namespace Composer\DependencyResolver; interface RepositoryInterface extends \Countable { function getPackages(); - function contains(Package $package); } diff --git a/src/Composer/DependencyResolver/Solver.php b/src/Composer/DependencyResolver/Solver.php index fd3d6948f..b10cfa16e 100644 --- a/src/Composer/DependencyResolver/Solver.php +++ b/src/Composer/DependencyResolver/Solver.php @@ -255,7 +255,7 @@ class Solver $this->addedMap[$package->getId()] = true; $dontFix = 0; - if ($this->installed->contains($package) && !isset($this->fixMap[$package->getId()])) { + if ($this->installed === $package->getRepository() && !isset($this->fixMap[$package->getId()])) { $dontFix = 1; } @@ -274,7 +274,7 @@ class Solver if ($dontFix) { $foundInstalled = false; foreach ($possibleRequires as $require) { - if ($this->installed->contains($require)) { + if ($this->installed === $require->getRepository()) { $foundInstalled = true; break; } @@ -297,7 +297,7 @@ class Solver $possibleConflicts = $this->pool->whatProvides($relation->getToPackageName(), $relation->getConstraint()); foreach ($possibleConflicts as $conflict) { - if ($dontfix && $this->installed->contains($conflict)) { + if ($dontfix && $this->installed === $conflict->getRepository()) { continue; } @@ -457,12 +457,12 @@ class Solver foreach ($job['packages'] as $package) { switch ($job['cmd']) { case 'fix': - if ($this->installed->contains($package)) { + if ($this->installed === $package->getRepository()) { $this->fixMap[$package->getId()] = true; } break; case 'update': - if ($this->installed->contains($package)) { + if ($this->installed === $package->getRepository()) { $this->updateMap[$package->getId()] = true; } break; @@ -547,7 +547,7 @@ class Solver break; case 'lock': foreach ($job['packages'] as $package) { - if ($this->installed->contains($package)) { + if ($this->installed === $package->getRepository()) { $rule = $this->createInstallRule($package, self::RULE_JOB_LOCK); } else { $rule = $this->createRemoveRule($package, self::RULE_JOB_LOCK); @@ -762,7 +762,7 @@ class Solver $minimizationsteps = 0; $installedPos = 0; - $this->installedPackages = array_values($this->installed->getPackages()); + $this->installedPackages = $this->installed->getPackages(); while (true) { @@ -798,7 +798,7 @@ class Solver if (count($this->installed) != count($this->updateMap)) { $prunedQueue = array(); foreach ($decisionQueue as $literal) { - if ($this->installed->contains($literal->getPackage())) { + if ($this->installed === $literal->getPackage()->getRepository()) { $prunedQueue[] = $literal; if (isset($this->updateMap[$literal->getPackageId()])) { $prunedQueue = $decisionQueue; diff --git a/src/Composer/Package/BasePackage.php b/src/Composer/Package/BasePackage.php index 54dbd5748..752392eb7 100644 --- a/src/Composer/Package/BasePackage.php +++ b/src/Composer/Package/BasePackage.php @@ -13,6 +13,7 @@ namespace Composer\Package; use Composer\DependencyResolver\RelationConstraint\RelationConstraintInterface; +use Composer\DependencyResolver\RepositoryInterface; /** * Base class for packages providing name storage and default match implementation @@ -22,6 +23,7 @@ use Composer\DependencyResolver\RelationConstraint\RelationConstraintInterface; abstract class BasePackage implements PackageInterface { protected $name; + protected $repository; /** * All descendents' constructors should call this parent constructor @@ -97,6 +99,19 @@ abstract class BasePackage implements PackageInterface return false; } + public function getRepository() + { + return $this->repository; + } + + public function setRepository(RepositoryInterface $repository) + { + if ($this->repository) { + throw new \LogicException('A package can only be added to one repository'); + } + $this->repository = $repository; + } + /** * Converts the package into a readable and unique string * diff --git a/src/Composer/Package/PackageInterface.php b/src/Composer/Package/PackageInterface.php index 08c1eb39d..4db93316c 100644 --- a/src/Composer/Package/PackageInterface.php +++ b/src/Composer/Package/PackageInterface.php @@ -13,6 +13,7 @@ namespace Composer\Package; use Composer\DependencyResolver\RelationConstraint\RelationConstraintInterface; +use Composer\DependencyResolver\RepositoryInterface; /** * @author Nils Adermann @@ -136,6 +137,20 @@ interface PackageInterface */ function getSuggests(); + /** + * Stores a reference to the repository that owns the package + * + * @param RepositoryInterface $repository + */ + function setRepository(RepositoryInterface $repository); + + /** + * Returns a reference to the repository that owns the package + * + * @return RepositoryInterface + */ + function getRepository(); + /** * Converts the package into a readable and unique string *