BasePackage.php 4.1 KB

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