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\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);
$packages = array();
// normalize version
if (null !== $version) {
if (null !== $constraint && !$constraint instanceof LinkConstraintInterface) {
$versionParser = new VersionParser();
$version = $versionParser->normalize($version);
$constraint = $versionParser->parseConstraints($constraint);
}
$packages = array();
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;
}
}
}

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

@ -67,11 +67,11 @@ class CompositeRepository implements RepositoryInterface
/**
* {@inheritdoc}
*/
public function findPackage($name, $version)
public function findPackage($name, $constraint)
{
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$package = $repository->findPackage($name, $version);
$package = $repository->findPackage($name, $constraint);
if (null !== $package) {
return $package;
}
@ -83,12 +83,12 @@ class CompositeRepository implements RepositoryInterface
/**
* {@inheritdoc}
*/
public function findPackages($name, $version = null)
public function findPackages($name, $constraint = null)
{
$packages = array();
foreach ($this->repositories as $repository) {
/* @var $repository RepositoryInterface */
$packages[] = $repository->findPackages($name, $version);
$packages[] = $repository->findPackages($name, $constraint);
}
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.
*
* @param string $name package name
* @param string $version package version
* @param string $name package name
* @param string|\Composer\Package\LinkConstraint\LinkConstraintInterface $constraint package version or version constraint to match against
*
* @return PackageInterface|null
*/
public function findPackage($name, $version);
public function findPackage($name, $constraint);
/**
* Searches for all packages matching a name and optionally a version.
*
* @param string $name package name
* @param string $version package version
* @param string $name package name
* @param string|\Composer\Package\LinkConstraint\LinkConstraintInterface $constraint package version or version constraint to match against
*
* @return array
*/
public function findPackages($name, $version = null);
public function findPackages($name, $constraint = null);
/**
* Returns list of registered packages.

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

Loading…
Cancel
Save