RuleWatchGraph.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 RuleWatchGraph
  16. {
  17. protected $watches = array();
  18. /**
  19. * Alters watch chains for a rule.
  20. *
  21. */
  22. public function insert(RuleWatchNode $node)
  23. {
  24. if ($node->getRule()->isAssertion()) {
  25. return;
  26. }
  27. foreach (array($node->watch1, $node->watch2) as $literal) {
  28. if (!isset($this->watches[$literal])) {
  29. $this->watches[$literal] = new RuleWatchChain;
  30. }
  31. $this->watches[$literal]->unshift($node);
  32. }
  33. }
  34. public function contains($literalId)
  35. {
  36. return isset($this->watches[$literalId]);
  37. }
  38. public function walkLiteral($literalId, $level, $skipCallback, $conflictCallback, $decideCallback)
  39. {
  40. if (!isset($this->watches[$literalId])) {
  41. return null;
  42. }
  43. $this->watches[$literalId]->rewind();
  44. while ($this->watches[$literalId]->valid()) {
  45. $node = $this->watches[$literalId]->current();
  46. $otherWatch = $node->getOtherWatch($literalId);
  47. if (!$node->getRule()->isDisabled() && !call_user_func($skipCallback, $otherWatch)) {
  48. $ruleLiterals = $node->getRule()->getLiterals();
  49. foreach ($ruleLiterals as $ruleLiteral) {
  50. $ruleLiteralId = $ruleLiteral->getId();
  51. if ($literalId !== $ruleLiteralId &&
  52. $otherWatch !== $ruleLiteralId &&
  53. !call_user_func($conflictCallback, $ruleLiteralId)) {
  54. $this->moveWatch($literalId, $ruleLiteralId, $node);
  55. continue 2;
  56. }
  57. }
  58. if (call_user_func($conflictCallback, $otherWatch)) {
  59. return $node->getRule();
  60. }
  61. call_user_func($decideCallback, $otherWatch, $level, $node->getRule());
  62. }
  63. $this->watches[$literalId]->next();
  64. }
  65. return null;
  66. }
  67. protected function moveWatch($fromLiteral, $toLiteral, $node)
  68. {
  69. if (!isset($this->watches[$toLiteral])) {
  70. $this->watches[$toLiteral] = new RuleWatchChain;
  71. }
  72. $node->moveWatch($fromLiteral, $toLiteral);
  73. $this->watches[$fromLiteral]->remove();
  74. $this->watches[$toLiteral]->unshift($node);
  75. }
  76. }