MultiConflictRule.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. * @param array $job The job this rule was created from
  27. */
  28. public function __construct(array $literals, $reason, $reasonData, $job = null)
  29. {
  30. parent::__construct($reason, $reasonData, $job);
  31. if (count($literals) < 3) {
  32. throw new \RuntimeException("multi conflict rule requires at least 3 literals");
  33. }
  34. // sort all packages ascending by id
  35. sort($literals);
  36. $this->literals = $literals;
  37. }
  38. public function getLiterals()
  39. {
  40. return $this->literals;
  41. }
  42. public function getHash()
  43. {
  44. $data = unpack('ihash', md5('c:'.implode(',', $this->literals), true));
  45. return $data['hash'];
  46. }
  47. /**
  48. * Checks if this rule is equal to another one
  49. *
  50. * Ignores whether either of the rules is disabled.
  51. *
  52. * @param Rule $rule The rule to check against
  53. * @return bool Whether the rules are equal
  54. */
  55. public function equals(Rule $rule)
  56. {
  57. if ($rule instanceof MultiConflictRule) {
  58. return $this->literals === $rule->getLiterals();
  59. }
  60. return false;
  61. }
  62. public function isAssertion()
  63. {
  64. return false;
  65. }
  66. public function disable()
  67. {
  68. throw new \RuntimeException("can't disable conflict rule");
  69. }
  70. /**
  71. * Formats a rule as a string of the format (Literal1|Literal2|...)
  72. *
  73. * @return string
  74. */
  75. public function __toString()
  76. {
  77. // TODO multi conflict?
  78. $result = $this->isDisabled() ? 'disabled(multi(' : '(multi(';
  79. foreach ($this->literals as $i => $literal) {
  80. if ($i != 0) {
  81. $result .= '|';
  82. }
  83. $result .= $literal;
  84. }
  85. $result .= '))';
  86. return $result;
  87. }
  88. }