Update solver to use PackageInterface and store pool package ids inside of packages

main
Nils Adermann 13 years ago
parent 8cbf3f4d75
commit 2e41993822

@ -37,6 +37,7 @@ class Pool
foreach ($repo->getPackages() as $package) { foreach ($repo->getPackages() as $package) {
$this->packages[] = $package; $this->packages[] = $package;
$package->setId(sizeof($this->packages));
foreach ($package->getNames() as $name) { foreach ($package->getNames() as $name) {
$this->packageByName[$name][] = $package; $this->packageByName[$name][] = $package;

@ -72,8 +72,9 @@ class Rule
$this->type = $type; $this->type = $type;
} }
public function getType($type) public function getType()
{ {
return $this->type;
} }
public function disable() public function disable()

@ -82,12 +82,12 @@ class Solver
* one requirement of the package A. * one requirement of the package A.
* *
* @param PackageInterface $package The package with a requirement * @param PackageInterface $package The package with a requirement
* @param array $providers The providers of the requirement * @param array $providers The providers of the requirement
* @param int $reason A RULE_* constant describing the reason for * @param int $reason A RULE_* constant describing the
* generating this rule * reason for generating this rule
* @param mixed $reasonData Any data, e.g. the requirement name, that goes with * @param mixed $reasonData Any data, e.g. the requirement name,
* the reason * that goes with the reason
* @return Rule The generated rule or null if tautological * @return Rule The generated rule or null if tautological
*/ */
public function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null) public function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null)
{ {
@ -110,12 +110,12 @@ class Solver
* If package A1 can be updated to A2 or A3 the rule is (A1|A2|A3). * If package A1 can be updated to A2 or A3 the rule is (A1|A2|A3).
* *
* @param PackageInterface $package The package to be updated * @param PackageInterface $package The package to be updated
* @param array $updates An array of update candidate packages * @param array $updates An array of update candidate packages
* @param int $reason A RULE_* constant describing the reason for * @param int $reason A RULE_* constant describing the
* generating this rule * reason for generating this rule
* @param mixed $reasonData Any data, e.g. the package name, that goes with * @param mixed $reasonData Any data, e.g. the package name, that
* the reason * goes with the reason
* @return Rule The generated rule or null if tautology * @return Rule The generated rule or null if tautology
*/ */
protected function createUpdateRule(PackageInterface $package, array $updates, $reason, $reasonData = null) protected function createUpdateRule(PackageInterface $package, array $updates, $reason, $reasonData = null)
{ {
@ -134,11 +134,11 @@ class Solver
* The rule is simply (A) for a package A to be installed. * The rule is simply (A) for a package A to be installed.
* *
* @param PackageInterface $package The package to be installed * @param PackageInterface $package The package to be installed
* @param int $reason A RULE_* constant describing the reason for * @param int $reason A RULE_* constant describing the
* generating this rule * reason for generating this rule
* @param mixed $reasonData Any data, e.g. the package name, that goes with * @param mixed $reasonData Any data, e.g. the package name, that
* the reason * goes with the reason
* @return Rule The generated rule * @return Rule The generated rule
*/ */
public function createInstallRule(PackageInterface $package, $reason, $reasonData = null) public function createInstallRule(PackageInterface $package, $reason, $reasonData = null)
{ {
@ -151,7 +151,7 @@ class Solver
* The rule is (A|B|C) with A, B and C different packages. If the given * The rule is (A|B|C) with A, B and C different packages. If the given
* set of packages is empty an impossible rule is generated. * set of packages is empty an impossible rule is generated.
* *
* @param array $packages The set of packages to choose from * @param array $packages The set of packages to choose from
* @param int $reason A RULE_* constant describing the reason for * @param int $reason A RULE_* constant describing the reason for
* generating this rule * generating this rule
* @param mixed $reasonData Any data, e.g. the package name, that goes with * @param mixed $reasonData Any data, e.g. the package name, that goes with
@ -178,11 +178,11 @@ class Solver
* The rule for a package A is (-A). * The rule for a package A is (-A).
* *
* @param PackageInterface $package The package to be removed * @param PackageInterface $package The package to be removed
* @param int $reason A RULE_* constant describing the reason for * @param int $reason A RULE_* constant describing the
* generating this rule * reason for generating this rule
* @param mixed $reasonData Any data, e.g. the package name, that goes with * @param mixed $reasonData Any data, e.g. the package name, that
* the reason * goes with the reason
* @return Rule The generated rule * @return Rule The generated rule
*/ */
public function createRemoveRule(PackageInterface $package, $reason, $reasonData = null) public function createRemoveRule(PackageInterface $package, $reason, $reasonData = null)
{ {
@ -195,15 +195,15 @@ class Solver
* The rule for conflicting packages A and B is (-A|-B). A is called the issuer * The rule for conflicting packages A and B is (-A|-B). A is called the issuer
* and B the provider. * and B the provider.
* *
* @param Package $issuer The package declaring the conflict * @param PackageInterface $issuer The package declaring the conflict
* @param Package $provider The package causing the conflict * @param Package $provider The package causing the conflict
* @param int $reason A RULE_* constant describing the reason for * @param int $reason A RULE_* constant describing the
* generating this rule * reason for generating this rule
* @param mixed $reasonData Any data, e.g. the package name, that goes with * @param mixed $reasonData Any data, e.g. the package name, that
* the reason * goes with the reason
* @return Rule The generated rule * @return Rule The generated rule
*/ */
public function createConflictRule(Package $issuer, Package $provider, $reason, $reasonData = null) public function createConflictRule(PackageInterface $issuer, Package $provider, $reason, $reasonData = null)
{ {
// ignore self conflict // ignore self conflict
if ($issuer === $provider) { if ($issuer === $provider) {
@ -261,14 +261,14 @@ class Solver
while (!$workQueue->isEmpty()) { while (!$workQueue->isEmpty()) {
$package = $workQueue->dequeue(); $package = $workQueue->dequeue();
if (isset($this->addedMap[$this->getId($package)])) { if (isset($this->addedMap[$package->getId()])) {
continue; continue;
} }
$this->addedMap[$this->getId($package)] = true; $this->addedMap[$package->getId()] = true;
$dontFix = 0; $dontFix = 0;
if ($this->installed === $package->getRepository() && !isset($this->fixMap[$this->getId($package)])) { if ($this->installed === $package->getRepository() && !isset($this->fixMap[$package->getId()])) {
$dontFix = 1; $dontFix = 1;
} }
@ -335,8 +335,9 @@ class Solver
/** /**
* Adds all rules for all update packages of a given package * Adds all rules for all update packages of a given package
* *
* @param PackageInterface $package Rules for this package's updates are to be added * @param PackageInterface $package Rules for this package's updates are to
* @param bool $allowAll Whether downgrades are allowed * be added
* @param bool $allowAll Whether downgrades are allowed
*/ */
private function addRulesForUpdatePackages(PackageInterface $package, $allowAll) private function addRulesForUpdatePackages(PackageInterface $package, $allowAll)
{ {
@ -427,7 +428,7 @@ class Solver
$conflict = $this->findDecisionRule($literal->getPackage()); $conflict = $this->findDecisionRule($literal->getPackage());
// todo: handle conflict with systemsolvable? // todo: handle conflict with systemsolvable?
if (self::TYPE_PACKAGE === $conflict->getType()) { if ($conflict && self::TYPE_PACKAGE === $conflict->getType()) {
} }
} }
@ -456,13 +457,13 @@ class Solver
switch ($job['cmd']) { switch ($job['cmd']) {
case 'update-all': case 'update-all':
foreach ($installedPackages as $package) { foreach ($installedPackages as $package) {
$this->updateMap[$this->getId($package)] = true; $this->updateMap[$package->getId()] = true;
} }
break; break;
case 'fix-all': case 'fix-all':
foreach ($installedPackages as $package) { foreach ($installedPackages as $package) {
$this->fixMap[$this->getId($package)] = true; $this->fixMap[$package->getId()] = true;
} }
break; break;
} }
@ -471,12 +472,12 @@ class Solver
switch ($job['cmd']) { switch ($job['cmd']) {
case 'fix': case 'fix':
if ($this->installed === $package->getRepository()) { if ($this->installed === $package->getRepository()) {
$this->fixMap[$this->getId($package)] = true; $this->fixMap[$package->getId()] = true;
} }
break; break;
case 'update': case 'update':
if ($this->installed === $package->getRepository()) { if ($this->installed === $package->getRepository()) {
$this->updateMap[$this->getId($package)] = true; $this->updateMap[$package->getId()] = true;
} }
break; break;
} }
@ -496,7 +497,7 @@ class Solver
foreach ($job['packages'] as $package) { foreach ($job['packages'] as $package) {
switch ($job['cmd']) { switch ($job['cmd']) {
case 'install': case 'install':
$this->installCandidateMap[$this->getId($package)] = true; $this->installCandidateMap[$package->getId()] = true;
$this->addRulesForPackage($package); $this->addRulesForPackage($package);
break; break;
} }
@ -641,22 +642,22 @@ class Solver
)); ));
} }
protected function decided(PackageInterface $package) protected function decided(PackageInterface $p)
{ {
return isset($this->decisionMap[$this->getId($package)]); return isset($this->decisionMap[$p->getId()]);
} }
protected function undecided(PackageInterface $package) protected function undecided(PackageInterface $p)
{ {
return !isset($this->decisionMap[$this->getId($package)]); return !isset($this->decisionMap[$p->getId()]);
} }
protected function decidedInstall(PackageInterface $package) { protected function decidedInstall(PackageInterface $p) {
return isset($this->decisionMap[$this->getId($package)]) && $this->decisionMap[$this->getId($package)] > 0; return isset($this->decisionMap[$p->getId()]) && $this->decisionMap[$p->getId()] > 0;
} }
protected function decidedRemove(PackageInterface $package) { protected function decidedRemove(PackageInterface $p) {
return isset($this->decisionMap[$this->getId($package)]) && $this->decisionMap[$this->getId($package)] < 0; return isset($this->decisionMap[$p->getId()]) && $this->decisionMap[$p->getId()] < 0;
} }
/** /**
@ -730,11 +731,6 @@ class Solver
return null; return null;
} }
private function getId($package)
{
return spl_object_hash($package);
}
private function setPropagateLearn($level, Literal $literal, $disableRules, Rule $rule) private function setPropagateLearn($level, Literal $literal, $disableRules, Rule $rule)
{ {
return 0; return 0;

@ -25,6 +25,7 @@ abstract class BasePackage implements PackageInterface
{ {
protected $name; protected $name;
protected $repository; protected $repository;
protected $id;
/** /**
* All descendents' constructors should call this parent constructor * All descendents' constructors should call this parent constructor
@ -34,6 +35,7 @@ abstract class BasePackage implements PackageInterface
public function __construct($name) public function __construct($name)
{ {
$this->name = $name; $this->name = $name;
$this->id = -1;
} }
/** /**
@ -71,6 +73,22 @@ abstract class BasePackage implements PackageInterface
return $names; return $names;
} }
/**
* {@inheritDoc}
*/
public function setId($id)
{
$this->id = $id;
}
/**
* {@inheritDoc}
*/
public function getId()
{
return $this->id;
}
/** /**
* Checks if the package matches the given constraint directly or through * Checks if the package matches the given constraint directly or through
* provided or replaced packages * provided or replaced packages

@ -37,6 +37,20 @@ interface PackageInterface
*/ */
function getNames(); function getNames();
/**
* Allows the solver to set an id for this package to refer to it.
*
* @param int $id
*/
function setId($id);
/**
* Retrieves the package's id set through setId
*
* @return int The previously set package id
*/
function getId();
/** /**
* Checks if the package matches the given constraint directly or through * Checks if the package matches the given constraint directly or through
* provided or replaced packages * provided or replaced packages

Loading…
Cancel
Save