BasePackage.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. protected $name;
  46. protected $prettyName;
  47. protected $repository;
  48. protected $transportOptions;
  49. /**
  50. * All descendants' constructors should call this parent constructor
  51. *
  52. * @param string $name The package's name
  53. */
  54. public function __construct($name)
  55. {
  56. $this->prettyName = $name;
  57. $this->name = strtolower($name);
  58. $this->id = -1;
  59. $this->transportOptions = array();
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. public function getName()
  65. {
  66. return $this->name;
  67. }
  68. /**
  69. * {@inheritDoc}
  70. */
  71. public function getPrettyName()
  72. {
  73. return $this->prettyName;
  74. }
  75. /**
  76. * {@inheritDoc}
  77. */
  78. public function getNames()
  79. {
  80. $names = array(
  81. $this->getName() => true,
  82. );
  83. foreach ($this->getProvides() as $link) {
  84. $names[$link->getTarget()] = true;
  85. }
  86. foreach ($this->getReplaces() as $link) {
  87. $names[$link->getTarget()] = true;
  88. }
  89. return array_keys($names);
  90. }
  91. /**
  92. * {@inheritDoc}
  93. */
  94. public function setId($id)
  95. {
  96. $this->id = $id;
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. public function getId()
  102. {
  103. return $this->id;
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function setRepository(RepositoryInterface $repository)
  109. {
  110. if ($this->repository && $repository !== $this->repository) {
  111. throw new \LogicException('A package can only be added to one repository');
  112. }
  113. $this->repository = $repository;
  114. }
  115. /**
  116. * {@inheritDoc}
  117. */
  118. public function getRepository()
  119. {
  120. return $this->repository;
  121. }
  122. /**
  123. * {@inheritDoc}
  124. */
  125. public function getTransportOptions()
  126. {
  127. return $this->transportOptions;
  128. }
  129. /**
  130. * Configures the list of options to download package dist files
  131. *
  132. * @param array $options
  133. */
  134. public function setTransportOptions(array $options)
  135. {
  136. $this->transportOptions = $options;
  137. }
  138. /**
  139. * checks if this package is a platform package
  140. *
  141. * @return boolean
  142. */
  143. public function isPlatform()
  144. {
  145. return $this->getRepository() instanceof PlatformRepository;
  146. }
  147. /**
  148. * Returns package unique name, constructed from name, version and release type.
  149. *
  150. * @return string
  151. */
  152. public function getUniqueName()
  153. {
  154. return $this->getName().'-'.$this->getVersion();
  155. }
  156. public function equals(PackageInterface $package)
  157. {
  158. $self = $this;
  159. if ($this instanceof AliasPackage) {
  160. $self = $this->getAliasOf();
  161. }
  162. if ($package instanceof AliasPackage) {
  163. $package = $package->getAliasOf();
  164. }
  165. return $package === $self;
  166. }
  167. /**
  168. * Converts the package into a readable and unique string
  169. *
  170. * @return string
  171. */
  172. public function __toString()
  173. {
  174. return $this->getUniqueName();
  175. }
  176. public function getPrettyString()
  177. {
  178. return $this->getPrettyName().' '.$this->getPrettyVersion();
  179. }
  180. public function __clone()
  181. {
  182. $this->repository = null;
  183. $this->id = -1;
  184. }
  185. }