RuleSetGenerator.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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\Package\PackageInterface;
  13. use Composer\Package\AliasPackage;
  14. use Composer\Repository\PlatformRepository;
  15. /**
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. class RuleSetGenerator
  19. {
  20. protected $policy;
  21. protected $pool;
  22. protected $rules;
  23. protected $jobs;
  24. protected $installedMap;
  25. protected $addedMap;
  26. protected $conflictAddedMap;
  27. protected $addedPackages;
  28. protected $addedPackagesByNames;
  29. public function __construct(PolicyInterface $policy, Pool $pool)
  30. {
  31. $this->policy = $policy;
  32. $this->pool = $pool;
  33. }
  34. /**
  35. * Creates a new rule for the requirements of a package
  36. *
  37. * This rule is of the form (-A|B|C), where B and C are the providers of
  38. * one requirement of the package A.
  39. *
  40. * @param PackageInterface $package The package with a requirement
  41. * @param array $providers The providers of the requirement
  42. * @param int $reason A RULE_* constant describing the
  43. * reason for generating this rule
  44. * @param mixed $reasonData Any data, e.g. the requirement name,
  45. * that goes with the reason
  46. * @return Rule|null The generated rule or null if tautological
  47. */
  48. protected function createRequireRule(PackageInterface $package, array $providers, $reason, $reasonData = null)
  49. {
  50. $literals = array(-$package->id);
  51. foreach ($providers as $provider) {
  52. // self fulfilling rule?
  53. if ($provider === $package) {
  54. return null;
  55. }
  56. $literals[] = $provider->id;
  57. }
  58. return new GenericRule($literals, $reason, $reasonData);
  59. }
  60. /**
  61. * Creates a rule to install at least one of a set of packages
  62. *
  63. * The rule is (A|B|C) with A, B and C different packages. If the given
  64. * set of packages is empty an impossible rule is generated.
  65. *
  66. * @param array $packages The set of packages to choose from
  67. * @param int $reason A RULE_* constant describing the reason for
  68. * generating this rule
  69. * @param array $job The job this rule was created from
  70. * @return Rule The generated rule
  71. */
  72. protected function createInstallOneOfRule(array $packages, $reason, $job)
  73. {
  74. $literals = array();
  75. foreach ($packages as $package) {
  76. $literals[] = $package->id;
  77. }
  78. return new GenericRule($literals, $reason, $job['packageName'], $job);
  79. }
  80. /**
  81. * Creates a rule to remove a package
  82. *
  83. * The rule for a package A is (-A).
  84. *
  85. * @param PackageInterface $package The package to be removed
  86. * @param int $reason A RULE_* constant describing the
  87. * reason for generating this rule
  88. * @param array $job The job this rule was created from
  89. * @return Rule The generated rule
  90. */
  91. protected function createRemoveRule(PackageInterface $package, $reason, $job)
  92. {
  93. return new GenericRule(array(-$package->id), $reason, $job['packageName'], $job);
  94. }
  95. /**
  96. * Creates a rule for two conflicting packages
  97. *
  98. * The rule for conflicting packages A and B is (-A|-B). A is called the issuer
  99. * and B the provider.
  100. *
  101. * @param PackageInterface $issuer The package declaring the conflict
  102. * @param PackageInterface $provider The package causing the conflict
  103. * @param int $reason A RULE_* constant describing the
  104. * reason for generating this rule
  105. * @param mixed $reasonData Any data, e.g. the package name, that
  106. * goes with the reason
  107. * @return Rule|null The generated rule
  108. */
  109. protected function createRule2Literals(PackageInterface $issuer, PackageInterface $provider, $reason, $reasonData = null)
  110. {
  111. // ignore self conflict
  112. if ($issuer === $provider) {
  113. return null;
  114. }
  115. return new Rule2Literals(-$issuer->id, -$provider->id, $reason, $reasonData);
  116. }
  117. /**
  118. * Adds a rule unless it duplicates an existing one of any type
  119. *
  120. * To be able to directly pass in the result of one of the rule creation
  121. * methods null is allowed which will not insert a rule.
  122. *
  123. * @param int $type A TYPE_* constant defining the rule type
  124. * @param Rule $newRule The rule about to be added
  125. */
  126. private function addRule($type, Rule $newRule = null)
  127. {
  128. if (!$newRule) {
  129. return;
  130. }
  131. $this->rules->add($newRule, $type);
  132. }
  133. protected function addRulesForPackage(PackageInterface $package, $ignorePlatformReqs)
  134. {
  135. $workQueue = new \SplQueue;
  136. $workQueue->enqueue($package);
  137. while (!$workQueue->isEmpty()) {
  138. /** @var PackageInterface $package */
  139. $package = $workQueue->dequeue();
  140. if (isset($this->addedMap[$package->id])) {
  141. continue;
  142. }
  143. $this->addedMap[$package->id] = true;
  144. $this->addedPackages[] = $package;
  145. foreach ($package->getNames() as $name) {
  146. $this->addedPackagesByNames[$name][] = $package;
  147. }
  148. foreach ($package->getRequires() as $link) {
  149. if ($ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $link->getTarget())) {
  150. continue;
  151. }
  152. $possibleRequires = $this->pool->whatProvides($link->getTarget(), $link->getConstraint());
  153. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package, $possibleRequires, Rule::RULE_PACKAGE_REQUIRES, $link));
  154. foreach ($possibleRequires as $require) {
  155. $workQueue->enqueue($require);
  156. }
  157. }
  158. $packageName = $package->getName();
  159. $obsoleteProviders = $this->pool->whatProvides($packageName, null);
  160. foreach ($obsoleteProviders as $provider) {
  161. if ($provider === $package) {
  162. continue;
  163. }
  164. if (($package instanceof AliasPackage) && $package->getAliasOf() === $provider) {
  165. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRequireRule($package, array($provider), Rule::RULE_PACKAGE_ALIAS, $package));
  166. } elseif (!$this->obsoleteImpossibleForAlias($package, $provider)) {
  167. $reason = ($packageName == $provider->getName()) ? Rule::RULE_PACKAGE_SAME_NAME : Rule::RULE_PACKAGE_IMPLICIT_OBSOLETES;
  168. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRule2Literals($package, $provider, $reason, $package));
  169. }
  170. }
  171. }
  172. }
  173. protected function addConflictRules()
  174. {
  175. /** @var PackageInterface $package */
  176. foreach ($this->addedPackages as $package) {
  177. foreach ($package->getConflicts() as $link) {
  178. if (!isset($this->addedPackagesByNames[$link->getTarget()])) {
  179. continue;
  180. }
  181. /** @var PackageInterface $possibleConflict */
  182. foreach ($this->addedPackagesByNames[$link->getTarget()] as $possibleConflict) {
  183. $conflictMatch = $this->pool->match($possibleConflict, $link->getTarget(), $link->getConstraint(), true);
  184. if ($conflictMatch === Pool::MATCH || $conflictMatch === Pool::MATCH_REPLACE) {
  185. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRule2Literals($package, $possibleConflict, Rule::RULE_PACKAGE_CONFLICT, $link));
  186. }
  187. }
  188. }
  189. // check obsoletes and implicit obsoletes of a package
  190. $isInstalled = isset($this->installedMap[$package->id]);
  191. foreach ($package->getReplaces() as $link) {
  192. if (!isset($this->addedPackagesByNames[$link->getTarget()])) {
  193. continue;
  194. }
  195. /** @var PackageInterface $possibleConflict */
  196. foreach ($this->addedPackagesByNames[$link->getTarget()] as $provider) {
  197. if ($provider === $package) {
  198. continue;
  199. }
  200. if (!$this->obsoleteImpossibleForAlias($package, $provider)) {
  201. $reason = $isInstalled ? Rule::RULE_INSTALLED_PACKAGE_OBSOLETES : Rule::RULE_PACKAGE_OBSOLETES;
  202. $this->addRule(RuleSet::TYPE_PACKAGE, $this->createRule2Literals($package, $provider, $reason, $link));
  203. }
  204. }
  205. }
  206. }
  207. }
  208. protected function obsoleteImpossibleForAlias($package, $provider)
  209. {
  210. $packageIsAlias = $package instanceof AliasPackage;
  211. $providerIsAlias = $provider instanceof AliasPackage;
  212. $impossible = (
  213. ($packageIsAlias && $package->getAliasOf() === $provider) ||
  214. ($providerIsAlias && $provider->getAliasOf() === $package) ||
  215. ($packageIsAlias && $providerIsAlias && $provider->getAliasOf() === $package->getAliasOf())
  216. );
  217. return $impossible;
  218. }
  219. protected function addRulesForJobs($ignorePlatformReqs)
  220. {
  221. foreach ($this->jobs as $job) {
  222. switch ($job['cmd']) {
  223. case 'install':
  224. if (!$job['fixed'] && $ignorePlatformReqs && preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $job['packageName'])) {
  225. break;
  226. }
  227. $packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
  228. if ($packages) {
  229. foreach ($packages as $package) {
  230. if (!isset($this->installedMap[$package->id])) {
  231. $this->addRulesForPackage($package, $ignorePlatformReqs);
  232. }
  233. }
  234. $rule = $this->createInstallOneOfRule($packages, Rule::RULE_JOB_INSTALL, $job);
  235. $this->addRule(RuleSet::TYPE_JOB, $rule);
  236. }
  237. break;
  238. case 'remove':
  239. // remove all packages with this name including uninstalled
  240. // ones to make sure none of them are picked as replacements
  241. $packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
  242. foreach ($packages as $package) {
  243. $rule = $this->createRemoveRule($package, Rule::RULE_JOB_REMOVE, $job);
  244. $this->addRule(RuleSet::TYPE_JOB, $rule);
  245. }
  246. break;
  247. }
  248. }
  249. }
  250. public function getRulesFor($jobs, $installedMap, $ignorePlatformReqs = false)
  251. {
  252. $this->jobs = $jobs;
  253. $this->rules = new RuleSet;
  254. $this->installedMap = $installedMap;
  255. $this->addedMap = array();
  256. $this->conflictAddedMap = array();
  257. $this->addedPackages = array();
  258. $this->addedPackagesByNames = array();
  259. foreach ($this->installedMap as $package) {
  260. $this->addRulesForPackage($package, $ignorePlatformReqs);
  261. }
  262. $this->addRulesForJobs($ignorePlatformReqs);
  263. $this->addConflictRules();
  264. // Remove references to packages
  265. $this->addedPackages = $this->addedPackagesByNames = null;
  266. return $this->rules;
  267. }
  268. }