1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-10 17:12:51 +00:00

Add support for constraints and not just exact versions in RepositoryInterface::findPackage/s

This commit is contained in:
Jordi Boggiano 2015-06-18 17:44:58 +01:00
parent 049f84f21f
commit 801a7fcd0a
5 changed files with 67 additions and 50 deletions

View file

@ -16,6 +16,8 @@ use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface;
use Composer\Package\CompletePackageInterface;
use Composer\Package\Version\VersionParser;
use Composer\Package\LinkConstraint\LinkConstraintInterface;
use Composer\Package\LinkConstraint\VersionConstraint;
/**
* A repository implementation that simply stores packages in an array
@ -36,16 +38,21 @@ class ArrayRepository implements RepositoryInterface
/**
* {@inheritDoc}
*/
public function findPackage($name, $version)
public function findPackage($name, $constraint)
{
// normalize version & name
$versionParser = new VersionParser();
$version = $versionParser->normalize($version);
$name = strtolower($name);
if (!$constraint instanceof LinkConstraintInterface) {
$versionParser = new VersionParser();
$constraint = $versionParser->parseConstraints($constraint);
}
foreach ($this->getPackages() as $package) {
if ($name === $package->getName() && $version === $package->getVersion()) {
return $package;
if ($name === $package->getName()) {
$pkgConstraint = new VersionConstraint('==', $package->getVersion());
if ($constraint->matches($pkgConstraint)) {
return $package;
}
}
}
}
@ -53,22 +60,23 @@ class ArrayRepository implements RepositoryInterface
/**
* {@inheritDoc}
*/
public function findPackages($name, $version = null)
public function findPackages($name, $constraint = null)
{
// normalize name
$name = strtolower($name);
// normalize version
if (null !== $version) {
$versionParser = new VersionParser();
$version = $versionParser->normalize($version);
}
$packages = array();
if (null !== $constraint && !$constraint instanceof LinkConstraintInterface) {
$versionParser = new VersionParser();
$constraint = $versionParser->parseConstraints($constraint);
}
foreach ($this->getPackages() as $package) {
if ($package->getName() === $name && (null === $version || $version === $package->getVersion())) {
$packages[] = $package;
if ($name === $package->getName()) {
$pkgConstraint = new VersionConstraint('==', $package->getVersion());
if (null === $constraint || $constraint->matches($pkgConstraint)) {
$packages[] = $package;
}
}
}