ExceptionCollection.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace Guzzle\Common\Exception;
  3. /**
  4. * Collection of exceptions
  5. */
  6. class ExceptionCollection extends \Exception implements GuzzleException, \IteratorAggregate, \Countable
  7. {
  8. /** @var array Array of Exceptions */
  9. protected $exceptions = array();
  10. /** @var string Succinct exception message not including sub-exceptions */
  11. private $shortMessage;
  12. public function __construct($message = '', $code = 0, \Exception $previous = null)
  13. {
  14. parent::__construct($message, $code, $previous);
  15. $this->shortMessage = $message;
  16. }
  17. /**
  18. * Set all of the exceptions
  19. *
  20. * @param array $exceptions Array of exceptions
  21. *
  22. * @return self
  23. */
  24. public function setExceptions(array $exceptions)
  25. {
  26. $this->exceptions = array();
  27. foreach ($exceptions as $exception) {
  28. $this->add($exception);
  29. }
  30. return $this;
  31. }
  32. /**
  33. * Add exceptions to the collection
  34. *
  35. * @param ExceptionCollection|\Exception $e Exception to add
  36. *
  37. * @return ExceptionCollection;
  38. */
  39. public function add($e)
  40. {
  41. $this->exceptions[] = $e;
  42. if ($this->message) {
  43. $this->message .= "\n";
  44. }
  45. $this->message .= $this->getExceptionMessage($e, 0);
  46. return $this;
  47. }
  48. /**
  49. * Get the total number of request exceptions
  50. *
  51. * @return int
  52. */
  53. public function count()
  54. {
  55. return count($this->exceptions);
  56. }
  57. /**
  58. * Allows array-like iteration over the request exceptions
  59. *
  60. * @return \ArrayIterator
  61. */
  62. public function getIterator()
  63. {
  64. return new \ArrayIterator($this->exceptions);
  65. }
  66. /**
  67. * Get the first exception in the collection
  68. *
  69. * @return \Exception
  70. */
  71. public function getFirst()
  72. {
  73. return $this->exceptions ? $this->exceptions[0] : null;
  74. }
  75. private function getExceptionMessage(\Exception $e, $depth = 0)
  76. {
  77. static $sp = ' ';
  78. $prefix = $depth ? str_repeat($sp, $depth) : '';
  79. $message = "{$prefix}(" . get_class($e) . ') ' . $e->getFile() . ' line ' . $e->getLine() . "\n";
  80. if ($e instanceof self) {
  81. if ($e->shortMessage) {
  82. $message .= "\n{$prefix}{$sp}" . str_replace("\n", "\n{$prefix}{$sp}", $e->shortMessage) . "\n";
  83. }
  84. foreach ($e as $ee) {
  85. $message .= "\n" . $this->getExceptionMessage($ee, $depth + 1);
  86. }
  87. } else {
  88. $message .= "\n{$prefix}{$sp}" . str_replace("\n", "\n{$prefix}{$sp}", $e->getMessage()) . "\n";
  89. $message .= "\n{$prefix}{$sp}" . str_replace("\n", "\n{$prefix}{$sp}", $e->getTraceAsString()) . "\n";
  90. }
  91. return str_replace(getcwd(), '.', $message);
  92. }
  93. }