protected function getPackage(RepositoryInterface $installedRepo, RepositoryInterface $repos, $name, $version = null)
{
$name = strtolower($name);
$constraint = null;
if ($version) {
$constraint = $this->versionParser->parseConstraints($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 prefered package according to policy rules
if (!$matchedPackage && $matches && ($prefered = $policy->selectPreferedPackages($pool, array(), $matches))) {
$matchedPackage = $pool->literalToPackage($prefered[0]);
}
return array($matchedPackage, $versions);
}