examiner.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php namespace Laravel\Exception;
  2. use Laravel\File;
  3. class Examiner {
  4. /**
  5. * The exception being examined.
  6. *
  7. * @var Exception
  8. */
  9. public $exception;
  10. /**
  11. * The file manager instance.
  12. *
  13. * @var File
  14. */
  15. private $file;
  16. /**
  17. * Human-readable error levels and descriptions.
  18. *
  19. * @var array
  20. */
  21. private $levels = array(
  22. 0 => 'Error',
  23. E_ERROR => 'Error',
  24. E_WARNING => 'Warning',
  25. E_PARSE => 'Parsing Error',
  26. E_NOTICE => 'Notice',
  27. E_CORE_ERROR => 'Core Error',
  28. E_CORE_WARNING => 'Core Warning',
  29. E_COMPILE_ERROR => 'Compile Error',
  30. E_COMPILE_WARNING => 'Compile Warning',
  31. E_USER_ERROR => 'User Error',
  32. E_USER_WARNING => 'User Warning',
  33. E_USER_NOTICE => 'User Notice',
  34. E_STRICT => 'Runtime Notice'
  35. );
  36. /**
  37. * Create a new exception examiner instance.
  38. *
  39. * @param Exception $exception
  40. * @param File $file
  41. * @return void
  42. */
  43. public function __construct($exception, File $file)
  44. {
  45. $this->exception = $exception;
  46. $this->file = $file;
  47. }
  48. /**
  49. * Get a human-readable version of the exception error code.
  50. *
  51. * @return string
  52. */
  53. public function severity()
  54. {
  55. if (array_key_exists($this->exception->getCode(), $this->levels))
  56. {
  57. return $this->levels[$this->exception->getCode()];
  58. }
  59. return $this->exception->getCode();
  60. }
  61. /**
  62. * Get the exception error message formatted for use by Laravel.
  63. *
  64. * The exception file paths will be shortened, and the file name and line number
  65. * will be added to the exception message.
  66. *
  67. * @return string
  68. */
  69. public function message()
  70. {
  71. $file = str_replace(array(APP_PATH, SYS_PATH), array('APP_PATH/', 'SYS_PATH/'), $this->exception->getFile());
  72. return rtrim($this->exception->getMessage(), '.').' in '.$file.' on line '.$this->exception->getLine().'.';
  73. }
  74. /**
  75. * Get the code surrounding the line where the exception occurred.
  76. *
  77. * @return array
  78. */
  79. public function context()
  80. {
  81. return $this->file->snapshot($this->exception->getFile(), $this->exception->getLine());
  82. }
  83. /**
  84. * Magic Method to pass function calls to the exception.
  85. */
  86. public function __call($method, $parameters)
  87. {
  88. return call_user_func_array(array($this->exception, $method), $parameters);
  89. }
  90. }