BasePackage.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. protected $name;
  41. protected $prettyName;
  42. protected $repository;
  43. protected $id;
  44. protected $options;
  45. /**
  46. * All descendants' constructors should call this parent constructor
  47. *
  48. * @param string $name The package's name
  49. */
  50. public function __construct($name)
  51. {
  52. $this->prettyName = $name;
  53. $this->name = strtolower($name);
  54. $this->id = -1;
  55. $this->options = array();
  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. * {@inheritDoc}
  103. */
  104. public function setRepository(RepositoryInterface $repository)
  105. {
  106. if ($this->repository && $repository !== $this->repository) {
  107. throw new \LogicException('A package can only be added to one repository');
  108. }
  109. $this->repository = $repository;
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. public function getRepository()
  115. {
  116. return $this->repository;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getOptions()
  122. {
  123. return $this->options;
  124. }
  125. /**
  126. * {@inheritDoc}
  127. */
  128. public function setOptions(array $options)
  129. {
  130. $this->options = $options;
  131. }
  132. /**
  133. * checks if this package is a platform package
  134. *
  135. * @return boolean
  136. */
  137. public function isPlatform()
  138. {
  139. return $this->getRepository() instanceof PlatformRepository;
  140. }
  141. /**
  142. * Returns package unique name, constructed from name, version and release type.
  143. *
  144. * @return string
  145. */
  146. public function getUniqueName()
  147. {
  148. return $this->getName().'-'.$this->getVersion();
  149. }
  150. public function equals(PackageInterface $package)
  151. {
  152. $self = $this;
  153. if ($this instanceof AliasPackage) {
  154. $self = $this->getAliasOf();
  155. }
  156. if ($package instanceof AliasPackage) {
  157. $package = $package->getAliasOf();
  158. }
  159. return $package === $self;
  160. }
  161. /**
  162. * Converts the package into a readable and unique string
  163. *
  164. * @return string
  165. */
  166. public function __toString()
  167. {
  168. return $this->getUniqueName();
  169. }
  170. public function getPrettyString()
  171. {
  172. return $this->getPrettyName().' '.$this->getPrettyVersion();
  173. }
  174. public function __clone()
  175. {
  176. $this->repository = null;
  177. $this->id = -1;
  178. }
  179. }