BasePackage.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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' => array('description' => 'requires', 'method' => 'requires'),
  25. 'conflict' => array('description' => 'conflicts', 'method' => 'conflicts'),
  26. 'provide' => array('description' => 'provides', 'method' => 'provides'),
  27. 'replace' => array('description' => 'replaces', 'method' => 'replaces'),
  28. 'require-dev' => array('description' => 'requires (for development)', 'method' => 'devRequires'),
  29. );
  30. const STABILITY_STABLE = 0;
  31. const STABILITY_RC = 5;
  32. const STABILITY_BETA = 10;
  33. const STABILITY_ALPHA = 15;
  34. const STABILITY_DEV = 20;
  35. public static $stabilities = array(
  36. 'stable' => self::STABILITY_STABLE,
  37. 'RC' => self::STABILITY_RC,
  38. 'beta' => self::STABILITY_BETA,
  39. 'alpha' => self::STABILITY_ALPHA,
  40. 'dev' => self::STABILITY_DEV,
  41. );
  42. protected $name;
  43. protected $prettyName;
  44. protected $repository;
  45. protected $id;
  46. /**
  47. * All descendants' constructors should call this parent constructor
  48. *
  49. * @param string $name The package's name
  50. */
  51. public function __construct($name)
  52. {
  53. $this->prettyName = $name;
  54. $this->name = strtolower($name);
  55. $this->id = -1;
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function getName()
  61. {
  62. return $this->name;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getPrettyName()
  68. {
  69. return $this->prettyName;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getNames()
  75. {
  76. $names = array(
  77. $this->getName() => true,
  78. );
  79. foreach ($this->getProvides() as $link) {
  80. $names[$link->getTarget()] = true;
  81. }
  82. foreach ($this->getReplaces() as $link) {
  83. $names[$link->getTarget()] = true;
  84. }
  85. return array_keys($names);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function setId($id)
  91. {
  92. $this->id = $id;
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getId()
  98. {
  99. return $this->id;
  100. }
  101. /**
  102. * Checks if the package matches the given constraint directly or through
  103. * provided or replaced packages
  104. *
  105. * @param string $name Name of the package to be matched
  106. * @param LinkConstraintInterface $constraint The constraint to verify
  107. * @return bool Whether this package matches the name and constraint
  108. */
  109. public function matches($name, LinkConstraintInterface $constraint)
  110. {
  111. if ($this->name === $name) {
  112. return $constraint->matches(new VersionConstraint('==', $this->getVersion()));
  113. }
  114. foreach ($this->getProvides() as $link) {
  115. if ($link->getTarget() === $name && $constraint->matches($link->getConstraint())) {
  116. return true;
  117. }
  118. }
  119. foreach ($this->getReplaces() as $link) {
  120. if ($link->getTarget() === $name && $constraint->matches($link->getConstraint())) {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. public function getRepository()
  127. {
  128. return $this->repository;
  129. }
  130. public function setRepository(RepositoryInterface $repository)
  131. {
  132. if ($this->repository) {
  133. throw new \LogicException('A package can only be added to one repository');
  134. }
  135. $this->repository = $repository;
  136. }
  137. /**
  138. * checks if this package is a platform package
  139. *
  140. * @return boolean
  141. */
  142. public function isPlatform()
  143. {
  144. return $this->getRepository() instanceof PlatformRepository;
  145. }
  146. /**
  147. * Returns package unique name, constructed from name, version and release type.
  148. *
  149. * @return string
  150. */
  151. public function getUniqueName()
  152. {
  153. return $this->getName().'-'.$this->getVersion();
  154. }
  155. public function equals(PackageInterface $package)
  156. {
  157. $self = $this;
  158. if ($this instanceof AliasPackage) {
  159. $self = $this->getAliasOf();
  160. }
  161. if ($package instanceof AliasPackage) {
  162. $package = $package->getAliasOf();
  163. }
  164. return $package === $self;
  165. }
  166. /**
  167. * Converts the package into a readable and unique string
  168. *
  169. * @return string
  170. */
  171. public function __toString()
  172. {
  173. return $this->getUniqueName();
  174. }
  175. public function getPrettyString()
  176. {
  177. return $this->getPrettyName().' '.$this->getPrettyVersion();
  178. }
  179. public function __clone()
  180. {
  181. $this->repository = null;
  182. }
  183. }