BasePackage.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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($provides = true)
  82. {
  83. $names = array(
  84. $this->getName() => true,
  85. );
  86. if ($provides) {
  87. foreach ($this->getProvides() as $link) {
  88. $names[$link->getTarget()] = true;
  89. }
  90. }
  91. foreach ($this->getReplaces() as $link) {
  92. $names[$link->getTarget()] = true;
  93. }
  94. return array_keys($names);
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function setId($id)
  100. {
  101. $this->id = $id;
  102. }
  103. /**
  104. * {@inheritDoc}
  105. */
  106. public function getId()
  107. {
  108. return $this->id;
  109. }
  110. /**
  111. * {@inheritDoc}
  112. */
  113. public function setRepository(RepositoryInterface $repository)
  114. {
  115. if ($this->repository && $repository !== $this->repository) {
  116. throw new \LogicException('A package can only be added to one repository');
  117. }
  118. $this->repository = $repository;
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public function getRepository()
  124. {
  125. return $this->repository;
  126. }
  127. /**
  128. * {@inheritDoc}
  129. */
  130. public function getTransportOptions()
  131. {
  132. return $this->transportOptions;
  133. }
  134. /**
  135. * Configures the list of options to download package dist files
  136. *
  137. * @param array $options
  138. */
  139. public function setTransportOptions(array $options)
  140. {
  141. $this->transportOptions = $options;
  142. }
  143. /**
  144. * checks if this package is a platform package
  145. *
  146. * @return bool
  147. */
  148. public function isPlatform()
  149. {
  150. return $this->getRepository() instanceof PlatformRepository;
  151. }
  152. /**
  153. * Returns package unique name, constructed from name, version and release type.
  154. *
  155. * @return string
  156. */
  157. public function getUniqueName()
  158. {
  159. return $this->getName().'-'.$this->getVersion();
  160. }
  161. public function equals(PackageInterface $package)
  162. {
  163. $self = $this;
  164. if ($this instanceof AliasPackage) {
  165. $self = $this->getAliasOf();
  166. }
  167. if ($package instanceof AliasPackage) {
  168. $package = $package->getAliasOf();
  169. }
  170. return $package === $self;
  171. }
  172. /**
  173. * Converts the package into a readable and unique string
  174. *
  175. * @return string
  176. */
  177. public function __toString()
  178. {
  179. return $this->getUniqueName();
  180. }
  181. public function getPrettyString()
  182. {
  183. return $this->getPrettyName().' '.$this->getPrettyVersion();
  184. }
  185. /**
  186. * {@inheritDoc}
  187. */
  188. public function getFullPrettyVersion($truncate = true, $displayMode = PackageInterface::DISPLAY_SOURCE_REF_IF_DEV)
  189. {
  190. if ($displayMode === PackageInterface::DISPLAY_SOURCE_REF_IF_DEV &&
  191. (!$this->isDev() || !in_array($this->getSourceType(), array('hg', 'git')))
  192. ) {
  193. return $this->getPrettyVersion();
  194. }
  195. switch ($displayMode) {
  196. case PackageInterface::DISPLAY_SOURCE_REF_IF_DEV:
  197. case PackageInterface::DISPLAY_SOURCE_REF:
  198. $reference = $this->getSourceReference();
  199. break;
  200. case PackageInterface::DISPLAY_DIST_REF:
  201. $reference = $this->getDistReference();
  202. break;
  203. }
  204. // if source reference is a sha1 hash -- truncate
  205. if ($truncate && strlen($reference) === 40) {
  206. return $this->getPrettyVersion() . ' ' . substr($reference, 0, 7);
  207. }
  208. return $this->getPrettyVersion() . ' ' . $reference;
  209. }
  210. public function getStabilityPriority()
  211. {
  212. return self::$stabilities[$this->getStability()];
  213. }
  214. public function __clone()
  215. {
  216. $this->repository = null;
  217. $this->id = -1;
  218. }
  219. /**
  220. * Build a regexp from a package name, expanding * globs as required
  221. *
  222. * @param string $allowPattern
  223. * @param string $wrap Wrap the cleaned string by the given string
  224. * @return string
  225. */
  226. public static function packageNameToRegexp($allowPattern, $wrap = '{^%s$}i')
  227. {
  228. $cleanedAllowPattern = str_replace('\\*', '.*', preg_quote($allowPattern));
  229. return sprintf($wrap, $cleanedAllowPattern);
  230. }
  231. }