Problem.php 8.7 KB

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