BasePackage.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Repository\RepositoryInterface;
  13. use Composer\Repository\PlatformRepository;
  14. /**
  15. * Base class for packages providing name storage and default match implementation
  16. *
  17. * @author Nils Adermann <naderman@naderman.de>
  18. */
  19. abstract class BasePackage implements PackageInterface
  20. {
  21. public static $supportedLinkTypes = array(
  22. 'require' => array('description' => 'requires', 'method' => 'requires'),
  23. 'conflict' => array('description' => 'conflicts', 'method' => 'conflicts'),
  24. 'provide' => array('description' => 'provides', 'method' => 'provides'),
  25. 'replace' => array('description' => 'replaces', 'method' => 'replaces'),
  26. 'require-dev' => array('description' => 'requires (for development)', 'method' => 'devRequires'),
  27. );
  28. const STABILITY_STABLE = 0;
  29. const STABILITY_RC = 5;
  30. const STABILITY_BETA = 10;
  31. const STABILITY_ALPHA = 15;
  32. const STABILITY_DEV = 20;
  33. public static $stabilities = array(
  34. 'stable' => self::STABILITY_STABLE,
  35. 'RC' => self::STABILITY_RC,
  36. 'beta' => self::STABILITY_BETA,
  37. 'alpha' => self::STABILITY_ALPHA,
  38. 'dev' => self::STABILITY_DEV,
  39. );
  40. /**
  41. * READ-ONLY: The package id, public for fast access in dependency solver
  42. * @var int
  43. */
  44. public $id;
  45. /** @var string */
  46. protected $name;
  47. /** @var string */
  48. protected $prettyName;
  49. /** @var RepositoryInterface */
  50. protected $repository;
  51. /** @var array */
  52. protected $transportOptions = array();
  53. /**
  54. * All descendants' constructors should call this parent constructor
  55. *
  56. * @param string $name The package's name
  57. */
  58. public function __construct($name)
  59. {
  60. $this->prettyName = $name;
  61. $this->name = strtolower($name);
  62. $this->id = -1;
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getName()
  68. {
  69. return $this->name;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getPrettyName()
  75. {
  76. return $this->prettyName;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function getNames()
  82. {
  83. $names = array(
  84. $this->getName() => true,
  85. );
  86. foreach ($this->getProvides() as $link) {
  87. $names[$link->getTarget()] = true;
  88. }
  89. foreach ($this->getReplaces() as $link) {
  90. $names[$link->getTarget()] = true;
  91. }
  92. return array_keys($names);
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function setId($id)
  98. {
  99. $this->id = $id;
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function getId()
  105. {
  106. return $this->id;
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function setRepository(RepositoryInterface $repository)
  112. {
  113. if ($this->repository && $repository !== $this->repository) {
  114. throw new \LogicException('A package can only be added to one repository');
  115. }
  116. $this->repository = $repository;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getRepository()
  122. {
  123. return $this->repository;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function getTransportOptions()
  129. {
  130. return $this->transportOptions;
  131. }
  132. /**
  133. * Configures the list of options to download package dist files
  134. *
  135. * @param array $options
  136. */
  137. public function setTransportOptions(array $options)
  138. {
  139. $this->transportOptions = $options;
  140. }
  141. /**
  142. * checks if this package is a platform package
  143. *
  144. * @return bool
  145. */
  146. public function isPlatform()
  147. {
  148. return $this->getRepository() instanceof PlatformRepository;
  149. }
  150. /**
  151. * Returns package unique name, constructed from name, version and release type.
  152. *
  153. * @return string
  154. */
  155. public function getUniqueName()
  156. {
  157. return $this->getName().'-'.$this->getVersion();
  158. }
  159. public function equals(PackageInterface $package)
  160. {
  161. $self = $this;
  162. if ($this instanceof AliasPackage) {
  163. $self = $this->getAliasOf();
  164. }
  165. if ($package instanceof AliasPackage) {
  166. $package = $package->getAliasOf();
  167. }
  168. return $package === $self;
  169. }
  170. /**
  171. * Converts the package into a readable and unique string
  172. *
  173. * @return string
  174. */
  175. public function __toString()
  176. {
  177. return $this->getUniqueName();
  178. }
  179. public function getPrettyString()
  180. {
  181. return $this->getPrettyName().' '.$this->getPrettyVersion();
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public function getFullPrettyVersion($truncate = true)
  187. {
  188. if (!$this->isDev() || !in_array($this->getSourceType(), array('hg', 'git'))) {
  189. return $this->getPrettyVersion();
  190. }
  191. // if source reference is a sha1 hash -- truncate
  192. if ($truncate && strlen($this->getSourceReference()) === 40) {
  193. return $this->getPrettyVersion() . ' ' . substr($this->getSourceReference(), 0, 7);
  194. }
  195. return $this->getPrettyVersion() . ' ' . $this->getSourceReference();
  196. }
  197. public function getStabilityPriority()
  198. {
  199. return self::$stabilities[$this->getStability()];
  200. }
  201. public function __clone()
  202. {
  203. $this->repository = null;
  204. $this->id = -1;
  205. }
  206. }