BasePackage.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\Package;
  12. use Composer\DependencyResolver\RelationConstraint\RelationConstraintInterface;
  13. /**
  14. * Base class for packages providing name storage and default match implementation
  15. *
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. abstract class Package implements PackageInterface
  19. {
  20. protected $name;
  21. protected $id;
  22. /**
  23. * All descendents' constructors should call this parent constructor
  24. *
  25. * @param string $name The package's name
  26. */
  27. public function __construct($name)
  28. {
  29. $this->name = $name;
  30. }
  31. /**
  32. * Returns the package's name without version info, thus not a unique identifier
  33. *
  34. * @return string package name
  35. */
  36. public function getName()
  37. {
  38. return $this->name;
  39. }
  40. /**
  41. * Returns a set of names that could refer to this package
  42. *
  43. * No version or release type information should be included in any of the
  44. * names. Provided or replaced package names need to be returned as well.
  45. *
  46. * @return array An array of strings refering to this package
  47. */
  48. public function getNames()
  49. {
  50. $names = array(
  51. $this->getName(),
  52. );
  53. foreach ($this->getProvides() as $relation) {
  54. $names[] = $relation->getToPackageName();
  55. }
  56. foreach ($this->getReplaces() as $relation) {
  57. $names[] = $relation->getToPackageName();
  58. }
  59. return $names;
  60. }
  61. /**
  62. * Checks if the package matches the given constraint directly or through
  63. * provided or replaced packages
  64. *
  65. * @param string $name Name of the package to be matched
  66. * @param RelationConstraintInterface $constraint The constraint to verify
  67. * @return bool Whether this package matches the name and constraint
  68. */
  69. public function matches($name, RelationConstraintInterface $constraint)
  70. {
  71. if ($this->name === $name) {
  72. return $constraint->matches($this->getReleaseType(), $this->getVersion());
  73. }
  74. foreach ($this->getProvides() as $relation) {
  75. if ($relation->getToPackageName() === $name) {
  76. return $constraint->matches($relation->getToReleaseType(), $relation->getToVersion());
  77. }
  78. }
  79. foreach ($this->getReplaces() as $relation) {
  80. if ($relation->getToPackageName() === $name) {
  81. return $constraint->matches($relation->getToReleaseType(), $relation->getToVersion());
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. * Converts the package into a readable and unique string
  88. *
  89. * @return string
  90. */
  91. public function __toString()
  92. {
  93. return $this->getName().'-'.$this->getReleaseType().'-'.$this->getVersion();
  94. }
  95. }