wrapper.php 2.4 KB

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