PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/pear/PEAR/Exception.php

https://github.com/axxtel/agilebill
PHP | 397 lines | 229 code | 18 blank | 150 comment | 27 complexity | 60040e0928e18981a9ccb036e54db840 MD5 | raw file
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. /**
  4. * PEAR_Exception
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category pear
  15. * @package PEAR
  16. * @author Tomas V. V. Cox <cox@idecnet.com>
  17. * @author Hans Lellelid <hans@velum.net>
  18. * @author Bertrand Mansion <bmansion@mamasam.com>
  19. * @author Greg Beaver <cellog@php.net>
  20. * @copyright 1997-2008 The PHP Group
  21. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  22. * @version CVS: $Id: Exception.php,v 1.29 2008/01/03 20:26:35 cellog Exp $
  23. * @link http://pear.php.net/package/PEAR
  24. * @since File available since Release 1.3.3
  25. */
  26. /**
  27. * Base PEAR_Exception Class
  28. *
  29. * 1) Features:
  30. *
  31. * - Nestable exceptions (throw new PEAR_Exception($msg, $prev_exception))
  32. * - Definable triggers, shot when exceptions occur
  33. * - Pretty and informative error messages
  34. * - Added more context info available (like class, method or cause)
  35. * - cause can be a PEAR_Exception or an array of mixed
  36. * PEAR_Exceptions/PEAR_ErrorStack warnings
  37. * - callbacks for specific exception classes and their children
  38. *
  39. * 2) Ideas:
  40. *
  41. * - Maybe a way to define a 'template' for the output
  42. *
  43. * 3) Inherited properties from PHP Exception Class:
  44. *
  45. * protected $message
  46. * protected $code
  47. * protected $line
  48. * protected $file
  49. * private $trace
  50. *
  51. * 4) Inherited methods from PHP Exception Class:
  52. *
  53. * __clone
  54. * __construct
  55. * getMessage
  56. * getCode
  57. * getFile
  58. * getLine
  59. * getTraceSafe
  60. * getTraceSafeAsString
  61. * __toString
  62. *
  63. * 5) Usage example
  64. *
  65. * <code>
  66. * require_once 'PEAR/Exception.php';
  67. *
  68. * class Test {
  69. * function foo() {
  70. * throw new PEAR_Exception('Error Message', ERROR_CODE);
  71. * }
  72. * }
  73. *
  74. * function myLogger($pear_exception) {
  75. * echo $pear_exception->getMessage();
  76. * }
  77. * // each time a exception is thrown the 'myLogger' will be called
  78. * // (its use is completely optional)
  79. * PEAR_Exception::addObserver('myLogger');
  80. * $test = new Test;
  81. * try {
  82. * $test->foo();
  83. * } catch (PEAR_Exception $e) {
  84. * print $e;
  85. * }
  86. * </code>
  87. *
  88. * @category pear
  89. * @package PEAR
  90. * @author Tomas V.V.Cox <cox@idecnet.com>
  91. * @author Hans Lellelid <hans@velum.net>
  92. * @author Bertrand Mansion <bmansion@mamasam.com>
  93. * @author Greg Beaver <cellog@php.net>
  94. * @copyright 1997-2008 The PHP Group
  95. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  96. * @version Release: 1.7.2
  97. * @link http://pear.php.net/package/PEAR
  98. * @since Class available since Release 1.3.3
  99. *
  100. */
  101. class PEAR_Exception extends Exception
  102. {
  103. const OBSERVER_PRINT = -2;
  104. const OBSERVER_TRIGGER = -4;
  105. const OBSERVER_DIE = -8;
  106. protected $cause;
  107. private static $_observers = array();
  108. private static $_uniqueid = 0;
  109. private $_trace;
  110. /**
  111. * Supported signatures:
  112. * - PEAR_Exception(string $message);
  113. * - PEAR_Exception(string $message, int $code);
  114. * - PEAR_Exception(string $message, Exception $cause);
  115. * - PEAR_Exception(string $message, Exception $cause, int $code);
  116. * - PEAR_Exception(string $message, PEAR_Error $cause);
  117. * - PEAR_Exception(string $message, PEAR_Error $cause, int $code);
  118. * - PEAR_Exception(string $message, array $causes);
  119. * - PEAR_Exception(string $message, array $causes, int $code);
  120. * @param string exception message
  121. * @param int|Exception|PEAR_Error|array|null exception cause
  122. * @param int|null exception code or null
  123. */
  124. public function __construct($message, $p2 = null, $p3 = null)
  125. {
  126. if (is_int($p2)) {
  127. $code = $p2;
  128. $this->cause = null;
  129. } elseif (is_object($p2) || is_array($p2)) {
  130. // using is_object allows both Exception and PEAR_Error
  131. if (is_object($p2) && !($p2 instanceof Exception)) {
  132. if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) {
  133. throw new PEAR_Exception('exception cause must be Exception, ' .
  134. 'array, or PEAR_Error');
  135. }
  136. }
  137. $code = $p3;
  138. if (is_array($p2) && isset($p2['message'])) {
  139. // fix potential problem of passing in a single warning
  140. $p2 = array($p2);
  141. }
  142. $this->cause = $p2;
  143. } else {
  144. $code = null;
  145. $this->cause = null;
  146. }
  147. parent::__construct($message, $code);
  148. $this->signal();
  149. }
  150. /**
  151. * @param mixed $callback - A valid php callback, see php func is_callable()
  152. * - A PEAR_Exception::OBSERVER_* constant
  153. * - An array(const PEAR_Exception::OBSERVER_*,
  154. * mixed $options)
  155. * @param string $label The name of the observer. Use this if you want
  156. * to remove it later with removeObserver()
  157. */
  158. public static function addObserver($callback, $label = 'default')
  159. {
  160. self::$_observers[$label] = $callback;
  161. }
  162. public static function removeObserver($label = 'default')
  163. {
  164. unset(self::$_observers[$label]);
  165. }
  166. /**
  167. * @return int unique identifier for an observer
  168. */
  169. public static function getUniqueId()
  170. {
  171. return self::$_uniqueid++;
  172. }
  173. private function signal()
  174. {
  175. foreach (self::$_observers as $func) {
  176. if (is_callable($func)) {
  177. call_user_func($func, $this);
  178. continue;
  179. }
  180. settype($func, 'array');
  181. switch ($func[0]) {
  182. case self::OBSERVER_PRINT :
  183. $f = (isset($func[1])) ? $func[1] : '%s';
  184. printf($f, $this->getMessage());
  185. break;
  186. case self::OBSERVER_TRIGGER :
  187. $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
  188. trigger_error($this->getMessage(), $f);
  189. break;
  190. case self::OBSERVER_DIE :
  191. $f = (isset($func[1])) ? $func[1] : '%s';
  192. die(printf($f, $this->getMessage()));
  193. break;
  194. default:
  195. trigger_error('invalid observer type', E_USER_WARNING);
  196. }
  197. }
  198. }
  199. /**
  200. * Return specific error information that can be used for more detailed
  201. * error messages or translation.
  202. *
  203. * This method may be overridden in child exception classes in order
  204. * to add functionality not present in PEAR_Exception and is a placeholder
  205. * to define API
  206. *
  207. * The returned array must be an associative array of parameter => value like so:
  208. * <pre>
  209. * array('name' => $name, 'context' => array(...))
  210. * </pre>
  211. * @return array
  212. */
  213. public function getErrorData()
  214. {
  215. return array();
  216. }
  217. /**
  218. * Returns the exception that caused this exception to be thrown
  219. * @access public
  220. * @return Exception|array The context of the exception
  221. */
  222. public function getCause()
  223. {
  224. return $this->cause;
  225. }
  226. /**
  227. * Function must be public to call on caused exceptions
  228. * @param array
  229. */
  230. public function getCauseMessage(&$causes)
  231. {
  232. $trace = $this->getTraceSafe();
  233. $cause = array('class' => get_class($this),
  234. 'message' => $this->message,
  235. 'file' => 'unknown',
  236. 'line' => 'unknown');
  237. if (isset($trace[0])) {
  238. if (isset($trace[0]['file'])) {
  239. $cause['file'] = $trace[0]['file'];
  240. $cause['line'] = $trace[0]['line'];
  241. }
  242. }
  243. $causes[] = $cause;
  244. if ($this->cause instanceof PEAR_Exception) {
  245. $this->cause->getCauseMessage($causes);
  246. } elseif ($this->cause instanceof Exception) {
  247. $causes[] = array('class' => get_class($this->cause),
  248. 'message' => $this->cause->getMessage(),
  249. 'file' => $this->cause->getFile(),
  250. 'line' => $this->cause->getLine());
  251. } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) {
  252. $causes[] = array('class' => get_class($this->cause),
  253. 'message' => $this->cause->getMessage(),
  254. 'file' => 'unknown',
  255. 'line' => 'unknown');
  256. } elseif (is_array($this->cause)) {
  257. foreach ($this->cause as $cause) {
  258. if ($cause instanceof PEAR_Exception) {
  259. $cause->getCauseMessage($causes);
  260. } elseif ($cause instanceof Exception) {
  261. $causes[] = array('class' => get_class($cause),
  262. 'message' => $cause->getMessage(),
  263. 'file' => $cause->getFile(),
  264. 'line' => $cause->getLine());
  265. } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) {
  266. $causes[] = array('class' => get_class($cause),
  267. 'message' => $cause->getMessage(),
  268. 'file' => 'unknown',
  269. 'line' => 'unknown');
  270. } elseif (is_array($cause) && isset($cause['message'])) {
  271. // PEAR_ErrorStack warning
  272. $causes[] = array(
  273. 'class' => $cause['package'],
  274. 'message' => $cause['message'],
  275. 'file' => isset($cause['context']['file']) ?
  276. $cause['context']['file'] :
  277. 'unknown',
  278. 'line' => isset($cause['context']['line']) ?
  279. $cause['context']['line'] :
  280. 'unknown',
  281. );
  282. }
  283. }
  284. }
  285. }
  286. public function getTraceSafe()
  287. {
  288. if (!isset($this->_trace)) {
  289. $this->_trace = $this->getTrace();
  290. if (empty($this->_trace)) {
  291. $backtrace = debug_backtrace();
  292. $this->_trace = array($backtrace[count($backtrace)-1]);
  293. }
  294. }
  295. return $this->_trace;
  296. }
  297. public function getErrorClass()
  298. {
  299. $trace = $this->getTraceSafe();
  300. return $trace[0]['class'];
  301. }
  302. public function getErrorMethod()
  303. {
  304. $trace = $this->getTraceSafe();
  305. return $trace[0]['function'];
  306. }
  307. public function __toString()
  308. {
  309. if (isset($_SERVER['REQUEST_URI'])) {
  310. return $this->toHtml();
  311. }
  312. return $this->toText();
  313. }
  314. public function toHtml()
  315. {
  316. $trace = $this->getTraceSafe();
  317. $causes = array();
  318. $this->getCauseMessage($causes);
  319. $html = '<table border="1" cellspacing="0">' . "\n";
  320. foreach ($causes as $i => $cause) {
  321. $html .= '<tr><td colspan="3" bgcolor="#ff9999">'
  322. . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
  323. . htmlspecialchars($cause['message']) . ' in <b>' . $cause['file'] . '</b> '
  324. . 'on line <b>' . $cause['line'] . '</b>'
  325. . "</td></tr>\n";
  326. }
  327. $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n"
  328. . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>'
  329. . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>'
  330. . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n";
  331. foreach ($trace as $k => $v) {
  332. $html .= '<tr><td align="center">' . $k . '</td>'
  333. . '<td>';
  334. if (!empty($v['class'])) {
  335. $html .= $v['class'] . $v['type'];
  336. }
  337. $html .= $v['function'];
  338. $args = array();
  339. if (!empty($v['args'])) {
  340. foreach ($v['args'] as $arg) {
  341. if (is_null($arg)) $args[] = 'null';
  342. elseif (is_array($arg)) $args[] = 'Array';
  343. elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')';
  344. elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false';
  345. elseif (is_int($arg) || is_double($arg)) $args[] = $arg;
  346. else {
  347. $arg = (string)$arg;
  348. $str = htmlspecialchars(substr($arg, 0, 16));
  349. if (strlen($arg) > 16) $str .= '&hellip;';
  350. $args[] = "'" . $str . "'";
  351. }
  352. }
  353. }
  354. $html .= '(' . implode(', ',$args) . ')'
  355. . '</td>'
  356. . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown')
  357. . ':' . (isset($v['line']) ? $v['line'] : 'unknown')
  358. . '</td></tr>' . "\n";
  359. }
  360. $html .= '<tr><td align="center">' . ($k+1) . '</td>'
  361. . '<td>{main}</td>'
  362. . '<td>&nbsp;</td></tr>' . "\n"
  363. . '</table>';
  364. return $html;
  365. }
  366. public function toText()
  367. {
  368. $causes = array();
  369. $this->getCauseMessage($causes);
  370. $causeMsg = '';
  371. foreach ($causes as $i => $cause) {
  372. $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
  373. . $cause['message'] . ' in ' . $cause['file']
  374. . ' on line ' . $cause['line'] . "\n";
  375. }
  376. return $causeMsg . $this->getTraceAsString();
  377. }
  378. }
  379. ?>