Rule.php 10 KB

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