SolverProblemsException.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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\Util\IniHelper;
  13. use Composer\Repository\RepositorySet;
  14. /**
  15. * @author Nils Adermann <naderman@naderman.de>
  16. */
  17. class SolverProblemsException extends \RuntimeException
  18. {
  19. protected $problems;
  20. protected $learnedPool;
  21. public function __construct(array $problems, array $learnedPool)
  22. {
  23. $this->problems = $problems;
  24. $this->learnedPool = $learnedPool;
  25. parent::__construct('Failed resolving dependencies with '.count($problems).' problems, call getPrettyString to get formatted details', 2);
  26. }
  27. public function getPrettyString(RepositorySet $repositorySet, Request $request, Pool $pool)
  28. {
  29. $installedMap = $request->getPresentMap(true);
  30. $text = "\n";
  31. $hasExtensionProblems = false;
  32. foreach ($this->problems as $i => $problem) {
  33. $text .= " Problem ".($i + 1).$problem->getPrettyString($repositorySet, $request, $pool, $installedMap, $this->learnedPool)."\n";
  34. if (!$hasExtensionProblems && $this->hasExtensionProblems($problem->getReasons())) {
  35. $hasExtensionProblems = true;
  36. }
  37. }
  38. if (strpos($text, 'could not be found') || strpos($text, 'no matching package found')) {
  39. $text .= "\nPotential causes:\n - A typo in the package name\n - The package is not available in a stable-enough version according to your minimum-stability setting\n see <https://getcomposer.org/doc/04-schema.md#minimum-stability> for more details.\n - It's a private package and you forgot to add a custom repository to find it\n\nRead <https://getcomposer.org/doc/articles/troubleshooting.md> for further common problems.";
  40. }
  41. if ($hasExtensionProblems) {
  42. $text .= $this->createExtensionHint();
  43. }
  44. return $text;
  45. }
  46. public function getProblems()
  47. {
  48. return $this->problems;
  49. }
  50. private function createExtensionHint()
  51. {
  52. $paths = IniHelper::getAll();
  53. if (count($paths) === 1 && empty($paths[0])) {
  54. return '';
  55. }
  56. $text = "\n To enable extensions, verify that they are enabled in your .ini files:\n - ";
  57. $text .= implode("\n - ", $paths);
  58. $text .= "\n You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.";
  59. return $text;
  60. }
  61. private function hasExtensionProblems(array $reasonSets)
  62. {
  63. foreach ($reasonSets as $reasonSet) {
  64. foreach ($reasonSet as $rule) {
  65. if (0 === strpos($rule->getRequiredPackage(), 'ext-')) {
  66. return true;
  67. }
  68. }
  69. }
  70. return false;
  71. }
  72. }