LimeError.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /*
  3. * This file is part of the Lime test framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. /**
  12. * Stores an error and optionally its trace.
  13. *
  14. * This class is similar to PHP's native Exception class, but is guaranteed
  15. * to be serializable. The native Exception class is not serializable if the
  16. * traces contain circular references between objects.
  17. *
  18. * @package Lime
  19. * @author Bernhard Schussek <bernhard.schussek@symfony-project.com>
  20. * @version SVN: $Id: LimeError.php 23701 2009-11-08 21:23:40Z bschussek $
  21. */
  22. class LimeError implements Serializable
  23. {
  24. private
  25. $type = null,
  26. $message = null,
  27. $file = null,
  28. $line = null,
  29. $trace = null;
  30. /**
  31. * Creates a new instance and copies the data from an exception.
  32. *
  33. * @param Exception $exception
  34. * @return LimeError
  35. */
  36. public static function fromException(Exception $exception)
  37. {
  38. return new self(
  39. $exception->getMessage(),
  40. $exception->getFile(),
  41. $exception->getLine(),
  42. get_class($exception),
  43. $exception->getTrace()
  44. );
  45. }
  46. /**
  47. * Constructor.
  48. *
  49. * @param string $message The error message
  50. * @param string $file The file where the error occurred
  51. * @param integer $line The line where the error occurred
  52. * @param string $type The error type, f.i. "Fatal Error"
  53. * @param array $trace The traces of the error
  54. */
  55. public function __construct($message, $file, $line, $type = 'Error', array $trace = array())
  56. {
  57. $this->message = $message;
  58. $this->file = $file;
  59. $this->line = $line;
  60. $this->type = $type;
  61. $this->trace = $trace;
  62. }
  63. /**
  64. * Returns the error type.
  65. *
  66. * @return string
  67. */
  68. public function getType()
  69. {
  70. return $this->type;
  71. }
  72. /**
  73. * Returns the error message.
  74. *
  75. * @return string
  76. */
  77. public function getMessage()
  78. {
  79. return $this->message;
  80. }
  81. /**
  82. * Returns the file where the error occurred.
  83. *
  84. * @return string
  85. */
  86. public function getFile()
  87. {
  88. return $this->file;
  89. }
  90. /**
  91. * Returns the line where the error occurred.
  92. *
  93. * @return integer
  94. */
  95. public function getLine()
  96. {
  97. return $this->line;
  98. }
  99. /**
  100. * Returns the trace of the error.
  101. *
  102. * @return array
  103. */
  104. public function getTrace()
  105. {
  106. return $this->trace;
  107. }
  108. /**
  109. * Serializes the error.
  110. *
  111. * @see Serializable#serialize()
  112. * @return string The serialized error content
  113. */
  114. public function serialize()
  115. {
  116. $traces = $this->trace;
  117. foreach ($traces as &$trace)
  118. {
  119. if (array_key_exists('args', $trace))
  120. {
  121. foreach ($trace['args'] as &$value)
  122. {
  123. // TODO: This should be improved. Maybe we can check for recursions
  124. // and only exclude duplicate objects from the trace
  125. if (is_object($value))
  126. {
  127. // replace object by class name
  128. $value = sprintf('object (%s) (...)', get_class($value));
  129. }
  130. else if (is_array($value))
  131. {
  132. $value = 'array(...)';
  133. }
  134. }
  135. }
  136. }
  137. return serialize(array($this->file, $this->line, $this->message, $traces, $this->type));
  138. }
  139. /**
  140. * Unserializes an error.
  141. *
  142. * @see Serializable#unserialize()
  143. * @param string $data The serialized error content
  144. */
  145. public function unserialize($data)
  146. {
  147. list($this->file, $this->line, $this->message, $this->trace, $this->type) = unserialize($data);
  148. }
  149. }