Pool.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\DependencyResolver;
  12. use Composer\Package\LinkConstraint\LinkConstraintInterface;
  13. use Composer\Repository\RepositoryInterface;
  14. /**
  15. * A package pool contains repositories that provide packages.
  16. *
  17. * @author Nils Adermann <naderman@naderman.de>
  18. */
  19. class Pool
  20. {
  21. protected $repositories = array();
  22. protected $packages = array();
  23. protected $packageByName = array();
  24. /**
  25. * Adds a repository and its packages to this package pool
  26. *
  27. * @param RepositoryInterface $repo A package repository
  28. */
  29. public function addRepository(RepositoryInterface $repo)
  30. {
  31. $this->repositories[] = $repo;
  32. foreach ($repo->getPackages() as $package) {
  33. $package->setId(count($this->packages) + 1);
  34. $this->packages[] = $package;
  35. foreach ($package->getNames() as $name) {
  36. $this->packageByName[$name][] = $package;
  37. }
  38. }
  39. }
  40. /**
  41. * Retrieves the package object for a given package id.
  42. *
  43. * @param int $id
  44. * @return PackageInterface
  45. */
  46. public function packageById($id)
  47. {
  48. return $this->packages[$id - 1];
  49. }
  50. /**
  51. * Searches all packages providing the given package name and match the constraint
  52. *
  53. * @param string $name The package name to be searched for
  54. * @param LinkConstraintInterface $constraint A constraint that all returned
  55. * packages must match or null to return all
  56. * @return array A set of packages
  57. */
  58. public function whatProvides($name, LinkConstraintInterface $constraint = null)
  59. {
  60. if (!isset($this->packageByName[$name])) {
  61. return array();
  62. }
  63. $candidates = $this->packageByName[$name];
  64. if (null === $constraint) {
  65. return $candidates;
  66. }
  67. $result = array();
  68. foreach ($candidates as $candidate) {
  69. if ($candidate->matches($name, $constraint)) {
  70. $result[] = $candidate;
  71. }
  72. }
  73. return $result;
  74. }
  75. }