RuleSet.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_FEATURE = 3;
  21. const TYPE_CHOICE = 4;
  22. const TYPE_LEARNED = 5;
  23. protected static $types = array(
  24. -1 => 'UNKNOWN',
  25. self::TYPE_PACKAGE => 'PACKAGE',
  26. self::TYPE_FEATURE => 'FEATURE',
  27. self::TYPE_JOB => 'JOB',
  28. self::TYPE_CHOICE => 'CHOICE',
  29. self::TYPE_LEARNED => 'LEARNED',
  30. );
  31. protected $rules;
  32. protected $ruleById;
  33. protected $nextRuleId;
  34. protected $rulesByHash;
  35. public function __construct()
  36. {
  37. $this->nextRuleId = 0;
  38. foreach ($this->getTypes() as $type) {
  39. $this->rules[$type] = array();
  40. }
  41. $this->rulesByHash = array();
  42. }
  43. public function add(Rule $rule, $type)
  44. {
  45. if (!isset(self::$types[$type])) {
  46. throw new \OutOfBoundsException('Unknown rule type: ' . $type);
  47. }
  48. if (!isset($this->rules[$type])) {
  49. $this->rules[$type] = array();
  50. }
  51. $this->rules[$type][] = $rule;
  52. $this->ruleById[$this->nextRuleId] = $rule;
  53. $rule->setType($type);
  54. $rule->setId($this->nextRuleId);
  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. {
  83. $types = array($types);
  84. }
  85. $allRules = $this->getRules();
  86. $rules = array();
  87. foreach ($types as $type)
  88. {
  89. $rules[$type] = $allRules[$type];
  90. }
  91. return new RuleSetIterator($rules);
  92. }
  93. public function getIteratorWithout($types)
  94. {
  95. if (!is_array($types))
  96. {
  97. $types = array($types);
  98. }
  99. $rules = $this->getRules();
  100. foreach ($types as $type)
  101. {
  102. unset($rules[$type]);
  103. }
  104. return new RuleSetIterator($rules);
  105. }
  106. public function getTypes()
  107. {
  108. $types = self::$types;
  109. unset($types[-1]);
  110. return array_keys($types);
  111. }
  112. public function containsEqual($rule)
  113. {
  114. if (isset($this->rulesByHash[$rule->getHash()])) {
  115. $potentialDuplicates = $this->rulesByHash[$rule->getHash()];
  116. foreach ($potentialDuplicates as $potentialDuplicate) {
  117. if ($rule->equals($potentialDuplicate)) {
  118. return true;
  119. }
  120. }
  121. }
  122. return false;
  123. }
  124. public function __toString()
  125. {
  126. $string = "\n";
  127. foreach ($this->rules as $type => $rules) {
  128. $string .= str_pad(self::$types[$type], 8, ' ') . ": ";
  129. foreach ($rules as $rule) {
  130. $string .= $rule."\n";
  131. }
  132. $string .= "\n\n";
  133. }
  134. return $string;
  135. }
  136. }