PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/system/php/lang/Exception.php

https://github.com/tstarling/hiphop-php
PHP | 226 lines | 106 code | 18 blank | 102 comment | 11 complexity | 816672a50317f2d8f06af86125ba7fea MD5 | raw file
  1. <?php
  2. // This doc comment block generated by idl/sysdoc.php
  3. /**
  4. * ( excerpt from http://php.net/manual/en/class.exception.php )
  5. *
  6. * Exception is the base class for all Exceptions.
  7. *
  8. */
  9. class Exception {
  10. protected $message = ''; // exception message
  11. protected $code = 0; // user defined exception code
  12. protected $previous = null;
  13. protected $file; // source filename of exception
  14. protected $line; // source line of exception
  15. protected $trace; // full stacktrace
  16. private $inited = false;
  17. private static $traceOpts = 0;
  18. /**
  19. * This cannot be implemented in __construct, because a derived class may
  20. * implement its own __construct, losing the stacktrace. The runtime has
  21. * special logic to call the __init__ method on instances of Exception before
  22. * calling __construct just to make sure $this->trace is always populated.
  23. */
  24. final function __init__() {
  25. if ($this->inited) {
  26. return;
  27. }
  28. $this->initTrace();
  29. $this->inited = true;
  30. }
  31. // This doc comment block generated by idl/sysdoc.php
  32. /**
  33. * ( excerpt from http://php.net/manual/en/exception.construct.php )
  34. *
  35. * Constructs the Exception.
  36. *
  37. * @message mixed The Exception message to throw.
  38. * @code mixed The Exception code.
  39. * @previous mixed The previous exception used for the exception
  40. * chaining.
  41. */
  42. function __construct($message = '', $code = 0, Exception $previous = null) {
  43. $this->message = $message;
  44. $this->code = $code;
  45. $this->previous = $previous;
  46. }
  47. // This doc comment block generated by idl/sysdoc.php
  48. /**
  49. * ( excerpt from http://php.net/manual/en/exception.getmessage.php )
  50. *
  51. * Returns the Exception message.
  52. *
  53. * @return mixed Returns the Exception message as a string.
  54. */
  55. function getMessage() {
  56. return $this->message;
  57. }
  58. // This doc comment block generated by idl/sysdoc.php
  59. /**
  60. * ( excerpt from http://php.net/manual/en/exception.getprevious.php )
  61. *
  62. * Returns previous Exception (the third parameter of
  63. * Exception::__construct()).
  64. *
  65. * @return mixed Returns the previous Exception if available or NULL
  66. * otherwise.
  67. */
  68. final function getPrevious() {
  69. return $this->previous;
  70. }
  71. final function setPrevious(Exception $previous) {
  72. $this->previous = $previous;
  73. }
  74. final function setPreviousChain(Exception $previous) {
  75. $cur = $this;
  76. $next = $cur->getPrevious();
  77. while ($next instanceof Exception) {
  78. $cur = $next;
  79. $next = $cur->getPrevious();
  80. }
  81. $cur->setPrevious($previous);
  82. }
  83. // This doc comment block generated by idl/sysdoc.php
  84. /**
  85. * ( excerpt from http://php.net/manual/en/exception.getcode.php )
  86. *
  87. * Returns the Exception code.
  88. *
  89. * @return mixed Returns the exception code as integer in Exception
  90. * but possibly as other type in Exception descendants
  91. * (for example as string in PDOException).
  92. */
  93. function getCode() {
  94. return $this->code;
  95. }
  96. // This doc comment block generated by idl/sysdoc.php
  97. /**
  98. * ( excerpt from http://php.net/manual/en/exception.getfile.php )
  99. *
  100. * Get the name of the file the exception was created.
  101. *
  102. * @return mixed Returns the filename in which the exception was
  103. * created.
  104. */
  105. final function getFile() {
  106. return $this->file;
  107. }
  108. // This doc comment block generated by idl/sysdoc.php
  109. /**
  110. * ( excerpt from http://php.net/manual/en/exception.getline.php )
  111. *
  112. * Get line number where the exception was created.
  113. *
  114. * @return mixed Returns the line number where the exception was
  115. * created.
  116. */
  117. final function getLine() {
  118. return $this->line;
  119. }
  120. // This doc comment block generated by idl/sysdoc.php
  121. /**
  122. * ( excerpt from http://php.net/manual/en/exception.gettrace.php )
  123. *
  124. * Returns the Exception stack trace.
  125. *
  126. * @return mixed Returns the Exception stack trace as an array.
  127. */
  128. final function getTrace() {
  129. return $this->trace;
  130. }
  131. // This doc comment block generated by idl/sysdoc.php
  132. /**
  133. * ( excerpt from http://php.net/manual/en/exception.gettraceasstring.php )
  134. *
  135. * Returns the Exception stack trace as a string.
  136. *
  137. * @return mixed Returns the Exception stack trace as a string.
  138. */
  139. final function getTraceAsString() {
  140. $i = 0;
  141. $s = "";
  142. foreach ($this->getTrace() as $frame) {
  143. if (!is_array($frame)) continue;
  144. $s .= "#$i " .
  145. (isset($frame['file']) ? $frame['file'] : "") . "(" .
  146. (isset($frame['line']) ? $frame['line'] : "") . "): " .
  147. (isset($frame['class']) ? $frame['class'] . $frame['type'] : "") .
  148. $frame['function'] . "()\n";
  149. $i++;
  150. }
  151. $s .= "#$i {main}";
  152. return $s;
  153. }
  154. /* Overrideable */
  155. // formated string for display
  156. // This doc comment block generated by idl/sysdoc.php
  157. /**
  158. * ( excerpt from http://php.net/manual/en/exception.tostring.php )
  159. *
  160. * Returns the string representation of the exception.
  161. *
  162. * @return mixed Returns the string representation of the exception.
  163. */
  164. function __toString() {
  165. $res = "";
  166. $lst = array();
  167. $ex = $this;
  168. while ($ex != null) {
  169. $lst[] = $ex;
  170. $ex = $ex->previous;
  171. }
  172. $lst = array_reverse($lst);
  173. foreach ($lst as $i => $ex) {
  174. if ($i > 0) {
  175. $res .= "\n\nNext ";
  176. }
  177. $res .= "exception '" . get_class($ex) . "' with message '" .
  178. $ex->getMessage() . "' in " . $ex->getFile() . ":" .
  179. $ex->getLine() . "\nStack trace:\n" . $ex->getTraceAsString();
  180. }
  181. return $res;
  182. }
  183. /**
  184. * Derived classes may override the methods below if different behavior
  185. * for initializing the trace is desired
  186. */
  187. protected function initTrace() {
  188. $this->trace = debug_backtrace(static::getTraceOptions());
  189. // Remove top stack frames up to and including Exception::__init__,
  190. // set the 'file' and 'line' properties appropriately
  191. while (!empty($this->trace)) {
  192. $top = array_shift($this->trace);
  193. if (isset($top['class']) && isset($top['function']) &&
  194. strcasecmp($top['class'], 'exception') === 0 &&
  195. strcasecmp($top['function'], '__init__') === 0) {
  196. if (isset($top['file'])) $this->file = $top['file'];
  197. if (isset($top['line'])) $this->line = $top['line'];
  198. return;
  199. }
  200. }
  201. }
  202. public static function getTraceOptions() {
  203. return self::$traceOpts;
  204. }
  205. public static function setTraceOptions($opts) {
  206. self::$traceOpts = (int)$opts;
  207. }
  208. }