SolverProblemsException.php 2.8 KB

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