RuleSet.php 3.5 KB

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