RuleSetGenerator.php 13 KB

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