RuleSet.php 3.6 KB

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