Rule.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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\DependencyResolver;
  12. use Composer\Package\CompletePackage;
  13. use Composer\Package\Link;
  14. use Composer\Package\PackageInterface;
  15. /**
  16. * @author Nils Adermann <naderman@naderman.de>
  17. * @author Ruben Gonzalez <rubenrua@gmail.com>
  18. */
  19. abstract class Rule
  20. {
  21. // reason constants
  22. const RULE_INTERNAL_ALLOW_UPDATE = 1;
  23. const RULE_JOB_INSTALL = 2;
  24. const RULE_JOB_REMOVE = 3;
  25. const RULE_PACKAGE_CONFLICT = 6;
  26. const RULE_PACKAGE_REQUIRES = 7;
  27. const RULE_PACKAGE_OBSOLETES = 8;
  28. const RULE_INSTALLED_PACKAGE_OBSOLETES = 9;
  29. const RULE_PACKAGE_SAME_NAME = 10;
  30. const RULE_PACKAGE_IMPLICIT_OBSOLETES = 11;
  31. const RULE_LEARNED = 12;
  32. const RULE_PACKAGE_ALIAS = 13;
  33. // bitfield defs
  34. const BITFIELD_TYPE = 0;
  35. const BITFIELD_REASON = 8;
  36. const BITFIELD_DISABLED = 16;
  37. protected $bitfield;
  38. protected $job;
  39. protected $reasonData;
  40. /**
  41. * @param int $reason A RULE_* constant describing the reason for generating this rule
  42. * @param Link|PackageInterface $reasonData
  43. * @param array $job The job this rule was created from
  44. */
  45. public function __construct($reason, $reasonData, $job = null)
  46. {
  47. $this->reasonData = $reasonData;
  48. if ($job) {
  49. $this->job = $job;
  50. }
  51. $this->bitfield = (0 << self::BITFIELD_DISABLED) |
  52. ($reason << self::BITFIELD_REASON) |
  53. (255 << self::BITFIELD_TYPE);
  54. }
  55. abstract public function getLiterals();
  56. abstract public function getHash();
  57. public function getJob()
  58. {
  59. return isset($this->job) ? $this->job : null;
  60. }
  61. abstract public function equals(Rule $rule);
  62. public function getReason()
  63. {
  64. return ($this->bitfield & (255 << self::BITFIELD_REASON)) >> self::BITFIELD_REASON;
  65. }
  66. public function getReasonData()
  67. {
  68. return $this->reasonData;
  69. }
  70. public function getRequiredPackage()
  71. {
  72. if ($this->getReason() === self::RULE_JOB_INSTALL) {
  73. return $this->reasonData;
  74. }
  75. if ($this->getReason() === self::RULE_PACKAGE_REQUIRES) {
  76. return $this->reasonData->getTarget();
  77. }
  78. }
  79. public function setType($type)
  80. {
  81. $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_TYPE)) | ((255 & $type) << self::BITFIELD_TYPE);
  82. }
  83. public function getType()
  84. {
  85. return ($this->bitfield & (255 << self::BITFIELD_TYPE)) >> self::BITFIELD_TYPE;
  86. }
  87. public function disable()
  88. {
  89. $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_DISABLED)) | (1 << self::BITFIELD_DISABLED);
  90. }
  91. public function enable()
  92. {
  93. $this->bitfield = $this->bitfield & ~(255 << self::BITFIELD_DISABLED);
  94. }
  95. public function isDisabled()
  96. {
  97. return (bool) (($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
  98. }
  99. public function isEnabled()
  100. {
  101. return !(($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
  102. }
  103. abstract public function isAssertion();
  104. public function getPrettyString(Pool $pool, array $installedMap = array())
  105. {
  106. $literals = $this->getLiterals();
  107. $ruleText = '';
  108. foreach ($literals as $i => $literal) {
  109. if ($i != 0) {
  110. $ruleText .= '|';
  111. }
  112. $ruleText .= $pool->literalToPrettyString($literal, $installedMap);
  113. }
  114. switch ($this->getReason()) {
  115. case self::RULE_INTERNAL_ALLOW_UPDATE:
  116. return $ruleText;
  117. case self::RULE_JOB_INSTALL:
  118. return "Install command rule ($ruleText)";
  119. case self::RULE_JOB_REMOVE:
  120. return "Remove command rule ($ruleText)";
  121. case self::RULE_PACKAGE_CONFLICT:
  122. $package1 = $pool->literalToPackage($literals[0]);
  123. $package2 = $pool->literalToPackage($literals[1]);
  124. return $package1->getPrettyString().' conflicts with '.$this->formatPackagesUnique($pool, array($package2)).'.';
  125. case self::RULE_PACKAGE_REQUIRES:
  126. $sourceLiteral = array_shift($literals);
  127. $sourcePackage = $pool->literalToPackage($sourceLiteral);
  128. $requires = array();
  129. foreach ($literals as $literal) {
  130. $requires[] = $pool->literalToPackage($literal);
  131. }
  132. $text = $this->reasonData->getPrettyString($sourcePackage);
  133. if ($requires) {
  134. $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires) . '.';
  135. } else {
  136. $targetName = $this->reasonData->getTarget();
  137. if ($targetName === 'php' || $targetName === 'php-64bit' || $targetName === 'hhvm') {
  138. // handle php/hhvm
  139. if (defined('HHVM_VERSION')) {
  140. return $text . ' -> your HHVM version does not satisfy that requirement.';
  141. }
  142. if ($targetName === 'hhvm') {
  143. return $text . ' -> you are running this with PHP and not HHVM.';
  144. }
  145. $packages = $pool->whatProvides($targetName);
  146. $package = count($packages) ? current($packages) : phpversion();
  147. if (!($package instanceof CompletePackage)) {
  148. return $text . ' -> your PHP version ('.phpversion().') does not satisfy that requirement.';
  149. }
  150. $extra = $package->getExtra();
  151. if (!empty($extra['config.platform'])) {
  152. $text .= ' -> your PHP version ('.phpversion().') overridden by "config.platform.php" version ('.$package->getPrettyVersion().') does not satisfy that requirement.';
  153. } else {
  154. $text .= ' -> your PHP version ('.$package->getPrettyVersion().') does not satisfy that requirement.';
  155. }
  156. return $text;
  157. }
  158. if (0 === strpos($targetName, 'ext-')) {
  159. // handle php extensions
  160. $ext = substr($targetName, 4);
  161. $error = extension_loaded($ext) ? 'has the wrong version ('.(phpversion($ext) ?: '0').') installed' : 'is missing from your system';
  162. return $text . ' -> the requested PHP extension '.$ext.' '.$error.'.';
  163. }
  164. if (0 === strpos($targetName, 'lib-')) {
  165. // handle linked libs
  166. $lib = substr($targetName, 4);
  167. return $text . ' -> the requested linked library '.$lib.' has the wrong version installed or is missing from your system, make sure to have the extension providing it.';
  168. }
  169. if ($providers = $pool->whatProvides($targetName, $this->reasonData->getConstraint(), true, true)) {
  170. return $text . ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $providers) .' but these conflict with your requirements or minimum-stability.';
  171. }
  172. return $text . ' -> no matching package found.';
  173. }
  174. return $text;
  175. case self::RULE_PACKAGE_OBSOLETES:
  176. return $ruleText;
  177. case self::RULE_INSTALLED_PACKAGE_OBSOLETES:
  178. return $ruleText;
  179. case self::RULE_PACKAGE_SAME_NAME:
  180. return 'Can only install one of: ' . $this->formatPackagesUnique($pool, $literals) . '.';
  181. case self::RULE_PACKAGE_IMPLICIT_OBSOLETES:
  182. return $ruleText;
  183. case self::RULE_LEARNED:
  184. return 'Conclusion: '.$ruleText;
  185. case self::RULE_PACKAGE_ALIAS:
  186. return $ruleText;
  187. default:
  188. return '('.$ruleText.')';
  189. }
  190. }
  191. /**
  192. * @param Pool $pool
  193. * @param array $packages
  194. *
  195. * @return string
  196. */
  197. protected function formatPackagesUnique($pool, array $packages)
  198. {
  199. $prepared = array();
  200. foreach ($packages as $package) {
  201. if (!is_object($package)) {
  202. $package = $pool->literalToPackage($package);
  203. }
  204. $prepared[$package->getName()]['name'] = $package->getPrettyName();
  205. $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
  206. }
  207. foreach ($prepared as $name => $package) {
  208. $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
  209. }
  210. return implode(', ', $prepared);
  211. }
  212. }