Problem.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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\Link;
  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($rule->getId(), 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 ($job && $job['cmd'] === 'install' && empty($job['packages'])) {
  72. // handle php extensions
  73. if (0 === stripos($job['packageName'], 'ext-')) {
  74. $ext = substr($job['packageName'], 4);
  75. $error = extension_loaded($ext) ? 'has the wrong version ('.phpversion($ext).') installed' : 'is missing from your system';
  76. return "\n - The requested PHP extension ".$job['packageName'].$this->constraintToText($job['constraint']).' '.$error.'.';
  77. }
  78. // handle linked libs
  79. if (0 === stripos($job['packageName'], 'lib-')) {
  80. if (strtolower($job['packageName']) === 'lib-icu') {
  81. $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.';
  82. return "\n - The requested linked library ".$job['packageName'].$this->constraintToText($job['constraint']).' '.$error;
  83. }
  84. 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.';
  85. }
  86. if (!preg_match('{^[A-Za-z0-9_./-]+$}', $job['packageName'])) {
  87. $illegalChars = preg_replace('{[A-Za-z0-9_./-]+}', '', $job['packageName']);
  88. 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.';
  89. }
  90. if (!$this->pool->whatProvides($job['packageName'])) {
  91. return "\n - The requested package ".$job['packageName'].' could not be found in any version, there may be a typo in the package name.';
  92. }
  93. return "\n - The requested package ".$job['packageName'].$this->constraintToText($job['constraint']).' could not be found.';
  94. }
  95. }
  96. $messages = array();
  97. foreach ($reasons as $reason) {
  98. $rule = $reason['rule'];
  99. $job = $reason['job'];
  100. if ($job) {
  101. $messages[] = $this->jobToText($job);
  102. } elseif ($rule) {
  103. if ($rule instanceof Rule) {
  104. $messages[] = $rule->getPrettyString($installedMap);
  105. }
  106. }
  107. }
  108. return "\n - ".implode("\n - ", $messages);
  109. }
  110. /**
  111. * Store a reason descriptor but ignore duplicates
  112. *
  113. * @param string $id A canonical identifier for the reason
  114. * @param string $reason The reason descriptor
  115. */
  116. protected function addReason($id, $reason)
  117. {
  118. if (!isset($this->reasonSeen[$id])) {
  119. $this->reasonSeen[$id] = true;
  120. $this->reasons[$this->section][] = $reason;
  121. }
  122. }
  123. public function nextSection()
  124. {
  125. $this->section++;
  126. }
  127. /**
  128. * Turns a job into a human readable description
  129. *
  130. * @param array $job
  131. * @return string
  132. */
  133. protected function jobToText($job)
  134. {
  135. switch ($job['cmd']) {
  136. case 'install':
  137. if (!$job['packages']) {
  138. return 'No package found to satisfy install request for '.$job['packageName'].$this->constraintToText($job['constraint']);
  139. }
  140. return 'Installation request for '.$job['packageName'].$this->constraintToText($job['constraint']).' -> satisfiable by '.$this->getPackageList($job['packages']).'.';
  141. case 'update':
  142. return 'Update request for '.$job['packageName'].$this->constraintToText($job['constraint']).'.';
  143. case 'remove':
  144. return 'Removal request for '.$job['packageName'].$this->constraintToText($job['constraint']).'';
  145. }
  146. return 'Job(cmd='.$job['cmd'].', target='.$job['packageName'].', packages=['.$this->getPackageList($job['packages']).'])';
  147. }
  148. protected function getPackageList($packages)
  149. {
  150. $prepared = array();
  151. foreach ($packages as $package) {
  152. $prepared[$package->getName()]['name'] = $package->getPrettyName();
  153. $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
  154. }
  155. foreach ($prepared as $name => $package) {
  156. $prepared[$name] = $package['name'].'['.implode(', ', $package['versions']).']';
  157. }
  158. return implode(', ', $prepared);
  159. }
  160. /**
  161. * Turns a constraint into text usable in a sentence describing a job
  162. *
  163. * @param LinkConstraint $constraint
  164. * @return string
  165. */
  166. protected function constraintToText($constraint)
  167. {
  168. return ($constraint) ? ' '.$constraint->getPrettyString() : '';
  169. }
  170. }