Problem.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * Represents a problem detected while solving dependencies
  14. *
  15. * @author Nils Adermann <naderman@naderman.de>
  16. */
  17. class Problem
  18. {
  19. /**
  20. * A set of reasons for the problem, each is a rule or a job and a rule
  21. * @var array
  22. */
  23. protected $reasons;
  24. /**
  25. * Add a job as a reason
  26. *
  27. * @param array $job A job descriptor which is a reason for this problem
  28. * @param Rule $rule An optional rule associated with the job
  29. */
  30. public function addJobRule($job, Rule $rule = null)
  31. {
  32. $this->addReason(serialize($job), array(
  33. 'rule' => $rule,
  34. 'job' => $job,
  35. ));
  36. }
  37. /**
  38. * Add a rule as a reason
  39. *
  40. * @param Rule $rule A rule which is a reason for this problem
  41. */
  42. public function addRule(Rule $rule)
  43. {
  44. $this->addReason($rule->getId(), array(
  45. 'rule' => $rule,
  46. 'job' => null,
  47. ));
  48. }
  49. /**
  50. * Retrieve all reasons for this problem
  51. *
  52. * @return array The problem's reasons
  53. */
  54. public function getReasons()
  55. {
  56. return $this->reasons;
  57. }
  58. /**
  59. * A human readable textual representation of the problem's reasons
  60. */
  61. public function __toString()
  62. {
  63. if (count($this->reasons) === 1) {
  64. reset($this->reasons);
  65. $reason = current($this->reasons);
  66. $rule = $reason['rule'];
  67. $job = $reason['job'];
  68. if ($job && $job['cmd'] === 'install' && empty($job['packages'])) {
  69. // handle php extensions
  70. if (0 === stripos($job['packageName'], 'ext-')) {
  71. $ext = substr($job['packageName'], 4);
  72. $error = extension_loaded($ext) ? 'has the wrong version ('.phpversion($ext).') installed' : 'is missing from your system';
  73. return 'The requested PHP extension "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).$error.'.';
  74. }
  75. return 'The requested package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'could not be found.';
  76. }
  77. }
  78. $messages = array("Problem caused by:");
  79. foreach ($this->reasons as $reason) {
  80. $rule = $reason['rule'];
  81. $job = $reason['job'];
  82. if ($job) {
  83. $messages[] = $this->jobToText($job);
  84. } elseif ($rule) {
  85. if ($rule instanceof Rule) {
  86. $messages[] = $rule->toHumanReadableString();
  87. }
  88. }
  89. }
  90. return implode("\n\t\t\t- ", $messages);
  91. }
  92. /**
  93. * Store a reason descriptor but ignore duplicates
  94. *
  95. * @param string $id A canonical identifier for the reason
  96. * @param string $reason The reason descriptor
  97. */
  98. protected function addReason($id, $reason)
  99. {
  100. if (!isset($this->reasons[$id])) {
  101. $this->reasons[$id] = $reason;
  102. }
  103. }
  104. /**
  105. * Turns a job into a human readable description
  106. *
  107. * @param array $job
  108. * @return string
  109. */
  110. protected function jobToText($job)
  111. {
  112. switch ($job['cmd']) {
  113. case 'install':
  114. return 'Installation of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested. Satisfiable by packages ['.implode(', ', $job['packages']).'].';
  115. case 'update':
  116. return 'Update of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested.';
  117. case 'remove':
  118. return 'Removal of package "'.$job['packageName'].'" '.$this->constraintToText($job['constraint']).'was requested.';
  119. }
  120. return 'Job(cmd='.$job['cmd'].', target='.$job['packageName'].', packages=['.implode(', ', $job['packages']).'])';
  121. }
  122. /**
  123. * Turns a constraint into text usable in a sentence describing a job
  124. *
  125. * @param LinkConstraint $constraint
  126. * @return string
  127. */
  128. protected function constraintToText($constraint)
  129. {
  130. return ($constraint) ? 'with constraint '.$constraint.' ' : '';
  131. }
  132. }