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

main
Jordi Boggiano 9 years ago
parent 049f84f21f
commit 801a7fcd0a

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

@ -26,6 +26,8 @@ use Composer\Util\RemoteFilesystem;
use Composer\Plugin\PluginEvents; use Composer\Plugin\PluginEvents;
use Composer\Plugin\PreFileDownloadEvent; use Composer\Plugin\PreFileDownloadEvent;
use Composer\EventDispatcher\EventDispatcher; use Composer\EventDispatcher\EventDispatcher;
use Composer\Package\LinkConstraint\LinkConstraintInterface;
use Composer\Package\LinkConstraint\VersionConstraint;
/** /**
* @author Jordi Boggiano <j.boggiano@seld.be> * @author Jordi Boggiano <j.boggiano@seld.be>
@ -99,22 +101,27 @@ class ComposerRepository extends ArrayRepository
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function findPackage($name, $version) public function findPackage($name, $constraint)
{ {
if (!$this->hasProviders()) { if (!$this->hasProviders()) {
return parent::findPackage($name, $version); return parent::findPackage($name, $constraint);
} }
// normalize version & name
$versionParser = new VersionParser();
$version = $versionParser->normalize($version);
$name = strtolower($name); $name = strtolower($name);
if (!$constraint instanceof LinkConstraintInterface) {
$versionParser = new VersionParser();
$constraint = $versionParser->parseConstraints($constraint);
}
foreach ($this->getProviderNames() as $providerName) { foreach ($this->getProviderNames() as $providerName) {
if ($name === $providerName) { if ($name === $providerName) {
$packages = $this->whatProvides(new Pool('dev'), $providerName); $packages = $this->whatProvides(new Pool('dev'), $providerName);
foreach ($packages as $package) { foreach ($packages as $package) {
if ($name == $package->getName() && $version === $package->getVersion()) { if ($name === $package->getName()) {
return $package; $pkgConstraint = new VersionConstraint('==', $package->getVersion());
if ($constraint->matches($pkgConstraint)) {
return $package;
}
} }
} }
} }
@ -124,28 +131,30 @@ class ComposerRepository extends ArrayRepository
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function findPackages($name, $version = null) public function findPackages($name, $constraint = null)
{ {
if (!$this->hasProviders()) { if (!$this->hasProviders()) {
return parent::findPackages($name, $version); return parent::findPackages($name, $constraint);
} }
// normalize name // normalize name
$name = strtolower($name); $name = strtolower($name);
// normalize version if (null !== $constraint && !$constraint instanceof LinkConstraintInterface) {
if (null !== $version) {
$versionParser = new VersionParser(); $versionParser = new VersionParser();
$version = $versionParser->normalize($version); $constraint = $versionParser->parseConstraints($constraint);
} }
$packages = array(); $packages = array();
foreach ($this->getProviderNames() as $providerName) { foreach ($this->getProviderNames() as $providerName) {
if ($name === $providerName) { if ($name === $providerName) {
$packages = $this->whatProvides(new Pool('dev'), $providerName); $candidates = $this->whatProvides(new Pool('dev'), $providerName);
foreach ($packages as $package) { foreach ($candidates as $package) {
if ($name == $package->getName() && (null === $version || $version === $package->getVersion())) { if ($name === $package->getName()) {
$packages[] = $package; $pkgConstraint = new VersionConstraint('==', $package->getVersion());
if (null === $constraint || $constraint->matches($pkgConstraint)) {
$packages[] = $package;
}
} }
} }
} }

@ -67,11 +67,11 @@ class CompositeRepository implements RepositoryInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function findPackage($name, $version) public function findPackage($name, $constraint)
{ {
foreach ($this->repositories as $repository) { foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */ /* @var $repository RepositoryInterface */
$package = $repository->findPackage($name, $version); $package = $repository->findPackage($name, $constraint);
if (null !== $package) { if (null !== $package) {
return $package; return $package;
} }
@ -83,12 +83,12 @@ class CompositeRepository implements RepositoryInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function findPackages($name, $version = null) public function findPackages($name, $constraint = null)
{ {
$packages = array(); $packages = array();
foreach ($this->repositories as $repository) { foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */ /* @var $repository RepositoryInterface */
$packages[] = $repository->findPackages($name, $version); $packages[] = $repository->findPackages($name, $constraint);
} }
return $packages ? call_user_func_array('array_merge', $packages) : array(); return $packages ? call_user_func_array('array_merge', $packages) : array();

@ -38,22 +38,22 @@ interface RepositoryInterface extends \Countable
/** /**
* Searches for the first match of a package by name and version. * Searches for the first match of a package by name and version.
* *
* @param string $name package name * @param string $name package name
* @param string $version package version * @param string|\Composer\Package\LinkConstraint\LinkConstraintInterface $constraint package version or version constraint to match against
* *
* @return PackageInterface|null * @return PackageInterface|null
*/ */
public function findPackage($name, $version); public function findPackage($name, $constraint);
/** /**
* Searches for all packages matching a name and optionally a version. * Searches for all packages matching a name and optionally a version.
* *
* @param string $name package name * @param string $name package name
* @param string $version package version * @param string|\Composer\Package\LinkConstraint\LinkConstraintInterface $constraint package version or version constraint to match against
* *
* @return array * @return array
*/ */
public function findPackages($name, $version = null); public function findPackages($name, $constraint = null);
/** /**
* Returns list of registered packages. * Returns list of registered packages.

@ -42,15 +42,15 @@ class RepositoryManager
/** /**
* Searches for a package by it's name and version in managed repositories. * Searches for a package by it's name and version in managed repositories.
* *
* @param string $name package name * @param string $name package name
* @param string $version package version * @param string|\Composer\Package\LinkConstraint\LinkConstraintInterface $constraint package version or version constraint to match against
* *
* @return PackageInterface|null * @return PackageInterface|null
*/ */
public function findPackage($name, $version) public function findPackage($name, $constraint)
{ {
foreach ($this->repositories as $repository) { foreach ($this->repositories as $repository) {
if ($package = $repository->findPackage($name, $version)) { if ($package = $repository->findPackage($name, $constraint)) {
return $package; return $package;
} }
} }
@ -59,17 +59,17 @@ class RepositoryManager
/** /**
* Searches for all packages matching a name and optionally a version in managed repositories. * Searches for all packages matching a name and optionally a version in managed repositories.
* *
* @param string $name package name * @param string $name package name
* @param string $version package version * @param string|\Composer\Package\LinkConstraint\LinkConstraintInterface $constraint package version or version constraint to match against
* *
* @return array * @return array
*/ */
public function findPackages($name, $version) public function findPackages($name, $constraint)
{ {
$packages = array(); $packages = array();
foreach ($this->repositories as $repository) { foreach ($this->repositories as $repository) {
$packages = array_merge($packages, $repository->findPackages($name, $version)); $packages = array_merge($packages, $repository->findPackages($name, $constraint));
} }
return $packages; return $packages;

Loading…
Cancel
Save