XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
Exception.php
Go to the documentation of this file.
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
97 class PEAR_Exception extends Exception
98 {
99  const OBSERVER_PRINT = -2;
100  const OBSERVER_TRIGGER = -4;
101  const OBSERVER_DIE = -8;
102  protected $cause;
103  private static $_observers = array();
104  private static $_uniqueid = 0;
105  private $_trace;
106 
121  public function __construct($message, $p2 = null, $p3 = null)
122  {
123  if (is_int($p2)) {
124  $code = $p2;
125  $this->cause = null;
126  } elseif (is_object($p2) || is_array($p2)) {
127  // using is_object allows both Exception and PEAR_Error
128  if (is_object($p2) && !($p2 instanceof Exception)) {
129  if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) {
130  throw new PEAR_Exception('exception cause must be Exception, ' .
131  'array, or PEAR_Error');
132  }
133  }
134  $code = $p3;
135  if (is_array($p2) && isset($p2['message'])) {
136  // fix potential problem of passing in a single warning
137  $p2 = array($p2);
138  }
139  $this->cause = $p2;
140  } else {
141  $code = null;
142  $this->cause = null;
143  }
144  parent::__construct($message, $code);
145  $this->signal();
146  }
147 
156  public static function addObserver($callback, $label = 'default')
157  {
158  self::$_observers[$label] = $callback;
159  }
160 
161  public static function removeObserver($label = 'default')
162  {
163  unset(self::$_observers[$label]);
164  }
165 
169  public static function getUniqueId()
170  {
171  return self::$_uniqueid++;
172  }
173 
174  private function signal()
175  {
176  foreach (self::$_observers as $func) {
177  if (is_callable($func)) {
178  call_user_func($func, $this);
179  continue;
180  }
181  settype($func, 'array');
182  switch ($func[0]) {
183  case self::OBSERVER_PRINT :
184  $f = (isset($func[1])) ? $func[1] : '%s';
185  printf($f, $this->getMessage());
186  break;
187  case self::OBSERVER_TRIGGER :
188  $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
189  trigger_error($this->getMessage(), $f);
190  break;
191  case self::OBSERVER_DIE :
192  $f = (isset($func[1])) ? $func[1] : '%s';
193  die(printf($f, $this->getMessage()));
194  break;
195  default:
196  trigger_error('invalid observer type', E_USER_WARNING);
197  }
198  }
199  }
200 
215  public function getErrorData()
216  {
217  return array();
218  }
219 
225  public function getCause()
226  {
227  return $this->cause;
228  }
229 
234  public function getCauseMessage(&$causes)
235  {
236  $trace = $this->getTraceSafe();
237  $cause = array('class' => get_class($this),
238  'message' => $this->message,
239  'file' => 'unknown',
240  'line' => 'unknown');
241  if (isset($trace[0])) {
242  if (isset($trace[0]['file'])) {
243  $cause['file'] = $trace[0]['file'];
244  $cause['line'] = $trace[0]['line'];
245  }
246  }
247  $causes[] = $cause;
248  if ($this->cause instanceof PEAR_Exception) {
249  $this->cause->getCauseMessage($causes);
250  } elseif ($this->cause instanceof Exception) {
251  $causes[] = array('class' => get_class($this->cause),
252  'message' => $this->cause->getMessage(),
253  'file' => $this->cause->getFile(),
254  'line' => $this->cause->getLine());
255  } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) {
256  $causes[] = array('class' => get_class($this->cause),
257  'message' => $this->cause->getMessage(),
258  'file' => 'unknown',
259  'line' => 'unknown');
260  } elseif (is_array($this->cause)) {
261  foreach ($this->cause as $cause) {
262  if ($cause instanceof PEAR_Exception) {
263  $cause->getCauseMessage($causes);
264  } elseif ($cause instanceof Exception) {
265  $causes[] = array('class' => get_class($cause),
266  'message' => $cause->getMessage(),
267  'file' => $cause->getFile(),
268  'line' => $cause->getLine());
269  } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) {
270  $causes[] = array('class' => get_class($cause),
271  'message' => $cause->getMessage(),
272  'file' => 'unknown',
273  'line' => 'unknown');
274  } elseif (is_array($cause) && isset($cause['message'])) {
275  // PEAR_ErrorStack warning
276  $causes[] = array(
277  'class' => $cause['package'],
278  'message' => $cause['message'],
279  'file' => isset($cause['context']['file']) ?
280  $cause['context']['file'] :
281  'unknown',
282  'line' => isset($cause['context']['line']) ?
283  $cause['context']['line'] :
284  'unknown',
285  );
286  }
287  }
288  }
289  }
290 
291  public function getTraceSafe()
292  {
293  if (!isset($this->_trace)) {
294  $this->_trace = $this->getTrace();
295  if (empty($this->_trace)) {
296  $backtrace = debug_backtrace();
297  $this->_trace = array($backtrace[count($backtrace)-1]);
298  }
299  }
300  return $this->_trace;
301  }
302 
303  public function getErrorClass()
304  {
305  $trace = $this->getTraceSafe();
306  return $trace[0]['class'];
307  }
308 
309  public function getErrorMethod()
310  {
311  $trace = $this->getTraceSafe();
312  return $trace[0]['function'];
313  }
314 
315  public function __toString()
316  {
317  if (isset($_SERVER['REQUEST_URI'])) {
318  return $this->toHtml();
319  }
320  return $this->toText();
321  }
322 
323  public function toHtml()
324  {
325  $trace = $this->getTraceSafe();
326  $causes = array();
327  $this->getCauseMessage($causes);
328  $html = '<table style="border: 1px" cellspacing="0">' . "\n";
329  foreach ($causes as $i => $cause) {
330  $html .= '<tr><td colspan="3" style="background: #ff9999">'
331  . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
332  . htmlspecialchars($cause['message']) . ' in <b>' . $cause['file'] . '</b> '
333  . 'on line <b>' . $cause['line'] . '</b>'
334  . "</td></tr>\n";
335  }
336  $html .= '<tr><td colspan="3" style="background-color: #aaaaaa; text-align: center; font-weight: bold;">Exception trace</td></tr>' . "\n"
337  . '<tr><td style="text-align: center; background: #cccccc; width:20px; font-weight: bold;">#</td>'
338  . '<td style="text-align: center; background: #cccccc; font-weight: bold;">Function</td>'
339  . '<td style="text-align: center; background: #cccccc; font-weight: bold;">Location</td></tr>' . "\n";
340 
341  foreach ($trace as $k => $v) {
342  $html .= '<tr><td style="text-align: center;">' . $k . '</td>'
343  . '<td>';
344  if (!empty($v['class'])) {
345  $html .= $v['class'] . $v['type'];
346  }
347  $html .= $v['function'];
348  $args = array();
349  if (!empty($v['args'])) {
350  foreach ($v['args'] as $arg) {
351  if (is_null($arg)) $args[] = 'null';
352  elseif (is_array($arg)) $args[] = 'Array';
353  elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')';
354  elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false';
355  elseif (is_int($arg) || is_double($arg)) $args[] = $arg;
356  else {
357  $arg = (string)$arg;
358  $str = htmlspecialchars(substr($arg, 0, 16));
359  if (strlen($arg) > 16) $str .= '&hellip;';
360  $args[] = "'" . $str . "'";
361  }
362  }
363  }
364  $html .= '(' . implode(', ',$args) . ')'
365  . '</td>'
366  . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown')
367  . ':' . (isset($v['line']) ? $v['line'] : 'unknown')
368  . '</td></tr>' . "\n";
369  }
370  $html .= '<tr><td style="text-align: center;">' . ($k+1) . '</td>'
371  . '<td>{main}</td>'
372  . '<td>&nbsp;</td></tr>' . "\n"
373  . '</table>';
374  return $html;
375  }
376 
377  public function toText()
378  {
379  $causes = array();
380  $this->getCauseMessage($causes);
381  $causeMsg = '';
382  foreach ($causes as $i => $cause) {
383  $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
384  . $cause['message'] . ' in ' . $cause['file']
385  . ' on line ' . $cause['line'] . "\n";
386  }
387  return $causeMsg . $this->getTraceAsString();
388  }
389 }
static removeObserver($label= 'default')
Definition: Exception.php:161
__construct($message, $p2=null, $p3=null)
Definition: Exception.php:121
static getUniqueId()
Definition: Exception.php:169
high class of message module
static addObserver($callback, $label= 'default')
Definition: Exception.php:156
getCauseMessage(&$causes)
Definition: Exception.php:234
$args
Definition: ko.install.php:185
const OBSERVER_DIE
Definition: Exception.php:101
const OBSERVER_TRIGGER
Definition: Exception.php:100
const OBSERVER_PRINT
Definition: Exception.php:99