Rule.php 9.5 KB

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