BasePackage.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /**
  16. * Base class for packages providing name storage and default match implementation
  17. *
  18. * @author Nils Adermann <naderman@naderman.de>
  19. */
  20. abstract class BasePackage implements PackageInterface
  21. {
  22. protected $name;
  23. protected $repository;
  24. protected $id;
  25. /**
  26. * All descendents' constructors should call this parent constructor
  27. *
  28. * @param string $name The package's name
  29. */
  30. public function __construct($name)
  31. {
  32. $this->name = strtolower($name);
  33. $this->id = -1;
  34. }
  35. /**
  36. * Returns the package's name without version info, thus not a unique identifier
  37. *
  38. * @return string package name
  39. */
  40. public function getName()
  41. {
  42. return $this->name;
  43. }
  44. /**
  45. * Returns a set of names that could refer to this package
  46. *
  47. * No version or release type information should be included in any of the
  48. * names. Provided or replaced package names need to be returned as well.
  49. *
  50. * @return array An array of strings refering to this package
  51. */
  52. public function getNames()
  53. {
  54. $names = array(
  55. $this->getName(),
  56. );
  57. foreach ($this->getProvides() as $link) {
  58. $names[] = $link->getTarget();
  59. }
  60. foreach ($this->getReplaces() as $link) {
  61. $names[] = $link->getTarget();
  62. }
  63. return $names;
  64. }
  65. /**
  66. * {@inheritDoc}
  67. */
  68. public function setId($id)
  69. {
  70. $this->id = $id;
  71. }
  72. /**
  73. * {@inheritDoc}
  74. */
  75. public function getId()
  76. {
  77. return $this->id;
  78. }
  79. /**
  80. * Checks if the package matches the given constraint directly or through
  81. * provided or replaced packages
  82. *
  83. * @param string $name Name of the package to be matched
  84. * @param LinkConstraintInterface $constraint The constraint to verify
  85. * @return bool Whether this package matches the name and constraint
  86. */
  87. public function matches($name, LinkConstraintInterface $constraint)
  88. {
  89. if ($this->name === $name) {
  90. return $constraint->matches(new VersionConstraint('==', $this->getVersion(), $this->getReleaseType()));
  91. }
  92. foreach ($this->getProvides() as $link) {
  93. if ($link->getTarget() === $name) {
  94. return $constraint->matches($link->getConstraint());
  95. }
  96. }
  97. foreach ($this->getReplaces() as $link) {
  98. if ($link->getTarget() === $name) {
  99. return $constraint->matches($link->getConstraint());
  100. }
  101. }
  102. return false;
  103. }
  104. public function getRepository()
  105. {
  106. return $this->repository;
  107. }
  108. public function setRepository(RepositoryInterface $repository)
  109. {
  110. if ($this->repository) {
  111. throw new \LogicException('A package can only be added to one repository');
  112. }
  113. $this->repository = $repository;
  114. }
  115. /**
  116. * Converts the package into a readable and unique string
  117. *
  118. * @return string
  119. */
  120. public function __toString()
  121. {
  122. return $this->getName().'-'.$this->getVersion().'-'.$this->getReleaseType();
  123. }
  124. /**
  125. * Parses a version string and returns an array with the version, its type (alpha, beta, RC, stable) and a dev flag (for development branches tracking)
  126. *
  127. * @param string $version
  128. * @return array
  129. */
  130. public static function parseVersion($version)
  131. {
  132. if (!preg_match('#^v?(\d+)(\.\d+)?(\.\d+)?-?((?:beta|RC|alpha)\d*)?-?(dev)?$#i', $version, $matches)) {
  133. throw new \UnexpectedValueException('Invalid version string '.$version);
  134. }
  135. return array(
  136. 'version' => $matches[1]
  137. .(!empty($matches[2]) ? $matches[2] : '.0')
  138. .(!empty($matches[3]) ? $matches[3] : '.0'),
  139. 'type' => strtolower(!empty($matches[4]) ? $matches[4] : 'stable'),
  140. 'dev' => !empty($matches[5]),
  141. );
  142. }
  143. }