MultiConflictRule.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\PackageInterface;
  13. use Composer\Package\Link;
  14. /**
  15. * @author Nils Adermann <naderman@naderman.de>
  16. *
  17. * MultiConflictRule([A, B, C]) acts as Rule([-A, -B]), Rule([-A, -C]), Rule([-B, -C])
  18. */
  19. class MultiConflictRule extends Rule
  20. {
  21. protected $literals;
  22. /**
  23. * @param array $literals
  24. * @param int $reason A RULE_* constant describing the reason for generating this rule
  25. * @param Link|PackageInterface $reasonData
  26. */
  27. public function __construct(array $literals, $reason, $reasonData)
  28. {
  29. parent::__construct($reason, $reasonData);
  30. if (count($literals) < 3) {
  31. throw new \RuntimeException("multi conflict rule requires at least 3 literals");
  32. }
  33. // sort all packages ascending by id
  34. sort($literals);
  35. $this->literals = $literals;
  36. }
  37. public function getLiterals()
  38. {
  39. return $this->literals;
  40. }
  41. public function getHash()
  42. {
  43. $data = unpack('ihash', md5('c:'.implode(',', $this->literals), true));
  44. return $data['hash'];
  45. }
  46. /**
  47. * Checks if this rule is equal to another one
  48. *
  49. * Ignores whether either of the rules is disabled.
  50. *
  51. * @param Rule $rule The rule to check against
  52. * @return bool Whether the rules are equal
  53. */
  54. public function equals(Rule $rule)
  55. {
  56. if ($rule instanceof MultiConflictRule) {
  57. return $this->literals === $rule->getLiterals();
  58. }
  59. return false;
  60. }
  61. public function isAssertion()
  62. {
  63. return false;
  64. }
  65. public function disable()
  66. {
  67. throw new \RuntimeException("Disabling multi conflict rules is not possible. Please contact composer at https://github.com/composer/composer to let us debug what lead to this situation.");
  68. }
  69. /**
  70. * Formats a rule as a string of the format (Literal1|Literal2|...)
  71. *
  72. * @return string
  73. */
  74. public function __toString()
  75. {
  76. // TODO multi conflict?
  77. $result = $this->isDisabled() ? 'disabled(multi(' : '(multi(';
  78. foreach ($this->literals as $i => $literal) {
  79. if ($i != 0) {
  80. $result .= '|';
  81. }
  82. $result .= $literal;
  83. }
  84. $result .= '))';
  85. return $result;
  86. }
  87. }