SolverProblemsException.php 2.7 KB

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