PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Composer/DependencyResolver/Rule.php

http://github.com/composer/composer
PHP | 293 lines | 212 code | 56 blank | 25 comment | 27 complexity | 162fdce4065b0495e9f2f5dfa10cf4c6 MD5 | raw file
  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\CompletePackage;
  13. use Composer\Package\Link;
  14. use Composer\Package\PackageInterface;
  15. use Composer\Package\AliasPackage;
  16. use Composer\Repository\RepositorySet;
  17. use Composer\Package\Version\VersionParser;
  18. /**
  19. * @author Nils Adermann <naderman@naderman.de>
  20. * @author Ruben Gonzalez <rubenrua@gmail.com>
  21. */
  22. abstract class Rule
  23. {
  24. // reason constants
  25. const RULE_INTERNAL_ALLOW_UPDATE = 1;
  26. const RULE_ROOT_REQUIRE = 2;
  27. const RULE_FIXED = 3;
  28. const RULE_PACKAGE_CONFLICT = 6;
  29. const RULE_PACKAGE_REQUIRES = 7;
  30. const RULE_PACKAGE_SAME_NAME = 10;
  31. const RULE_LEARNED = 12;
  32. const RULE_PACKAGE_ALIAS = 13;
  33. // bitfield defs
  34. const BITFIELD_TYPE = 0;
  35. const BITFIELD_REASON = 8;
  36. const BITFIELD_DISABLED = 16;
  37. protected $bitfield;
  38. protected $request;
  39. protected $reasonData;
  40. /**
  41. * @param int $reason A RULE_* constant describing the reason for generating this rule
  42. * @param Link|PackageInterface $reasonData
  43. */
  44. public function __construct($reason, $reasonData)
  45. {
  46. $this->reasonData = $reasonData;
  47. $this->bitfield = (0 << self::BITFIELD_DISABLED) |
  48. ($reason << self::BITFIELD_REASON) |
  49. (255 << self::BITFIELD_TYPE);
  50. }
  51. abstract public function getLiterals();
  52. abstract public function getHash();
  53. abstract public function equals(Rule $rule);
  54. public function getReason()
  55. {
  56. return ($this->bitfield & (255 << self::BITFIELD_REASON)) >> self::BITFIELD_REASON;
  57. }
  58. public function getReasonData()
  59. {
  60. return $this->reasonData;
  61. }
  62. public function getRequiredPackage()
  63. {
  64. $reason = $this->getReason();
  65. if ($reason === self::RULE_ROOT_REQUIRE) {
  66. return $this->reasonData['packageName'];
  67. }
  68. if ($reason === self::RULE_FIXED) {
  69. return $this->reasonData['package']->getName();
  70. }
  71. if ($reason === self::RULE_PACKAGE_REQUIRES) {
  72. return $this->reasonData->getTarget();
  73. }
  74. }
  75. public function setType($type)
  76. {
  77. $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_TYPE)) | ((255 & $type) << self::BITFIELD_TYPE);
  78. }
  79. public function getType()
  80. {
  81. return ($this->bitfield & (255 << self::BITFIELD_TYPE)) >> self::BITFIELD_TYPE;
  82. }
  83. public function disable()
  84. {
  85. $this->bitfield = ($this->bitfield & ~(255 << self::BITFIELD_DISABLED)) | (1 << self::BITFIELD_DISABLED);
  86. }
  87. public function enable()
  88. {
  89. $this->bitfield &= ~(255 << self::BITFIELD_DISABLED);
  90. }
  91. public function isDisabled()
  92. {
  93. return (bool) (($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
  94. }
  95. public function isEnabled()
  96. {
  97. return !(($this->bitfield & (255 << self::BITFIELD_DISABLED)) >> self::BITFIELD_DISABLED);
  98. }
  99. abstract public function isAssertion();
  100. public function isCausedByLock()
  101. {
  102. return $this->getReason() === self::RULE_FIXED && $this->reasonData['lockable'];
  103. }
  104. public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool, $isVerbose, array $installedMap = array(), array $learnedPool = array())
  105. {
  106. $literals = $this->getLiterals();
  107. $ruleText = '';
  108. foreach ($literals as $i => $literal) {
  109. if ($i != 0) {
  110. $ruleText .= '|';
  111. }
  112. $ruleText .= $pool->literalToPrettyString($literal, $installedMap);
  113. }
  114. switch ($this->getReason()) {
  115. case self::RULE_INTERNAL_ALLOW_UPDATE:
  116. return $ruleText;
  117. case self::RULE_ROOT_REQUIRE:
  118. $packageName = $this->reasonData['packageName'];
  119. $constraint = $this->reasonData['constraint'];
  120. $packages = $pool->whatProvides($packageName, $constraint);
  121. if (!$packages) {
  122. return 'No package found to satisfy root composer.json require '.$packageName.($constraint ? ' '.$constraint->getPrettyString() : '');
  123. }
  124. return 'Root composer.json requires '.$packageName.($constraint ? ' '.$constraint->getPrettyString() : '').' -> satisfiable by '.$this->formatPackagesUnique($pool, $packages, $isVerbose).'.';
  125. case self::RULE_FIXED:
  126. $package = $this->deduplicateMasterAlias($this->reasonData['package']);
  127. if ($this->reasonData['lockable']) {
  128. return $package->getPrettyName().' is locked to version '.$package->getPrettyVersion().' and an update of this package was not requested.';
  129. }
  130. return $package->getPrettyName().' is present at version '.$package->getPrettyVersion() . ' and cannot be modified by Composer';
  131. case self::RULE_PACKAGE_CONFLICT:
  132. $package1 = $this->deduplicateMasterAlias($pool->literalToPackage($literals[0]));
  133. $package2 = $this->deduplicateMasterAlias($pool->literalToPackage($literals[1]));
  134. return $package2->getPrettyString().' conflicts with '.$package1->getPrettyString().'.';
  135. case self::RULE_PACKAGE_REQUIRES:
  136. $sourceLiteral = array_shift($literals);
  137. $sourcePackage = $this->deduplicateMasterAlias($pool->literalToPackage($sourceLiteral));
  138. $requires = array();
  139. foreach ($literals as $literal) {
  140. $requires[] = $pool->literalToPackage($literal);
  141. }
  142. $text = $this->reasonData->getPrettyString($sourcePackage);
  143. if ($requires) {
  144. $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires, $isVerbose) . '.';
  145. } else {
  146. $targetName = $this->reasonData->getTarget();
  147. $reason = Problem::getMissingPackageReason($repositorySet, $request, $pool, $isVerbose, $targetName, $this->reasonData->getConstraint());
  148. return $text . ' -> ' . $reason[1];
  149. }
  150. return $text;
  151. case self::RULE_PACKAGE_SAME_NAME:
  152. $packageNames = array();
  153. foreach ($literals as $literal) {
  154. $package = $pool->literalToPackage($literal);
  155. $packageNames[$package->getName()] = true;
  156. }
  157. $replacedName = $this->reasonData;
  158. if (count($packageNames) > 1) {
  159. $reason = null;
  160. if (!isset($packageNames[$replacedName])) {
  161. $reason = 'They '.(count($literals) == 2 ? 'both' : 'all').' replace '.$replacedName.' and thus cannot coexist.';
  162. } else {
  163. $replacerNames = $packageNames;
  164. unset($replacerNames[$replacedName]);
  165. $replacerNames = array_keys($replacerNames);
  166. if (count($replacerNames) == 1) {
  167. $reason = $replacerNames[0] . ' replaces ';
  168. } else {
  169. $reason = '['.implode(', ', $replacerNames).'] replace ';
  170. }
  171. $reason .= $replacedName.' and thus cannot coexist with it.';
  172. }
  173. $installedPackages = array();
  174. $removablePackages = array();
  175. foreach ($literals as $literal) {
  176. if (isset($installedMap[abs($literal)])) {
  177. $installedPackages[] = $pool->literalToPackage($literal);
  178. } else {
  179. $removablePackages[] = $pool->literalToPackage($literal);
  180. }
  181. }
  182. if ($installedPackages && $removablePackages) {
  183. return $this->formatPackagesUnique($pool, $removablePackages, $isVerbose).' cannot be installed as that would require removing '.$this->formatPackagesUnique($pool, $installedPackages, $isVerbose).'. '.$reason;
  184. }
  185. return 'Only one of these can be installed: '.$this->formatPackagesUnique($pool, $literals, $isVerbose).'. '.$reason;
  186. }
  187. return 'You can only install one version of a package, so only one of these can be installed: ' . $this->formatPackagesUnique($pool, $literals, $isVerbose) . '.';
  188. case self::RULE_LEARNED:
  189. if (isset($learnedPool[$this->reasonData])) {
  190. $learnedString = ', learned rules:'."\n - ";
  191. $reasons = array();
  192. foreach ($learnedPool[$this->reasonData] as $learnedRule) {
  193. $reasons[] = $learnedRule->getPrettyString($repositorySet, $request, $pool, $installedMap, $learnedPool);
  194. }
  195. $learnedString .= implode("\n - ", array_unique($reasons));
  196. } else {
  197. $learnedString = ' (reasoning unavailable)';
  198. }
  199. return 'Conclusion: '.$ruleText.$learnedString;
  200. case self::RULE_PACKAGE_ALIAS:
  201. return $ruleText;
  202. default:
  203. return '('.$ruleText.')';
  204. }
  205. }
  206. /**
  207. * @param Pool $pool
  208. * @param array $packages
  209. *
  210. * @return string
  211. */
  212. protected function formatPackagesUnique($pool, array $packages, $isVerbose)
  213. {
  214. $prepared = array();
  215. foreach ($packages as $index => $package) {
  216. if (!\is_object($package)) {
  217. $packages[$index] = $pool->literalToPackage($package);
  218. }
  219. }
  220. return Problem::getPackageList($packages, $isVerbose);
  221. }
  222. private function getReplacedNames(PackageInterface $package)
  223. {
  224. $names = array();
  225. foreach ($package->getReplaces() as $link) {
  226. $names[] = $link->getTarget();
  227. }
  228. return $names;
  229. }
  230. private function deduplicateMasterAlias(PackageInterface $package)
  231. {
  232. if ($package instanceof AliasPackage && $package->getPrettyVersion() === VersionParser::DEV_MASTER_ALIAS) {
  233. $package = $package->getAliasOf();
  234. }
  235. return $package;
  236. }
  237. }