SolverProblemsException.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 $installedMap;
  21. protected $learnedPool;
  22. public function __construct(array $problems, RepositorySet $repositorySet, Request $request, array $learnedPool)
  23. {
  24. $this->problems = $problems;
  25. $this->installedMap = $request->getPresentMap(true);
  26. $this->learnedPool = $learnedPool;
  27. parent::__construct($this->createMessage($repositorySet, $request), 2);
  28. }
  29. protected function createMessage(RepositorySet $repositorySet, Request $request)
  30. {
  31. $text = "\n";
  32. $hasExtensionProblems = false;
  33. foreach ($this->problems as $i => $problem) {
  34. $text .= " Problem ".($i + 1).$problem->getPrettyString($repositorySet, $request, $this->installedMap, $this->learnedPool)."\n";
  35. if (!$hasExtensionProblems && $this->hasExtensionProblems($problem->getReasons())) {
  36. $hasExtensionProblems = true;
  37. }
  38. }
  39. if (strpos($text, 'could not be found') || strpos($text, 'no matching package found')) {
  40. $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.";
  41. }
  42. if ($hasExtensionProblems) {
  43. $text .= $this->createExtensionHint();
  44. }
  45. return $text;
  46. }
  47. public function getProblems()
  48. {
  49. return $this->problems;
  50. }
  51. private function createExtensionHint()
  52. {
  53. $paths = IniHelper::getAll();
  54. if (count($paths) === 1 && empty($paths[0])) {
  55. return '';
  56. }
  57. $text = "\n To enable extensions, verify that they are enabled in your .ini files:\n - ";
  58. $text .= implode("\n - ", $paths);
  59. $text .= "\n You can also run `php --ini` inside terminal to see which files are used by PHP in CLI mode.";
  60. return $text;
  61. }
  62. private function hasExtensionProblems(array $reasonSets)
  63. {
  64. foreach ($reasonSets as $reasonSet) {
  65. foreach ($reasonSet as $rule) {
  66. if (0 === strpos($rule->getRequiredPackage(), 'ext-')) {
  67. return true;
  68. }
  69. }
  70. }
  71. return false;
  72. }
  73. }