Problem.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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\CompletePackageInterface;
  13. /**
  14. * Represents a problem detected while solving dependencies
  15. *
  16. * @author Nils Adermann <naderman@naderman.de>
  17. */
  18. class Problem
  19. {
  20. /**
  21. * A map containing the id of each rule part of this problem as a key
  22. * @var array
  23. */
  24. protected $reasonSeen;
  25. /**
  26. * A set of reasons for the problem, each is a rule or a job and a rule
  27. * @var array
  28. */
  29. protected $reasons = array();
  30. protected $section = 0;
  31. protected $pool;
  32. public function __construct(Pool $pool)
  33. {
  34. $this->pool = $pool;
  35. }
  36. /**
  37. * Add a rule as a reason
  38. *
  39. * @param Rule $rule A rule which is a reason for this problem
  40. */
  41. public function addRule(Rule $rule)
  42. {
  43. $this->addReason(spl_object_hash($rule), array(
  44. 'rule' => $rule,
  45. 'job' => $rule->getJob(),
  46. ));
  47. }
  48. /**
  49. * Retrieve all reasons for this problem
  50. *
  51. * @return array The problem's reasons
  52. */
  53. public function getReasons()
  54. {
  55. return $this->reasons;
  56. }
  57. /**
  58. * A human readable textual representation of the problem's reasons
  59. *
  60. * @param array $installedMap A map of all installed packages
  61. * @return string
  62. */
  63. public function getPrettyString(array $installedMap = array(), array $learnedPool = array())
  64. {
  65. $reasons = call_user_func_array('array_merge', array_reverse($this->reasons));
  66. if (count($reasons) === 1) {
  67. reset($reasons);
  68. $reason = current($reasons);
  69. $job = $reason['job'];
  70. $packageName = $job['packageName'];
  71. $constraint = $job['constraint'];
  72. if (isset($constraint)) {
  73. $packages = $this->pool->whatProvides($packageName, $constraint);
  74. } else {
  75. $packages = array();
  76. }
  77. if ($job && $job['cmd'] === 'install' && empty($packages)) {
  78. // handle php/hhvm
  79. if ($packageName === 'php' || $packageName === 'php-64bit' || $packageName === 'hhvm') {
  80. $version = phpversion();
  81. $available = $this->pool->whatProvides($packageName);
  82. if (count($available)) {
  83. $firstAvailable = reset($available);
  84. $version = $firstAvailable->getPrettyVersion();
  85. $extra = $firstAvailable->getExtra();
  86. if ($firstAvailable instanceof CompletePackageInterface && isset($extra['config.platform']) && $extra['config.platform'] === true) {
  87. $version .= '; ' . $firstAvailable->getDescription();
  88. }
  89. }
  90. $msg = "\n - This package requires ".$packageName.$this->constraintToText($constraint).' but ';
  91. if (defined('HHVM_VERSION') || (count($available) && $packageName === 'hhvm')) {
  92. return $msg . 'your HHVM version does not satisfy that requirement.';
  93. }
  94. if ($packageName === 'hhvm') {
  95. return $msg . 'you are running this with PHP and not HHVM.';
  96. }
  97. return $msg . 'your PHP version ('. $version .') does not satisfy that requirement.';
  98. }
  99. // handle php extensions
  100. if (0 === stripos($packageName, 'ext-')) {
  101. if (false !== strpos($packageName, ' ')) {
  102. return "\n - The requested PHP extension ".$packageName.' should be required as '.str_replace(' ', '-', $packageName).'.';
  103. }
  104. $ext = substr($packageName, 4);
  105. $error = extension_loaded($ext) ? 'has the wrong version ('.(phpversion($ext) ?: '0').') installed' : 'is missing from your system';
  106. return "\n - The requested PHP extension ".$packageName.$this->constraintToText($constraint).' '.$error.'. Install or enable PHP\'s '.$ext.' extension.';
  107. }
  108. // handle linked libs
  109. if (0 === stripos($packageName, 'lib-')) {
  110. if (strtolower($packageName) === 'lib-icu') {
  111. $error = extension_loaded('intl') ? 'has the wrong version installed, try upgrading the intl extension.' : 'is missing from your system, make sure the intl extension is loaded.';
  112. return "\n - The requested linked library ".$packageName.$this->constraintToText($constraint).' '.$error;
  113. }
  114. return "\n - The requested linked library ".$packageName.$this->constraintToText($constraint).' has the wrong version installed or is missing from your system, make sure to load the extension providing it.';
  115. }
  116. if (!preg_match('{^[A-Za-z0-9_./-]+$}', $packageName)) {
  117. $illegalChars = preg_replace('{[A-Za-z0-9_./-]+}', '', $packageName);
  118. return "\n - The requested package ".$packageName.' could not be found, it looks like its name is invalid, "'.$illegalChars.'" is not allowed in package names.';
  119. }
  120. if ($providers = $this->pool->whatProvides($packageName, $constraint, true, true)) {
  121. return "\n - The requested package ".$packageName.$this->constraintToText($constraint).' is satisfiable by '.$this->getPackageList($providers).' but these conflict with your requirements or minimum-stability.';
  122. }
  123. if ($providers = $this->pool->whatProvides($packageName, null, true, true)) {
  124. return "\n - The requested package ".$packageName.$this->constraintToText($constraint).' exists as '.$this->getPackageList($providers).' but these are rejected by your constraint.';
  125. }
  126. return "\n - The requested package ".$packageName.' could not be found in any version, there may be a typo in the package name.';
  127. }
  128. }
  129. $messages = array();
  130. foreach ($reasons as $reason) {
  131. $rule = $reason['rule'];
  132. $job = $reason['job'];
  133. if ($job) {
  134. $messages[] = $this->jobToText($job);
  135. } elseif ($rule) {
  136. if ($rule instanceof Rule) {
  137. $messages[] = $rule->getPrettyString($this->pool, $installedMap, $learnedPool);
  138. }
  139. }
  140. }
  141. return "\n - ".implode("\n - ", $messages);
  142. }
  143. /**
  144. * Store a reason descriptor but ignore duplicates
  145. *
  146. * @param string $id A canonical identifier for the reason
  147. * @param string|array $reason The reason descriptor
  148. */
  149. protected function addReason($id, $reason)
  150. {
  151. if (!isset($this->reasonSeen[$id])) {
  152. $this->reasonSeen[$id] = true;
  153. $this->reasons[$this->section][] = $reason;
  154. }
  155. }
  156. public function nextSection()
  157. {
  158. $this->section++;
  159. }
  160. /**
  161. * Turns a job into a human readable description
  162. *
  163. * @param array $job
  164. * @return string
  165. */
  166. protected function jobToText($job)
  167. {
  168. $packageName = $job['packageName'];
  169. $constraint = $job['constraint'];
  170. switch ($job['cmd']) {
  171. case 'install':
  172. $packages = $this->pool->whatProvides($packageName, $constraint);
  173. if (!$packages) {
  174. return 'No package found to satisfy install request for '.$packageName.$this->constraintToText($constraint);
  175. }
  176. return 'Installation request for '.$packageName.$this->constraintToText($constraint).' -> satisfiable by '.$this->getPackageList($packages).'.';
  177. case 'update':
  178. return 'Update request for '.$packageName.$this->constraintToText($constraint).'.';
  179. case 'remove':
  180. return 'Removal request for '.$packageName.$this->constraintToText($constraint).'';
  181. }
  182. if (isset($constraint)) {
  183. $packages = $this->pool->whatProvides($packageName, $constraint);
  184. } else {
  185. $packages = array();
  186. }
  187. return 'Job(cmd='.$job['cmd'].', target='.$packageName.', packages=['.$this->getPackageList($packages).'])';
  188. }
  189. protected function getPackageList($packages)
  190. {
  191. $prepared = array();
  192. foreach ($packages as $package) {
  193. $prepared[$package->getName()]['name'] = $package->getPrettyName();
  194. $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
  195. }
  196. foreach ($prepared as $name => $package) {
  197. $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
  198. }
  199. return implode(', ', $prepared);
  200. }
  201. /**
  202. * Turns a constraint into text usable in a sentence describing a job
  203. *
  204. * @param \Composer\Semver\Constraint\ConstraintInterface $constraint
  205. * @return string
  206. */
  207. protected function constraintToText($constraint)
  208. {
  209. return $constraint ? ' '.$constraint->getPrettyString() : '';
  210. }
  211. }