Problem.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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())
  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. $rule = $reason['rule'];
  70. $job = $reason['job'];
  71. if (isset($job['constraint'])) {
  72. $packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
  73. } else {
  74. $packages = array();
  75. }
  76. if ($job && $job['cmd'] === 'install' && empty($packages)) {
  77. // handle php/hhvm
  78. if ($job['packageName'] === 'php' || $job['packageName'] === 'php-64bit' || $job['packageName'] === 'hhvm') {
  79. $available = $this->pool->whatProvides($job['packageName']);
  80. $firstAvailable = reset($available);
  81. $version = count($available) ? $firstAvailable->getPrettyVersion() : phpversion();
  82. if (count($available) && $firstAvailable instanceof CompletePackageInterface) {
  83. $version .= '; ' . $firstAvailable->getDescription();
  84. }
  85. $msg = "\n - This package requires ".$job['packageName'].$this->constraintToText($job['constraint']).' but ';
  86. if (defined('HHVM_VERSION')) {
  87. return $msg . 'your HHVM version does not satisfy that requirement.';
  88. }
  89. if ($job['packageName'] === 'hhvm') {
  90. return $msg . 'you are running this with PHP and not HHVM.';
  91. }
  92. return $msg . 'your PHP version ('. $version .') does not satisfy that requirement.';
  93. }
  94. // handle php extensions
  95. if (0 === stripos($job['packageName'], 'ext-')) {
  96. if (false !== strpos($job['packageName'], ' ')) {
  97. return "\n - The requested PHP extension ".$job['packageName'].' should be required as '.str_replace(' ', '-', $job['packageName']).'.';
  98. }
  99. $ext = substr($job['packageName'], 4);
  100. $error = extension_loaded($ext) ? 'has the wrong version ('.(phpversion($ext) ?: '0').') installed' : 'is missing from your system';
  101. return "\n - The requested PHP extension ".$job['packageName'].$this->constraintToText($job['constraint']).' '.$error.'. Install or enable PHP\'s '.$ext.' extension.';
  102. }
  103. // handle linked libs
  104. if (0 === stripos($job['packageName'], 'lib-')) {
  105. if (strtolower($job['packageName']) === 'lib-icu') {
  106. $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.';
  107. return "\n - The requested linked library ".$job['packageName'].$this->constraintToText($job['constraint']).' '.$error;
  108. }
  109. 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.';
  110. }
  111. if (!preg_match('{^[A-Za-z0-9_./-]+$}', $job['packageName'])) {
  112. $illegalChars = preg_replace('{[A-Za-z0-9_./-]+}', '', $job['packageName']);
  113. 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.';
  114. }
  115. if ($providers = $this->pool->whatProvides($job['packageName'], $job['constraint'], true, true)) {
  116. 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.';
  117. }
  118. if ($providers = $this->pool->whatProvides($job['packageName'], null, true, true)) {
  119. return "\n - The requested package ".$job['packageName'].$this->constraintToText($job['constraint']).' exists as '.$this->getPackageList($providers).' but these are rejected by your constraint.';
  120. }
  121. return "\n - The requested package ".$job['packageName'].' could not be found in any version, there may be a typo in the package name.';
  122. }
  123. }
  124. $messages = array();
  125. foreach ($reasons as $reason) {
  126. $rule = $reason['rule'];
  127. $job = $reason['job'];
  128. if ($job) {
  129. $messages[] = $this->jobToText($job);
  130. } elseif ($rule) {
  131. if ($rule instanceof Rule) {
  132. $messages[] = $rule->getPrettyString($this->pool, $installedMap);
  133. }
  134. }
  135. }
  136. return "\n - ".implode("\n - ", $messages);
  137. }
  138. /**
  139. * Store a reason descriptor but ignore duplicates
  140. *
  141. * @param string $id A canonical identifier for the reason
  142. * @param string $reason The reason descriptor
  143. */
  144. protected function addReason($id, $reason)
  145. {
  146. if (!isset($this->reasonSeen[$id])) {
  147. $this->reasonSeen[$id] = true;
  148. $this->reasons[$this->section][] = $reason;
  149. }
  150. }
  151. public function nextSection()
  152. {
  153. $this->section++;
  154. }
  155. /**
  156. * Turns a job into a human readable description
  157. *
  158. * @param array $job
  159. * @return string
  160. */
  161. protected function jobToText($job)
  162. {
  163. switch ($job['cmd']) {
  164. case 'install':
  165. $packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
  166. if (!$packages) {
  167. return 'No package found to satisfy install request for '.$job['packageName'].$this->constraintToText($job['constraint']);
  168. }
  169. return 'Installation request for '.$job['packageName'].$this->constraintToText($job['constraint']).' -> satisfiable by '.$this->getPackageList($packages).'.';
  170. case 'update':
  171. return 'Update request for '.$job['packageName'].$this->constraintToText($job['constraint']).'.';
  172. case 'remove':
  173. return 'Removal request for '.$job['packageName'].$this->constraintToText($job['constraint']).'';
  174. }
  175. if (isset($job['constraint'])) {
  176. $packages = $this->pool->whatProvides($job['packageName'], $job['constraint']);
  177. } else {
  178. $packages = array();
  179. }
  180. return 'Job(cmd='.$job['cmd'].', target='.$job['packageName'].', packages=['.$this->getPackageList($packages).'])';
  181. }
  182. protected function getPackageList($packages)
  183. {
  184. $prepared = array();
  185. foreach ($packages as $package) {
  186. $prepared[$package->getName()]['name'] = $package->getPrettyName();
  187. $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
  188. }
  189. foreach ($prepared as $name => $package) {
  190. $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
  191. }
  192. return implode(', ', $prepared);
  193. }
  194. /**
  195. * Turns a constraint into text usable in a sentence describing a job
  196. *
  197. * @param \Composer\Semver\Constraint\ConstraintInterface $constraint
  198. * @return string
  199. */
  200. protected function constraintToText($constraint)
  201. {
  202. return ($constraint) ? ' '.$constraint->getPrettyString() : '';
  203. }
  204. }