PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/api/exceptionhandler/default.php

http://github.com/symfony-cmf/phpcrbrowser
PHP | 84 lines | 55 code | 8 blank | 21 comment | 9 complexity | c6d86c7e637e0f823a6468849406a9a5 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. <?php
  2. /* Licensed under the Apache License, Version 2.0
  3. * See the LICENSE and NOTICE file for further information
  4. */
  5. /**
  6. * Default exception handler. Includes a very detailled backtrace in the
  7. * collected data including source file information.
  8. */
  9. class api_exceptionhandler_default extends api_exceptionhandler_base {
  10. /** Number of code lines included before and after the relevant code line
  11. * in the back trace.
  12. */
  13. const BACKTRACE_CONTEXT = 8;
  14. /**
  15. * Calls getTrace and does api_exceptionhandler_base::dispatch()
  16. * with the collected data.
  17. * @param $e Exception: The exception to handle.
  18. */
  19. public function handle(Exception $e) {
  20. $this->data['exception'] = $this->getTrace($e);;
  21. $this->dispatch($this->data);
  22. return true;
  23. }
  24. /**
  25. * Process the exception. Calls the Exception::getTrace() method to
  26. * get the backtrace. Gets the relevant lines of code for each step
  27. * in the backtrace.
  28. */
  29. public function getTrace($e) {
  30. $trace = $e->getTrace();
  31. foreach($trace as $i => &$entry) {
  32. if (isset($entry['class'])) {
  33. try {
  34. $refl = new ReflectionMethod($entry['class'], $entry['function']);
  35. if (isset($trace[$i -1])) {
  36. $entry['caller'] = (int) $trace[$i -1]['line'] -1;
  37. } else if ($i === 0) {
  38. $entry['caller'] = (int) $e->getLine() -1;
  39. }
  40. $start = $entry['caller'] - self::BACKTRACE_CONTEXT;
  41. if ($start < $refl->getStartLine()) { $start = $refl->getStartLine() - 1; }
  42. $end = $entry['caller'] + self::BACKTRACE_CONTEXT;
  43. if ($end > $refl->getEndLine()) { $end = $refl->getEndLine(); }
  44. $entry['source'] = $this->getSourceFromFile($refl->getFileName(), $start, $end);
  45. } catch (Exception $e) {
  46. $entry['caller'] = null;
  47. $entry['source'] = '';
  48. }
  49. }
  50. if (isset($entry['args'])) {
  51. // Duplicate so we don't overwrite by-reference variables
  52. $args = array();
  53. foreach($entry['args'] as $i => $arg) {
  54. $args[$i] = gettype($arg);
  55. }
  56. $entry['args'] = $args;
  57. }
  58. }
  59. $exceptionParams = array();
  60. if (method_exists($e, 'getParams')) {
  61. $exceptionParams = $e->getParams();
  62. }
  63. $d = array('backtrace' => $trace,
  64. 'message' => $e->getMessage(),
  65. 'code' => $e->getCode(),
  66. 'file' => $e->getFile(),
  67. 'line' => $e->getLine(),
  68. 'name' => api_helpers_class::getBaseName($e),
  69. 'params' => $exceptionParams,
  70. );
  71. if(!empty($e->userInfo)) {
  72. $d['userInfo'] = $e->userInfo;
  73. }
  74. return $d;
  75. }
  76. }