PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/common/libraries/plugin/pear/PEAR/Exception.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 405 lines | 238 code | 17 blank | 150 comment | 25 complexity | 996e9c62fcdd93d65431e615f36068bf MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  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 137 2009-11-09 13:24:37Z vanpouckesven $
  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. {
  128. $code = $p2;
  129. $this->cause = null;
  130. }
  131. elseif (is_object($p2) || is_array($p2))
  132. {
  133. // using is_object allows both Exception and PEAR_Error
  134. if (is_object($p2) && ! ($p2 instanceof Exception))
  135. {
  136. if (! class_exists('PEAR_Error') || ! ($p2 instanceof PEAR_Error))
  137. {
  138. throw new PEAR_Exception('exception cause must be Exception, ' . 'array, or PEAR_Error');
  139. }
  140. }
  141. $code = $p3;
  142. if (is_array($p2) && isset($p2['message']))
  143. {
  144. // fix potential problem of passing in a single warning
  145. $p2 = array($p2);
  146. }
  147. $this->cause = $p2;
  148. }
  149. else
  150. {
  151. $code = null;
  152. $this->cause = null;
  153. }
  154. parent :: __construct($message, $code);
  155. $this->signal();
  156. }
  157. /**
  158. * @param mixed $callback - A valid php callback, see php func is_callable()
  159. * - A PEAR_Exception::OBSERVER_* constant
  160. * - An array(const PEAR_Exception::OBSERVER_*,
  161. * mixed $options)
  162. * @param string $label The name of the observer. Use this if you want
  163. * to remove it later with removeObserver()
  164. */
  165. public static function addObserver($callback, $label = 'default')
  166. {
  167. self :: $_observers[$label] = $callback;
  168. }
  169. public static function removeObserver($label = 'default')
  170. {
  171. unset(self :: $_observers[$label]);
  172. }
  173. /**
  174. * @return int unique identifier for an observer
  175. */
  176. public static function getUniqueId()
  177. {
  178. return self :: $_uniqueid ++;
  179. }
  180. private function signal()
  181. {
  182. foreach (self :: $_observers as $func)
  183. {
  184. if (is_callable($func))
  185. {
  186. call_user_func($func, $this);
  187. continue;
  188. }
  189. settype($func, 'array');
  190. switch ($func[0])
  191. {
  192. case self :: OBSERVER_PRINT :
  193. $f = (isset($func[1])) ? $func[1] : '%s';
  194. printf($f, $this->getMessage());
  195. break;
  196. case self :: OBSERVER_TRIGGER :
  197. $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
  198. trigger_error($this->getMessage(), $f);
  199. break;
  200. case self :: OBSERVER_DIE :
  201. $f = (isset($func[1])) ? $func[1] : '%s';
  202. die(printf($f, $this->getMessage()));
  203. break;
  204. default :
  205. trigger_error('invalid observer type', E_USER_WARNING);
  206. }
  207. }
  208. }
  209. /**
  210. * Return specific error information that can be used for more detailed
  211. * error messages or translation.
  212. *
  213. * This method may be overridden in child exception classes in order
  214. * to add functionality not present in PEAR_Exception and is a placeholder
  215. * to define API
  216. *
  217. * The returned array must be an associative array of parameter => value like so:
  218. * <pre>
  219. * array('name' => $name, 'context' => array(...))
  220. * </pre>
  221. * @return array
  222. */
  223. public function getErrorData()
  224. {
  225. return array();
  226. }
  227. /**
  228. * Returns the exception that caused this exception to be thrown
  229. * @access public
  230. * @return Exception|array The context of the exception
  231. */
  232. public function getCause()
  233. {
  234. return $this->cause;
  235. }
  236. /**
  237. * Function must be public to call on caused exceptions
  238. * @param array
  239. */
  240. public function getCauseMessage(&$causes)
  241. {
  242. $trace = $this->getTraceSafe();
  243. $cause = array('class' => get_class($this), 'message' => $this->message, 'file' => 'unknown',
  244. 'line' => 'unknown');
  245. if (isset($trace[0]))
  246. {
  247. if (isset($trace[0]['file']))
  248. {
  249. $cause['file'] = $trace[0]['file'];
  250. $cause['line'] = $trace[0]['line'];
  251. }
  252. }
  253. $causes[] = $cause;
  254. if ($this->cause instanceof PEAR_Exception)
  255. {
  256. $this->cause->getCauseMessage($causes);
  257. }
  258. elseif ($this->cause instanceof Exception)
  259. {
  260. $causes[] = array('class' => get_class($this->cause), 'message' => $this->cause->getMessage(),
  261. 'file' => $this->cause->getFile(), 'line' => $this->cause->getLine());
  262. }
  263. elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error)
  264. {
  265. $causes[] = array('class' => get_class($this->cause), 'message' => $this->cause->getMessage(),
  266. 'file' => 'unknown', 'line' => 'unknown');
  267. }
  268. elseif (is_array($this->cause))
  269. {
  270. foreach ($this->cause as $cause)
  271. {
  272. if ($cause instanceof PEAR_Exception)
  273. {
  274. $cause->getCauseMessage($causes);
  275. }
  276. elseif ($cause instanceof Exception)
  277. {
  278. $causes[] = array('class' => get_class($cause), 'message' => $cause->getMessage(),
  279. 'file' => $cause->getFile(), 'line' => $cause->getLine());
  280. }
  281. elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error)
  282. {
  283. $causes[] = array('class' => get_class($cause), 'message' => $cause->getMessage(),
  284. 'file' => 'unknown', 'line' => 'unknown');
  285. }
  286. elseif (is_array($cause) && isset($cause['message']))
  287. {
  288. // PEAR_ErrorStack warning
  289. $causes[] = array('class' => $cause['package'], 'message' => $cause['message'],
  290. 'file' => isset($cause['context']['file']) ? $cause['context']['file'] : 'unknown',
  291. 'line' => isset($cause['context']['line']) ? $cause['context']['line'] : 'unknown');
  292. }
  293. }
  294. }
  295. }
  296. public function getTraceSafe()
  297. {
  298. if (! isset($this->_trace))
  299. {
  300. $this->_trace = $this->getTrace();
  301. if (empty($this->_trace))
  302. {
  303. $backtrace = debug_backtrace();
  304. $this->_trace = array($backtrace[count($backtrace) - 1]);
  305. }
  306. }
  307. return $this->_trace;
  308. }
  309. public function getErrorClass()
  310. {
  311. $trace = $this->getTraceSafe();
  312. return $trace[0]['class'];
  313. }
  314. public function getErrorMethod()
  315. {
  316. $trace = $this->getTraceSafe();
  317. return $trace[0]['function'];
  318. }
  319. public function __toString()
  320. {
  321. if (isset($_SERVER['REQUEST_URI']))
  322. {
  323. return $this->toHtml();
  324. }
  325. return $this->toText();
  326. }
  327. public function toHtml()
  328. {
  329. $trace = $this->getTraceSafe();
  330. $causes = array();
  331. $this->getCauseMessage($causes);
  332. $html = '<table border="1" cellspacing="0">' . "\n";
  333. foreach ($causes as $i => $cause)
  334. {
  335. $html .= '<tr><td colspan="3" bgcolor="#ff9999">' . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: ' . htmlspecialchars($cause['message']) . ' in <b>' . $cause['file'] . '</b> ' . 'on line <b>' . $cause['line'] . '</b>' . "</td></tr>\n";
  336. }
  337. $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n" . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>' . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>' . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n";
  338. foreach ($trace as $k => $v)
  339. {
  340. $html .= '<tr><td align="center">' . $k . '</td>' . '<td>';
  341. if (! empty($v['class']))
  342. {
  343. $html .= $v['class'] . $v['type'];
  344. }
  345. $html .= $v['function'];
  346. $args = array();
  347. if (! empty($v['args']))
  348. {
  349. foreach ($v['args'] as $arg)
  350. {
  351. if (is_null($arg))
  352. $args[] = 'null';
  353. elseif (is_array($arg))
  354. $args[] = 'Array';
  355. elseif (is_object($arg))
  356. $args[] = 'Object(' . get_class($arg) . ')';
  357. elseif (is_bool($arg))
  358. $args[] = $arg ? 'true' : 'false';
  359. elseif (is_int($arg) || is_double($arg))
  360. $args[] = $arg;
  361. else
  362. {
  363. $arg = (string) $arg;
  364. $str = htmlspecialchars(substr($arg, 0, 16));
  365. if (strlen($arg) > 16)
  366. $str .= '&hellip;';
  367. $args[] = "'" . $str . "'";
  368. }
  369. }
  370. }
  371. $html .= '(' . implode(', ', $args) . ')' . '</td>' . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown') . ':' . (isset($v['line']) ? $v['line'] : 'unknown') . '</td></tr>' . "\n";
  372. }
  373. $html .= '<tr><td align="center">' . ($k + 1) . '</td>' . '<td>{main}</td>' . '<td>&nbsp;</td></tr>' . "\n" . '</table>';
  374. return $html;
  375. }
  376. public function toText()
  377. {
  378. $causes = array();
  379. $this->getCauseMessage($causes);
  380. $causeMsg = '';
  381. foreach ($causes as $i => $cause)
  382. {
  383. $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': ' . $cause['message'] . ' in ' . $cause['file'] . ' on line ' . $cause['line'] . "\n";
  384. }
  385. return $causeMsg . $this->getTraceAsString();
  386. }
  387. }
  388. ?>