Rule.php 8.4 KB

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