TYPO3  7.6
vendor/pear/pear_exception/PEAR/Exception.php
Go to the documentation of this file.
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
95 class PEAR_Exception extends Exception
96 {
97  const OBSERVER_PRINT = -2;
98  const OBSERVER_TRIGGER = -4;
99  const OBSERVER_DIE = -8;
100  protected $cause;
101  private static $_observers = array();
102  private static $_uniqueid = 0;
103  private $_trace;
104 
120  public function __construct($message, $p2 = null, $p3 = null)
121  {
122  if (is_int($p2)) {
123  $code = $p2;
124  $this->cause = null;
125  } elseif (is_object($p2) || is_array($p2)) {
126  // using is_object allows both Exception and PEAR_Error
127  if (is_object($p2) && !($p2 instanceof Exception)) {
128  if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) {
129  throw new PEAR_Exception(
130  'exception cause must be Exception, ' .
131  'array, or PEAR_Error'
132  );
133  }
134  }
135  $code = $p3;
136  if (is_array($p2) && isset($p2['message'])) {
137  // fix potential problem of passing in a single warning
138  $p2 = array($p2);
139  }
140  $this->cause = $p2;
141  } else {
142  $code = null;
143  $this->cause = null;
144  }
145  parent::__construct($message, $code);
146  $this->signal();
147  }
148 
161  public static function addObserver($callback, $label = 'default')
162  {
163  self::$_observers[$label] = $callback;
164  }
165 
173  public static function removeObserver($label = 'default')
174  {
175  unset(self::$_observers[$label]);
176  }
177 
183  public static function getUniqueId()
184  {
185  return self::$_uniqueid++;
186  }
187 
193  protected function signal()
194  {
195  foreach (self::$_observers as $func) {
196  if (is_callable($func)) {
197  call_user_func($func, $this);
198  continue;
199  }
200  settype($func, 'array');
201  switch ($func[0]) {
202  case self::OBSERVER_PRINT :
203  $f = (isset($func[1])) ? $func[1] : '%s';
204  printf($f, $this->getMessage());
205  break;
206  case self::OBSERVER_TRIGGER :
207  $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
208  trigger_error($this->getMessage(), $f);
209  break;
210  case self::OBSERVER_DIE :
211  $f = (isset($func[1])) ? $func[1] : '%s';
212  die(printf($f, $this->getMessage()));
213  break;
214  default:
215  trigger_error('invalid observer type', E_USER_WARNING);
216  }
217  }
218  }
219 
235  public function getErrorData()
236  {
237  return array();
238  }
239 
245  public function getCause()
246  {
247  return $this->cause;
248  }
249 
257  public function getCauseMessage(&$causes)
258  {
259  $trace = $this->getTraceSafe();
260  $cause = array('class' => get_class($this),
261  'message' => $this->message,
262  'file' => 'unknown',
263  'line' => 'unknown');
264  if (isset($trace[0])) {
265  if (isset($trace[0]['file'])) {
266  $cause['file'] = $trace[0]['file'];
267  $cause['line'] = $trace[0]['line'];
268  }
269  }
270  $causes[] = $cause;
271  if ($this->cause instanceof PEAR_Exception) {
272  $this->cause->getCauseMessage($causes);
273  } elseif ($this->cause instanceof Exception) {
274  $causes[] = array('class' => get_class($this->cause),
275  'message' => $this->cause->getMessage(),
276  'file' => $this->cause->getFile(),
277  'line' => $this->cause->getLine());
278  } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) {
279  $causes[] = array('class' => get_class($this->cause),
280  'message' => $this->cause->getMessage(),
281  'file' => 'unknown',
282  'line' => 'unknown');
283  } elseif (is_array($this->cause)) {
284  foreach ($this->cause as $cause) {
285  if ($cause instanceof PEAR_Exception) {
286  $cause->getCauseMessage($causes);
287  } elseif ($cause instanceof Exception) {
288  $causes[] = array('class' => get_class($cause),
289  'message' => $cause->getMessage(),
290  'file' => $cause->getFile(),
291  'line' => $cause->getLine());
292  } elseif (class_exists('PEAR_Error')
293  && $cause instanceof PEAR_Error
294  ) {
295  $causes[] = array('class' => get_class($cause),
296  'message' => $cause->getMessage(),
297  'file' => 'unknown',
298  'line' => 'unknown');
299  } elseif (is_array($cause) && isset($cause['message'])) {
300  // PEAR_ErrorStack warning
301  $causes[] = array(
302  'class' => $cause['package'],
303  'message' => $cause['message'],
304  'file' => isset($cause['context']['file']) ?
305  $cause['context']['file'] :
306  'unknown',
307  'line' => isset($cause['context']['line']) ?
308  $cause['context']['line'] :
309  'unknown',
310  );
311  }
312  }
313  }
314  }
315 
321  public function getTraceSafe()
322  {
323  if (!isset($this->_trace)) {
324  $this->_trace = $this->getTrace();
325  if (empty($this->_trace)) {
326  $backtrace = debug_backtrace();
327  $this->_trace = array($backtrace[count($backtrace)-1]);
328  }
329  }
330  return $this->_trace;
331  }
332 
338  public function getErrorClass()
339  {
340  $trace = $this->getTraceSafe();
341  return $trace[0]['class'];
342  }
343 
349  public function getErrorMethod()
350  {
351  $trace = $this->getTraceSafe();
352  return $trace[0]['function'];
353  }
354 
363  public function __toString()
364  {
365  if (isset($_SERVER['REQUEST_URI'])) {
366  return $this->toHtml();
367  }
368  return $this->toText();
369  }
370 
376  public function toHtml()
377  {
378  $trace = $this->getTraceSafe();
379  $causes = array();
380  $this->getCauseMessage($causes);
381  $html = '<table style="border: 1px" cellspacing="0">' . "\n";
382  foreach ($causes as $i => $cause) {
383  $html .= '<tr><td colspan="3" style="background: #ff9999">'
384  . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
385  . htmlspecialchars($cause['message'])
386  . ' in <b>' . $cause['file'] . '</b> '
387  . 'on line <b>' . $cause['line'] . '</b>'
388  . "</td></tr>\n";
389  }
390  $html .= '<tr><td colspan="3" style="background-color: #aaaaaa; text-align: center; font-weight: bold;">Exception trace</td></tr>' . "\n"
391  . '<tr><td style="text-align: center; background: #cccccc; width:20px; font-weight: bold;">#</td>'
392  . '<td style="text-align: center; background: #cccccc; font-weight: bold;">Function</td>'
393  . '<td style="text-align: center; background: #cccccc; font-weight: bold;">Location</td></tr>' . "\n";
394 
395  foreach ($trace as $k => $v) {
396  $html .= '<tr><td style="text-align: center;">' . $k . '</td>'
397  . '<td>';
398  if (!empty($v['class'])) {
399  $html .= $v['class'] . $v['type'];
400  }
401  $html .= $v['function'];
402  $args = array();
403  if (!empty($v['args'])) {
404  foreach ($v['args'] as $arg) {
405  if (is_null($arg)) {
406  $args[] = 'null';
407  } else if (is_array($arg)) {
408  $args[] = 'Array';
409  } else if (is_object($arg)) {
410  $args[] = 'Object('.get_class($arg).')';
411  } else if (is_bool($arg)) {
412  $args[] = $arg ? 'true' : 'false';
413  } else if (is_int($arg) || is_double($arg)) {
414  $args[] = $arg;
415  } else {
416  $arg = (string)$arg;
417  $str = htmlspecialchars(substr($arg, 0, 16));
418  if (strlen($arg) > 16) {
419  $str .= '&hellip;';
420  }
421  $args[] = "'" . $str . "'";
422  }
423  }
424  }
425  $html .= '(' . implode(', ', $args) . ')'
426  . '</td>'
427  . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown')
428  . ':' . (isset($v['line']) ? $v['line'] : 'unknown')
429  . '</td></tr>' . "\n";
430  }
431  $html .= '<tr><td style="text-align: center;">' . ($k+1) . '</td>'
432  . '<td>{main}</td>'
433  . '<td>&nbsp;</td></tr>' . "\n"
434  . '</table>';
435  return $html;
436  }
437 
443  public function toText()
444  {
445  $causes = array();
446  $this->getCauseMessage($causes);
447  $causeMsg = '';
448  foreach ($causes as $i => $cause) {
449  $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
450  . $cause['message'] . ' in ' . $cause['file']
451  . ' on line ' . $cause['line'] . "\n";
452  }
453  return $causeMsg . $this->getTraceAsString();
454  }
455 }
456 ?>