BasePackage.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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\Package\LinkConstraint\LinkConstraintInterface;
  13. use Composer\Package\LinkConstraint\VersionConstraint;
  14. use Composer\Repository\RepositoryInterface;
  15. /**
  16. * Base class for packages providing name storage and default match implementation
  17. *
  18. * @author Nils Adermann <naderman@naderman.de>
  19. */
  20. abstract class BasePackage implements PackageInterface
  21. {
  22. protected $name;
  23. protected $prettyName;
  24. protected $repository;
  25. protected $id;
  26. /**
  27. * All descendants' constructors should call this parent constructor
  28. *
  29. * @param string $name The package's name
  30. */
  31. public function __construct($name)
  32. {
  33. $this->prettyName = $name;
  34. $this->name = strtolower($name);
  35. $this->id = -1;
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function getName()
  41. {
  42. return $this->name;
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function getPrettyName()
  48. {
  49. return $this->prettyName;
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function getNames()
  55. {
  56. $names = array(
  57. $this->getName(),
  58. );
  59. foreach ($this->getProvides() as $link) {
  60. $names[] = $link->getTarget();
  61. }
  62. foreach ($this->getReplaces() as $link) {
  63. $names[] = $link->getTarget();
  64. }
  65. return $names;
  66. }
  67. /**
  68. * {@inheritDoc}
  69. */
  70. public function setId($id)
  71. {
  72. $this->id = $id;
  73. }
  74. /**
  75. * {@inheritDoc}
  76. */
  77. public function getId()
  78. {
  79. return $this->id;
  80. }
  81. /**
  82. * Checks if the package matches the given constraint directly or through
  83. * provided or replaced packages
  84. *
  85. * @param string $name Name of the package to be matched
  86. * @param LinkConstraintInterface $constraint The constraint to verify
  87. * @return bool Whether this package matches the name and constraint
  88. */
  89. public function matches($name, LinkConstraintInterface $constraint)
  90. {
  91. if ($this->name === $name) {
  92. return $constraint->matches(new VersionConstraint('==', $this->getVersion()));
  93. }
  94. foreach ($this->getProvides() as $link) {
  95. if ($link->getTarget() === $name) {
  96. return $constraint->matches($link->getConstraint());
  97. }
  98. }
  99. foreach ($this->getReplaces() as $link) {
  100. if ($link->getTarget() === $name) {
  101. return $constraint->matches($link->getConstraint());
  102. }
  103. }
  104. return false;
  105. }
  106. public function getRepository()
  107. {
  108. return $this->repository;
  109. }
  110. public function setRepository(RepositoryInterface $repository)
  111. {
  112. if ($this->repository) {
  113. throw new \LogicException('A package can only be added to one repository');
  114. }
  115. $this->repository = $repository;
  116. }
  117. /**
  118. * Returns package unique name, constructed from name, version and release type.
  119. *
  120. * @return string
  121. */
  122. public function getUniqueName()
  123. {
  124. return $this->getName().'-'.$this->getVersion();
  125. }
  126. /**
  127. * Converts the package into a readable and unique string
  128. *
  129. * @return string
  130. */
  131. public function __toString()
  132. {
  133. return $this->getUniqueName();
  134. }
  135. public function __clone()
  136. {
  137. $this->repository = null;
  138. }
  139. }