RuleSet.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. use Composer\Repository\RepositorySet;
  13. /**
  14. * @author Nils Adermann <naderman@naderman.de>
  15. */
  16. class RuleSet implements \IteratorAggregate, \Countable
  17. {
  18. // highest priority => lowest number
  19. const TYPE_PACKAGE = 0;
  20. const TYPE_REQUEST = 1;
  21. const TYPE_LEARNED = 4;
  22. /**
  23. * READ-ONLY: Lookup table for rule id to rule object
  24. *
  25. * @var Rule[]
  26. */
  27. public $ruleById;
  28. protected static $types = array(
  29. 255 => 'UNKNOWN',
  30. self::TYPE_PACKAGE => 'PACKAGE',
  31. self::TYPE_REQUEST => 'REQUEST',
  32. self::TYPE_LEARNED => 'LEARNED',
  33. );
  34. protected $rules;
  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. $hash = $rule->getHash();
  51. // Do not add if rule already exists
  52. if (isset($this->rulesByHash[$hash])) {
  53. $potentialDuplicates = $this->rulesByHash[$hash];
  54. if (is_array($potentialDuplicates)) {
  55. foreach ($potentialDuplicates as $potentialDuplicate) {
  56. if ($rule->equals($potentialDuplicate)) {
  57. return;
  58. }
  59. }
  60. } else {
  61. if ($rule->equals($potentialDuplicates)) {
  62. return;
  63. }
  64. }
  65. }
  66. if (!isset($this->rules[$type])) {
  67. $this->rules[$type] = array();
  68. }
  69. $this->rules[$type][] = $rule;
  70. $this->ruleById[$this->nextRuleId] = $rule;
  71. $rule->setType($type);
  72. $this->nextRuleId++;
  73. if (!isset($this->rulesByHash[$hash])) {
  74. $this->rulesByHash[$hash] = $rule;
  75. } elseif (is_array($this->rulesByHash[$hash])) {
  76. $this->rulesByHash[$hash][] = $rule;
  77. } else {
  78. $originalRule = $this->rulesByHash[$hash];
  79. $this->rulesByHash[$hash] = array($originalRule, $rule);
  80. }
  81. }
  82. public function count()
  83. {
  84. return $this->nextRuleId;
  85. }
  86. public function ruleById($id)
  87. {
  88. return $this->ruleById[$id];
  89. }
  90. public function getRules()
  91. {
  92. return $this->rules;
  93. }
  94. public function getIterator()
  95. {
  96. return new RuleSetIterator($this->getRules());
  97. }
  98. public function getIteratorFor($types)
  99. {
  100. if (!is_array($types)) {
  101. $types = array($types);
  102. }
  103. $allRules = $this->getRules();
  104. $rules = array();
  105. foreach ($types as $type) {
  106. $rules[$type] = $allRules[$type];
  107. }
  108. return new RuleSetIterator($rules);
  109. }
  110. public function getIteratorWithout($types)
  111. {
  112. if (!is_array($types)) {
  113. $types = array($types);
  114. }
  115. $rules = $this->getRules();
  116. foreach ($types as $type) {
  117. unset($rules[$type]);
  118. }
  119. return new RuleSetIterator($rules);
  120. }
  121. public function getTypes()
  122. {
  123. $types = self::$types;
  124. unset($types[255]);
  125. return array_keys($types);
  126. }
  127. public function getPrettyString(RepositorySet $repositorySet = null, Request $request = null, Pool $pool = null)
  128. {
  129. $string = "\n";
  130. foreach ($this->rules as $type => $rules) {
  131. $string .= str_pad(self::$types[$type], 8, ' ') . ": ";
  132. foreach ($rules as $rule) {
  133. $string .= ($repositorySet && $request && $pool ? $rule->getPrettyString($repositorySet, $request, $pool) : $rule)."\n";
  134. }
  135. $string .= "\n\n";
  136. }
  137. return $string;
  138. }
  139. public function __toString()
  140. {
  141. return $this->getPrettyString(null, null, null);
  142. }
  143. }