/trunk/src/library/ZFDebug/Controller/Plugin/Debug/Plugin/Exception.php

https://github.com/emiliocad/medtechtrade · PHP · 153 lines · 87 code · 9 blank · 57 comment · 18 complexity · 52ff823d1990b2390fe2a96bc8c42d22 MD5 · raw file

  1. <?php
  2. /**
  3. * ZFDebug Zend Additions
  4. *
  5. * @category ZFDebug
  6. * @package ZFDebug_Controller
  7. * @subpackage Plugins
  8. * @copyright Copyright (c) 2008-2009 ZF Debug Bar Team (http://code.google.com/p/zfdebug)
  9. * @license http://code.google.com/p/zfdebug/wiki/License New BSD License
  10. * @version $Id: Exception.php 289 2009-12-10 10:47:02Z huuphuoc $
  11. */
  12. /**
  13. * @category ZFDebug
  14. * @package ZFDebug_Controller
  15. * @subpackage Plugins
  16. * @copyright Copyright (c) 2008-2009 ZF Debug Bar Team (http://code.google.com/p/zfdebug)
  17. * @license http://code.google.com/p/zfdebug/wiki/License New BSD License
  18. */
  19. class ZFDebug_Controller_Plugin_Debug_Plugin_Exception implements ZFDebug_Controller_Plugin_Debug_Plugin_Interface
  20. {
  21. /**
  22. * Contains plugin identifier name
  23. *
  24. * @var string
  25. */
  26. protected $_identifier = 'exception';
  27. /**
  28. * Contains any errors
  29. *
  30. * @var param array
  31. */
  32. static $errors = array();
  33. /**
  34. * Gets identifier for this plugin
  35. *
  36. * @return string
  37. */
  38. public function getIdentifier ()
  39. {
  40. return $this->_identifier;
  41. }
  42. /**
  43. * Creates Error Plugin ans sets the Error Handler
  44. *
  45. * @return void
  46. */
  47. public function __construct ()
  48. {
  49. set_error_handler(array($this , 'errorHandler'));
  50. }
  51. /**
  52. * Gets menu tab for the Debugbar
  53. *
  54. * @return string
  55. */
  56. public function getTab ()
  57. {
  58. $response = Zend_Controller_Front::getInstance()->getResponse();
  59. $errorCount = count(self::$errors);
  60. if (! $response->isException() && ! $errorCount)
  61. return '';
  62. $error = '';
  63. $exception = '';
  64. if ($errorCount)
  65. $error = ($errorCount == 1 ? '1 Error' : $errorCount . ' Errors');
  66. $count = count($response->getException());
  67. //if ($this->_options['show_exceptions'] && $count)
  68. if ($count)
  69. $exception = ($count == 1) ? '1 Exception' : $count . ' Exceptions';
  70. $text = $exception . ($exception == '' || $error == '' ? '' : ' - ') . $error;
  71. return $text;
  72. }
  73. /**
  74. * Gets content panel for the Debugbar
  75. *
  76. * @return string
  77. */
  78. public function getPanel ()
  79. {
  80. $response = Zend_Controller_Front::getInstance()->getResponse();
  81. $errorCount = count(self::$errors);
  82. if (! $response->isException() && ! $errorCount)
  83. return '';
  84. $html = '';
  85. foreach ($response->getException() as $e) {
  86. $html .= '<h4>' . get_class($e) . ': ' . $e->getMessage() . '</h4><p>thrown in ' . $e->getFile() . ' on line ' . $e->getLine() . '</p>';
  87. $html .= '<h4>Call Stack</h4><ol>';
  88. foreach ($e->getTrace() as $t) {
  89. $func = $t['function'] . '()';
  90. if (isset($t['class']))
  91. $func = $t['class'] . $t['type'] . $func;
  92. if (! isset($t['file']))
  93. $t['file'] = 'unknown';
  94. if (! isset($t['line']))
  95. $t['line'] = 'n/a';
  96. $html .= '<li>' . $func . '<br>in ' . str_replace($_SERVER['DOCUMENT_ROOT'], '', $t['file']) . ' on line ' . $t['line'] . '</li>';
  97. }
  98. $html .= '</ol>';
  99. }
  100. if ($errorCount) {
  101. $html .= '<h4>Errors</h4><ol>';
  102. foreach (self::$errors as $error) {
  103. $html .= '<li>' . sprintf("%s: %s in %s on line %d", $error['type'], $error['message'], str_replace($_SERVER['DOCUMENT_ROOT'], '', $error['file']), $error['line']) . '</li>';
  104. }
  105. $html .= '</ol>';
  106. }
  107. return $html;
  108. }
  109. /**
  110. * Debug Bar php error handler
  111. *
  112. * @param string $level
  113. * @param string $message
  114. * @param string $file
  115. * @param string $line
  116. * @return bool
  117. */
  118. public static function errorHandler ($level, $message, $file, $line)
  119. {
  120. if (! ($level & error_reporting()))
  121. return false;
  122. switch ($level) {
  123. case E_NOTICE:
  124. case E_USER_NOTICE:
  125. $type = 'Notice';
  126. break;
  127. case E_WARNING:
  128. case E_USER_WARNING:
  129. $type = 'Warning';
  130. break;
  131. case E_ERROR:
  132. case E_USER_ERROR:
  133. $type = 'Fatal Error';
  134. break;
  135. default:
  136. $type = 'Unknown, ' . $level;
  137. break;
  138. }
  139. self::$errors[] = array('type' => $type , 'message' => $message , 'file' => $file , 'line' => $line);
  140. if (ini_get('log_errors'))
  141. error_log(sprintf("%s: %s in %s on line %d", $type, $message, $file, $line));
  142. return true;
  143. }
  144. }