Problem.php 5.7 KB

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