RuleSetGenerator.php 12 KB

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