RuleSet.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 RuleSet implements \IteratorAggregate, \Countable
  16. {
  17. // highest priority => lowest number
  18. const TYPE_PACKAGE = 0;
  19. const TYPE_JOB = 1;
  20. const TYPE_LEARNED = 4;
  21. /**
  22. * READ-ONLY: Lookup table for rule id to rule object
  23. *
  24. * @var Rule[]
  25. */
  26. public $ruleById;
  27. protected static $types = array(
  28. 255 => 'UNKNOWN',
  29. self::TYPE_PACKAGE => 'PACKAGE',
  30. self::TYPE_JOB => 'JOB',
  31. self::TYPE_LEARNED => 'LEARNED',
  32. );
  33. protected $rules;
  34. protected $nextRuleId;
  35. protected $rulesByHash;
  36. public function __construct()
  37. {
  38. $this->nextRuleId = 0;
  39. foreach ($this->getTypes() as $type) {
  40. $this->rules[$type] = array();
  41. }
  42. $this->rulesByHash = array();
  43. }
  44. public function add(Rule $rule, $type)
  45. {
  46. if (!isset(self::$types[$type])) {
  47. throw new \OutOfBoundsException('Unknown rule type: ' . $type);
  48. }
  49. if (!isset($this->rules[$type])) {
  50. $this->rules[$type] = array();
  51. }
  52. $this->rules[$type][] = $rule;
  53. $this->ruleById[$this->nextRuleId] = $rule;
  54. $rule->setType($type);
  55. $this->nextRuleId++;
  56. $hash = $rule->getHash();
  57. if (!isset($this->rulesByHash[$hash])) {
  58. $this->rulesByHash[$hash] = array($rule);
  59. } else {
  60. $this->rulesByHash[$hash][] = $rule;
  61. }
  62. }
  63. public function count()
  64. {
  65. return $this->nextRuleId;
  66. }
  67. public function ruleById($id)
  68. {
  69. return $this->ruleById[$id];
  70. }
  71. public function getRules()
  72. {
  73. return $this->rules;
  74. }
  75. public function getIterator()
  76. {
  77. return new RuleSetIterator($this->getRules());
  78. }
  79. public function getIteratorFor($types)
  80. {
  81. if (!is_array($types)) {
  82. $types = array($types);
  83. }
  84. $allRules = $this->getRules();
  85. $rules = array();
  86. foreach ($types as $type) {
  87. $rules[$type] = $allRules[$type];
  88. }
  89. return new RuleSetIterator($rules);
  90. }
  91. public function getIteratorWithout($types)
  92. {
  93. if (!is_array($types)) {
  94. $types = array($types);
  95. }
  96. $rules = $this->getRules();
  97. foreach ($types as $type) {
  98. unset($rules[$type]);
  99. }
  100. return new RuleSetIterator($rules);
  101. }
  102. public function getTypes()
  103. {
  104. $types = self::$types;
  105. unset($types[255]);
  106. return array_keys($types);
  107. }
  108. public function containsEqual($rule)
  109. {
  110. if (isset($this->rulesByHash[$rule->getHash()])) {
  111. $potentialDuplicates = $this->rulesByHash[$rule->getHash()];
  112. foreach ($potentialDuplicates as $potentialDuplicate) {
  113. if ($rule->equals($potentialDuplicate)) {
  114. return true;
  115. }
  116. }
  117. }
  118. return false;
  119. }
  120. public function getPrettyString(Pool $pool = null)
  121. {
  122. $string = "\n";
  123. foreach ($this->rules as $type => $rules) {
  124. $string .= str_pad(self::$types[$type], 8, ' ') . ": ";
  125. foreach ($rules as $rule) {
  126. $string .= ($pool ? $rule->getPrettyString($pool) : $rule)."\n";
  127. }
  128. $string .= "\n\n";
  129. }
  130. return $string;
  131. }
  132. public function __toString()
  133. {
  134. return $this->getPrettyString(null);
  135. }
  136. }