BasePackage.php 4.2 KB

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