PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Composer/DependencyResolver/SolverProblemsException.php

http://github.com/composer/composer
PHP | 117 lines | 80 code | 24 blank | 13 comment | 18 complexity | 814f22a2531bb419a8102c494261e7f6 MD5 | raw file
  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, $isVerbose, $isDevExtraction = false)
  28. {
  29. $installedMap = $request->getPresentMap(true);
  30. $hasExtensionProblems = false;
  31. $isCausedByLock = false;
  32. $problems = array();
  33. foreach ($this->problems as $problem) {
  34. $problems[] = $problem->getPrettyString($repositorySet, $request, $pool, $isVerbose, $installedMap, $this->learnedPool)."\n";
  35. if (!$hasExtensionProblems && $this->hasExtensionProblems($problem->getReasons())) {
  36. $hasExtensionProblems = true;
  37. }
  38. $isCausedByLock |= $problem->isCausedByLock();
  39. }
  40. $i = 1;
  41. $text = "\n";
  42. foreach (array_unique($problems) as $problem) {
  43. $text .= " Problem ".($i++).$problem;
  44. }
  45. if (!$isDevExtraction && (strpos($text, 'could not be found') || strpos($text, 'no matching package found'))) {
  46. $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.";
  47. }
  48. if ($hasExtensionProblems) {
  49. $text .= $this->createExtensionHint();
  50. }
  51. if ($isCausedByLock && !$isDevExtraction) {
  52. $text .= "\nUse the option --with-all-dependencies to allow updates and removals for packages currently locked to specific versions.";
  53. }
  54. if (strpos($text, 'found composer-plugin-api[2.0.0] but it does not match') && strpos($text, '- ocramius/package-versions')) {
  55. $text .= "\n<warning>ocramius/package-versions only provides support for Composer 2 in 1.8+, which requires PHP 7.4.</warning>\nIf you can not upgrade PHP you can require <info>composer/package-versions-deprecated</info> to resolve this with PHP 7.0+.\n";
  56. }
  57. // TODO remove before 2.0 final
  58. if (!class_exists('PHPUnit\Framework\TestCase', false)) {
  59. if (strpos($text, 'found composer-plugin-api[2.0.0] but it does not match')) {
  60. $text .= "\nYou are using a snapshot build of Composer 2, which some of your plugins seem to be incompatible with. Make sure you update your plugins or report an issue to them to ask them to support Composer 2. To work around this you can run Composer with --ignore-platform-reqs, but this will also ignore your PHP version and may result in bigger problems down the line.";
  61. } else {
  62. $text .= "\nYou are using a snapshot build of Composer 2, which may be the cause of the problem. Run `composer self-update --stable` and then try again. In case it solves the problem, please report an issue mentioning Composer 2.";
  63. }
  64. }
  65. return $text;
  66. }
  67. public function getProblems()
  68. {
  69. return $this->problems;
  70. }
  71. private function createExtensionHint()
  72. {
  73. $paths = IniHelper::getAll();
  74. if (count($paths) === 1 && empty($paths[0])) {
  75. return '';
  76. }
  77. $text = "\n To enable extensions, verify that they are enabled in your .ini files:\n - ";
  78. $text .= implode("\n - ", $paths);
  79. $text .= "\n You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.";
  80. return $text;
  81. }
  82. private function hasExtensionProblems(array $reasonSets)
  83. {
  84. foreach ($reasonSets as $reasonSet) {
  85. foreach ($reasonSet as $rule) {
  86. if (0 === strpos($rule->getRequiredPackage(), 'ext-')) {
  87. return true;
  88. }
  89. }
  90. }
  91. return false;
  92. }
  93. }