Rule.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. protected $pool;
  29. protected $disabled;
  30. protected $literals;
  31. protected $type;
  32. protected $id;
  33. protected $job;
  34. protected $ruleHash;
  35. public function __construct(Pool $pool, array $literals, $reason, $reasonData, $job = null)
  36. {
  37. $this->pool = $pool;
  38. // sort all packages ascending by id
  39. sort($literals);
  40. $this->literals = $literals;
  41. $this->reason = $reason;
  42. $this->reasonData = $reasonData;
  43. $this->disabled = false;
  44. $this->job = $job;
  45. $this->type = -1;
  46. $this->ruleHash = substr(md5(implode(',', $this->literals)), 0, 5);
  47. }
  48. public function getHash()
  49. {
  50. return $this->ruleHash;
  51. }
  52. public function setId($id)
  53. {
  54. $this->id = $id;
  55. }
  56. public function getId()
  57. {
  58. return $this->id;
  59. }
  60. public function getJob()
  61. {
  62. return $this->job;
  63. }
  64. /**
  65. * Checks if this rule is equal to another one
  66. *
  67. * Ignores whether either of the rules is disabled.
  68. *
  69. * @param Rule $rule The rule to check against
  70. * @return bool Whether the rules are equal
  71. */
  72. public function equals(Rule $rule)
  73. {
  74. if ($this->ruleHash !== $rule->ruleHash) {
  75. return false;
  76. }
  77. if (count($this->literals) != count($rule->literals)) {
  78. return false;
  79. }
  80. for ($i = 0, $n = count($this->literals); $i < $n; $i++) {
  81. if ($this->literals[$i] !== $rule->literals[$i]) {
  82. return false;
  83. }
  84. }
  85. return true;
  86. }
  87. public function setType($type)
  88. {
  89. $this->type = $type;
  90. }
  91. public function getType()
  92. {
  93. return $this->type;
  94. }
  95. public function disable()
  96. {
  97. $this->disabled = true;
  98. }
  99. public function enable()
  100. {
  101. $this->disabled = false;
  102. }
  103. public function isDisabled()
  104. {
  105. return $this->disabled;
  106. }
  107. public function isEnabled()
  108. {
  109. return !$this->disabled;
  110. }
  111. public function getLiterals()
  112. {
  113. return $this->literals;
  114. }
  115. public function isAssertion()
  116. {
  117. return 1 === count($this->literals);
  118. }
  119. public function toHumanReadableString()
  120. {
  121. $ruleText = '';
  122. foreach ($this->literals as $i => $literal) {
  123. if ($i != 0) {
  124. $ruleText .= '|';
  125. }
  126. $ruleText .= $this->pool->literalToString($literal);
  127. }
  128. switch ($this->reason) {
  129. case self::RULE_INTERNAL_ALLOW_UPDATE:
  130. return $ruleText;
  131. case self::RULE_JOB_INSTALL:
  132. return "Install command rule ($ruleText)";
  133. case self::RULE_JOB_REMOVE:
  134. return "Remove command rule ($ruleText)";
  135. case self::RULE_PACKAGE_CONFLICT:
  136. $package1 = $this->pool->literalToPackage($this->literals[0]);
  137. $package2 = $this->pool->literalToPackage($this->literals[1]);
  138. return 'Package "'.$package1.'" conflicts with "'.$package2.'"';
  139. case self::RULE_PACKAGE_REQUIRES:
  140. $literals = $this->literals;
  141. $sourceLiteral = array_shift($literals);
  142. $sourcePackage = $this->pool->literalToPackage($sourceLiteral);
  143. $requires = array();
  144. foreach ($literals as $literal) {
  145. $requires[] = $this->pool->literalToPackage($literal);
  146. }
  147. $text = 'Package "'.$sourcePackage.'" contains the rule '.$this->reasonData.'. ';
  148. if ($requires) {
  149. $text .= 'Any of these packages satisfy the dependency: '.implode(', ', $requires).'.';
  150. } else {
  151. $text .= 'No package satisfies this dependency.';
  152. }
  153. return $text;
  154. case self::RULE_PACKAGE_OBSOLETES:
  155. return $ruleText;
  156. case self::RULE_INSTALLED_PACKAGE_OBSOLETES:
  157. return $ruleText;
  158. case self::RULE_PACKAGE_SAME_NAME:
  159. return $ruleText;
  160. case self::RULE_PACKAGE_IMPLICIT_OBSOLETES:
  161. return $ruleText;
  162. case self::RULE_LEARNED:
  163. return 'learned: '.$ruleText;
  164. case self::RULE_PACKAGE_ALIAS:
  165. return $ruleText;
  166. }
  167. }
  168. /**
  169. * Formats a rule as a string of the format (Literal1|Literal2|...)
  170. *
  171. * @return string
  172. */
  173. public function __toString()
  174. {
  175. $result = ($this->isDisabled()) ? 'disabled(' : '(';
  176. foreach ($this->literals as $i => $literal) {
  177. if ($i != 0) {
  178. $result .= '|';
  179. }
  180. $result .= $this->pool->literalToString($literal);
  181. }
  182. $result .= ')';
  183. return $result;
  184. }
  185. }