* Jordi Boggiano * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Composer\DependencyResolver; use Composer\Repository\RepositorySet; /** * @author Nils Adermann * @implements \IteratorAggregate */ class RuleSet implements \IteratorAggregate, \Countable { // highest priority => lowest number public const TYPE_PACKAGE = 0; public const TYPE_REQUEST = 1; public const TYPE_LEARNED = 4; /** * READ-ONLY: Lookup table for rule id to rule object * * @var array */ public $ruleById = array(); /** @var array<0|1|4, string> */ protected static $types = array( self::TYPE_PACKAGE => 'PACKAGE', self::TYPE_REQUEST => 'REQUEST', self::TYPE_LEARNED => 'LEARNED', ); /** @var array */ protected $rules; /** @var int */ protected $nextRuleId = 0; /** @var array */ protected $rulesByHash = array(); public function __construct() { foreach ($this->getTypes() as $type) { $this->rules[$type] = array(); } } /** * @param self::TYPE_* $type * @return void */ public function add(Rule $rule, $type): void { if (!isset(self::$types[$type])) { throw new \OutOfBoundsException('Unknown rule type: ' . $type); } $hash = $rule->getHash(); // Do not add if rule already exists if (isset($this->rulesByHash[$hash])) { $potentialDuplicates = $this->rulesByHash[$hash]; if (\is_array($potentialDuplicates)) { foreach ($potentialDuplicates as $potentialDuplicate) { if ($rule->equals($potentialDuplicate)) { return; } } } else { if ($rule->equals($potentialDuplicates)) { return; } } } if (!isset($this->rules[$type])) { $this->rules[$type] = array(); } $this->rules[$type][] = $rule; $this->ruleById[$this->nextRuleId] = $rule; $rule->setType($type); $this->nextRuleId++; if (!isset($this->rulesByHash[$hash])) { $this->rulesByHash[$hash] = $rule; } elseif (\is_array($this->rulesByHash[$hash])) { $this->rulesByHash[$hash][] = $rule; } else { $originalRule = $this->rulesByHash[$hash]; $this->rulesByHash[$hash] = array($originalRule, $rule); } } public function count(): int { return $this->nextRuleId; } /** * @param int $id * @return Rule */ public function ruleById(int $id): Rule { return $this->ruleById[$id]; } /** @return array */ public function getRules(): array { return $this->rules; } public function getIterator(): RuleSetIterator { return new RuleSetIterator($this->getRules()); } /** * @param self::TYPE_*|array $types * @return RuleSetIterator */ public function getIteratorFor($types): RuleSetIterator { if (!\is_array($types)) { $types = array($types); } $allRules = $this->getRules(); /** @var array $rules */ $rules = array(); foreach ($types as $type) { $rules[$type] = $allRules[$type]; } return new RuleSetIterator($rules); } /** * @param array|self::TYPE_* $types * @return RuleSetIterator */ public function getIteratorWithout($types): RuleSetIterator { if (!\is_array($types)) { $types = array($types); } $rules = $this->getRules(); foreach ($types as $type) { unset($rules[$type]); } return new RuleSetIterator($rules); } /** @return array{0: 0, 1: 1, 2: 4} */ public function getTypes(): array { $types = self::$types; return array_keys($types); } /** * @param bool $isVerbose * @return string */ public function getPrettyString(RepositorySet $repositorySet = null, Request $request = null, Pool $pool = null, bool $isVerbose = false): string { $string = "\n"; foreach ($this->rules as $type => $rules) { $string .= str_pad(self::$types[$type], 8, ' ') . ": "; foreach ($rules as $rule) { $string .= ($repositorySet && $request && $pool ? $rule->getPrettyString($repositorySet, $request, $pool, $isVerbose) : $rule)."\n"; } $string .= "\n\n"; } return $string; } public function __toString(): string { return $this->getPrettyString(); } }