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