Rule.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. /**
  13. * @author Nils Adermann <naderman@naderman.de>
  14. */
  15. class Rule
  16. {
  17. const RULE_INTERNAL_ALLOW_UPDATE = 1;
  18. const RULE_JOB_INSTALL = 2;
  19. const RULE_JOB_REMOVE = 3;
  20. const RULE_PACKAGE_CONFLICT = 6;
  21. const RULE_PACKAGE_REQUIRES = 7;
  22. const RULE_PACKAGE_OBSOLETES = 8;
  23. const RULE_INSTALLED_PACKAGE_OBSOLETES = 9;
  24. const RULE_PACKAGE_SAME_NAME = 10;
  25. const RULE_PACKAGE_IMPLICIT_OBSOLETES = 11;
  26. const RULE_LEARNED = 12;
  27. const RULE_PACKAGE_ALIAS = 13;
  28. const BITFIELD_TYPE = 0;
  29. const BITFIELD_REASON = 8;
  30. const BITFIELD_DISABLED = 16;
  31. /**
  32. * READ-ONLY: The literals this rule consists of.
  33. * @var array
  34. */
  35. public $literals;
  36. protected $bitfield;
  37. protected $reasonData;
  38. public function __construct(array $literals, $reason, $reasonData, $job = null)
  39. {
  40. // sort all packages ascending by id
  41. sort($literals);
  42. $this->literals = $literals;
  43. $this->reasonData = $reasonData;
  44. if ($job) {
  45. $this->job = $job;
  46. }
  47. $this->bitfield = (0 << self::BITFIELD_DISABLED) |
  48. ($reason << self::BITFIELD_REASON) |
  49. (255 << self::BITFIELD_TYPE);
  50. }
  51. public function getHash()
  52. {
  53. $data = unpack('ihash', md5(implode(',', $this->literals), true));
  54. return $data['hash'];
  55. }
  56. public function getJob()
  57. {
  58. return isset($this->job) ? $this->job : null;
  59. }
  60. public function getReason()
  61. {
  62. return ($this->bitfield & (255 << self::BITFIELD_REASON)) >> self::BITFIELD_REASON;
  63. }
  64. public function getReasonData()
  65. {
  66. return $this->reasonData;
  67. }
  68. public function getRequiredPackage()
  69. {
  70. if ($this->getReason() === self::RULE_JOB_INSTALL) {
  71. return $this->reasonData;
  72. }
  73. if ($this->getReason() === self::RULE_PACKAGE_REQUIRES) {
  74. return $this->reasonData->getTarget();
  75. }
  76. }
  77. /**
  78. * Checks if this rule is equal to another one
  79. *
  80. * Ignores whether either of the rules is disabled.
  81. *
  82. * @param Rule $rule The rule to check against
  83. * @return bool Whether the rules are equal
  84. */
  85. public function equals(Rule $rule)
  86. {
  87. if (count($this->literals) != count($rule->literals)) {
  88. return false;
  89. }
  90. for ($i = 0, $n = count($this->literals); $i < $n; $i++) {
  91. if ($this->literals[$i] !== $rule->literals[$i]) {
  92. return false;
  93. }
  94. }
  95. return true;
  96. }
  97. public function setType($type)
  98. {
  99. $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_TYPE)) | ((255 & $type) << self::BITFIELD_TYPE);
  100. }
  101. public function getType()
  102. {
  103. return ($this->bitfield & (255 << self::BITFIELD_TYPE)) >> self::BITFIELD_TYPE;
  104. }
  105. public function disable()
  106. {
  107. $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_DISABLED)) | (1 << self::BITFIELD_DISABLED);
  108. }
  109. public function enable()
  110. {
  111. $this->bitfield = $this->bitfield & ~(255 << self::BITFIELD_DISABLED);
  112. }
  113. public function isDisabled()
  114. {
  115. return (bool) (($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
  116. }
  117. public function isEnabled()
  118. {
  119. return !(($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
  120. }
  121. /**
  122. * @deprecated Use public literals member
  123. */
  124. public function getLiterals()
  125. {
  126. return $this->literals;
  127. }
  128. public function isAssertion()
  129. {
  130. return 1 === count($this->literals);
  131. }
  132. public function getPrettyString(Pool $pool, array $installedMap = array())
  133. {
  134. $ruleText = '';
  135. foreach ($this->literals as $i => $literal) {
  136. if ($i != 0) {
  137. $ruleText .= '|';
  138. }
  139. $ruleText .= $pool->literalToPrettyString($literal, $installedMap);
  140. }
  141. switch ($this->getReason()) {
  142. case self::RULE_INTERNAL_ALLOW_UPDATE:
  143. return $ruleText;
  144. case self::RULE_JOB_INSTALL:
  145. return "Install command rule ($ruleText)";
  146. case self::RULE_JOB_REMOVE:
  147. return "Remove command rule ($ruleText)";
  148. case self::RULE_PACKAGE_CONFLICT:
  149. $package1 = $pool->literalToPackage($this->literals[0]);
  150. $package2 = $pool->literalToPackage($this->literals[1]);
  151. return $package1->getPrettyString().' conflicts with '.$this->formatPackagesUnique($pool, array($package2)).'.';
  152. case self::RULE_PACKAGE_REQUIRES:
  153. $literals = $this->literals;
  154. $sourceLiteral = array_shift($literals);
  155. $sourcePackage = $pool->literalToPackage($sourceLiteral);
  156. $requires = array();
  157. foreach ($literals as $literal) {
  158. $requires[] = $pool->literalToPackage($literal);
  159. }
  160. $text = $this->reasonData->getPrettyString($sourcePackage);
  161. if ($requires) {
  162. $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires) . '.';
  163. } else {
  164. $targetName = $this->reasonData->getTarget();
  165. // handle php extensions
  166. if ($targetName === 'php' || $targetName === 'php-64bit' || $targetName === 'hhvm') {
  167. if (defined('HHVM_VERSION')) {
  168. $text .= ' -> your HHVM version does not satisfy that requirement.';
  169. } elseif ($targetName === 'hhvm') {
  170. $text .= ' -> you are running this with PHP and not HHVM.';
  171. } else {
  172. $text .= ' -> your PHP version ('. phpversion().') does not satisfy that requirement.';
  173. }
  174. } elseif (0 === strpos($targetName, 'ext-')) {
  175. $ext = substr($targetName, 4);
  176. $error = extension_loaded($ext) ? 'has the wrong version ('.(phpversion($ext) ?: '0').') installed' : 'is missing from your system';
  177. $text .= ' -> the requested PHP extension '.$ext.' '.$error.'.';
  178. } elseif (0 === strpos($targetName, 'lib-')) {
  179. // handle linked libs
  180. $lib = substr($targetName, 4);
  181. $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.';
  182. } else {
  183. $text .= ' -> no matching package found.';
  184. }
  185. }
  186. return $text;
  187. case self::RULE_PACKAGE_OBSOLETES:
  188. return $ruleText;
  189. case self::RULE_INSTALLED_PACKAGE_OBSOLETES:
  190. return $ruleText;
  191. case self::RULE_PACKAGE_SAME_NAME:
  192. return 'Can only install one of: ' . $this->formatPackagesUnique($pool, $this->literals) . '.';
  193. case self::RULE_PACKAGE_IMPLICIT_OBSOLETES:
  194. return $ruleText;
  195. case self::RULE_LEARNED:
  196. return 'Conclusion: '.$ruleText;
  197. case self::RULE_PACKAGE_ALIAS:
  198. return $ruleText;
  199. default:
  200. return '('.$ruleText.')';
  201. }
  202. }
  203. protected function formatPackagesUnique($pool, array $packages)
  204. {
  205. $prepared = array();
  206. foreach ($packages as $package) {
  207. if (!is_object($package)) {
  208. $package = $pool->literalToPackage($package);
  209. }
  210. $prepared[$package->getName()]['name'] = $package->getPrettyName();
  211. $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
  212. }
  213. foreach ($prepared as $name => $package) {
  214. $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
  215. }
  216. return implode(', ', $prepared);
  217. }
  218. /**
  219. * Formats a rule as a string of the format (Literal1|Literal2|...)
  220. *
  221. * @return string
  222. */
  223. public function __toString()
  224. {
  225. $result = ($this->isDisabled()) ? 'disabled(' : '(';
  226. foreach ($this->literals as $i => $literal) {
  227. if ($i != 0) {
  228. $result .= '|';
  229. }
  230. $result .= $literal;
  231. }
  232. $result .= ')';
  233. return $result;
  234. }
  235. }