/** * finds a package by name and version if provided * * @param RepositoryInterface $installedRepo * @param RepositoryInterface $repos * @param string $name * @param ConstraintInterface|string $version * @throws \InvalidArgumentException * @return array array(CompletePackageInterface, array of versions) */ protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null) { $name = strtolower($name); $constraint = is_string($version) ? $this->versionParser->parseConstraints($version) : $version; $policy = new DefaultPolicy(); $pool = new Pool('dev'); $pool->addRepository($repos); $matchedPackage = null; $versions = array(); $matches = $pool->whatProvides($name, $constraint); foreach ($matches as $index => $package) { // skip providers/replacers if ($package->getName() !== $name) { unset($matches[$index]); continue; } // select an exact match if it is in the installed repo and no specific version was required if (null === $version && $installedRepo->hasPackage($package)) { $matchedPackage = $package; } $versions[$package->getPrettyVersion()] = $package->getVersion(); $matches[$index] = $package->getId(); } // select preferred package according to policy rules if (!$matchedPackage && $matches && ($preferred = $policy->selectPreferredPackages($pool, array(), $matches))) { $matchedPackage = $pool->literalToPackage($preferred[0]); } return array($matchedPackage, $versions); }
public function testSelectLowest() { $policy = new DefaultPolicy(false, true); $this->repo->addPackage($packageA1 = $this->getPackage('A', '1.0')); $this->repo->addPackage($packageA2 = $this->getPackage('A', '2.0')); $this->pool->addRepository($this->repo); $literals = array($packageA1->getId(), $packageA2->getId()); $expected = array($packageA1->getId()); $selected = $policy->selectPreferredPackages($this->pool, array(), $literals); $this->assertEquals($expected, $selected); }
/** * Finds a package by name and version if provided. * * @param RepositoryInterface $installedRepo * @param RepositoryInterface $repos * @param string $name * @param string|null $version * * @throws \InvalidArgumentException * * @return array [CompletePackageInterface, array of versions] */ protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null) { $name = strtolower($name); $constraint = null; if ($version !== null) { $constraint = $this->versionParser->parseConstraints($version); } $policy = new DefaultPolicy(); $pool = new Pool('dev'); $pool->addRepository($repos); $matchedPackage = null; $versions = []; $matches = $pool->whatProvides($name, $constraint); foreach ($matches as $index => $package) { // Skip providers/replacers. if ($package->getName() !== $name) { unset($matches[$index]); continue; } // Select an exact match if it is in the installed repo and no specific version was required. if ($version === null && $installedRepo->hasPackage($package)) { $matchedPackage = $package; } $versions[$package->getPrettyVersion()] = $package->getVersion(); $matches[$index] = $package->getId(); } // Select prefered package according to policy rules. if (!$matchedPackage && !empty($matches) && ($prefered = $policy->selectPreferredPackages($pool, [], $matches))) { $matchedPackage = $pool->literalToPackage($prefered[0]); } // If we have package result, return them. if ($matchedPackage) { return [$matchedPackage->getName() => ['package' => $matchedPackage, 'versions' => $versions]]; } return null; }