RuleSet.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. $hash = $rule->getHash();
  50. // Do not add if rule already exists
  51. if (isset($this->rulesByHash[$hash])) {
  52. $potentialDuplicates = $this->rulesByHash[$hash];
  53. foreach ($potentialDuplicates as $potentialDuplicate) {
  54. if ($rule->equals($potentialDuplicate)) {
  55. return;
  56. }
  57. }
  58. }
  59. if (!isset($this->rules[$type])) {
  60. $this->rules[$type] = array();
  61. }
  62. $this->rules[$type][] = $rule;
  63. $this->ruleById[$this->nextRuleId] = $rule;
  64. $rule->setType($type);
  65. $this->nextRuleId++;
  66. if (!isset($this->rulesByHash[$hash])) {
  67. $this->rulesByHash[$hash] = array($rule);
  68. } else {
  69. $this->rulesByHash[$hash][] = $rule;
  70. }
  71. }
  72. public function count()
  73. {
  74. return $this->nextRuleId;
  75. }
  76. public function ruleById($id)
  77. {
  78. return $this->ruleById[$id];
  79. }
  80. public function getRules()
  81. {
  82. return $this->rules;
  83. }
  84. public function getIterator()
  85. {
  86. return new RuleSetIterator($this->getRules());
  87. }
  88. public function getIteratorFor($types)
  89. {
  90. if (!is_array($types)) {
  91. $types = array($types);
  92. }
  93. $allRules = $this->getRules();
  94. $rules = array();
  95. foreach ($types as $type) {
  96. $rules[$type] = $allRules[$type];
  97. }
  98. return new RuleSetIterator($rules);
  99. }
  100. public function getIteratorWithout($types)
  101. {
  102. if (!is_array($types)) {
  103. $types = array($types);
  104. }
  105. $rules = $this->getRules();
  106. foreach ($types as $type) {
  107. unset($rules[$type]);
  108. }
  109. return new RuleSetIterator($rules);
  110. }
  111. public function getTypes()
  112. {
  113. $types = self::$types;
  114. unset($types[255]);
  115. return array_keys($types);
  116. }
  117. public function getPrettyString(Pool $pool = null)
  118. {
  119. $string = "\n";
  120. foreach ($this->rules as $type => $rules) {
  121. $string .= str_pad(self::$types[$type], 8, ' ') . ": ";
  122. foreach ($rules as $rule) {
  123. $string .= ($pool ? $rule->getPrettyString($pool) : $rule)."\n";
  124. }
  125. $string .= "\n\n";
  126. }
  127. return $string;
  128. }
  129. public function __toString()
  130. {
  131. return $this->getPrettyString(null);
  132. }
  133. }