Problem.php 6.8 KB

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