RuleSet.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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
  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_WEAK = 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_WEAK => 'WEAK',
  31. self::TYPE_LEARNED => 'LEARNED',
  32. );
  33. protected $rules;
  34. public function __construct()
  35. {
  36. foreach ($this->getTypes() as $type) {
  37. $this->rules[$type] = array();
  38. }
  39. }
  40. public function add(Rule $rule, $type)
  41. {
  42. if (!isset(self::$types[$type]))
  43. {
  44. throw OutOfBoundsException('Unknown rule type: ' . $type);
  45. }
  46. if (!isset($this->rules[$type]))
  47. {
  48. $this->rules[$type] = array();
  49. }
  50. $this->rules[$type][] = $rule;
  51. $rule->setType($type);
  52. }
  53. public function getRules()
  54. {
  55. return $this->rules;
  56. }
  57. public function getIterator()
  58. {
  59. return new RuleSetIterator($this->getRules());
  60. }
  61. public function getIteratorFor($types)
  62. {
  63. if (!is_array($types))
  64. {
  65. $types = array($types);
  66. }
  67. $allRules = $this->getRules();
  68. $rules = array();
  69. foreach ($types as $type)
  70. {
  71. $rules[$type] = $allRules[$type];
  72. }
  73. return new RuleSetIterator($rules);
  74. }
  75. public function getIteratorWithout($types)
  76. {
  77. if (!is_array($types))
  78. {
  79. $types = array($types);
  80. }
  81. $rules = $this->getRules();
  82. foreach ($types as $type)
  83. {
  84. unset($rules[$type]);
  85. }
  86. return new RuleSetIterator($rules);
  87. }
  88. public function getTypes()
  89. {
  90. $types = self::$types;
  91. unset($types[-1]);
  92. return array_keys($types);
  93. }
  94. public function __toString()
  95. {
  96. $string = "\n";
  97. foreach ($this->rules as $type => $rules) {
  98. $string .= str_pad(self::$types[$type], 8, ' ') . ": ";
  99. foreach ($rules as $rule) {
  100. $string .= $rule;
  101. }
  102. $string .= "\n";
  103. }
  104. return $string;
  105. }
  106. }