|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * File for ErrorHandler utility class. |
| 5 | + * @package Phrity > Util > ErrorHandler |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Phrity\Util; |
| 9 | + |
| 10 | +use ErrorException; |
| 11 | +use Throwable; |
| 12 | + |
| 13 | +/** |
| 14 | + * ErrorHandler utility class. |
| 15 | + * Allows catching and resolving errors inline. |
| 16 | + */ |
| 17 | +class ErrorHandler |
| 18 | +{ |
| 19 | + /* ----------------- Public methods ---------------------------------------------- */ |
| 20 | + |
| 21 | + /** |
| 22 | + * Set error handler to run until removed. |
| 23 | + * @param mixed $handling |
| 24 | + * - If null, handler will throw ErrorException |
| 25 | + * - If Throwable $t, throw $t with ErrorException attached as previous |
| 26 | + * - If callable, will invoke callback with ErrorException as argument |
| 27 | + * @param int $levels Error levels to catch, all errors by default |
| 28 | + * @return mixed Previously registered error handler, if any |
| 29 | + */ |
| 30 | + public function set($handling = null, int $levels = E_ALL) |
| 31 | + { |
| 32 | + return set_error_handler($this->getHandler($handling), $levels); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Remove error handler. |
| 37 | + * @return bool True if removed |
| 38 | + */ |
| 39 | + public function restore(): bool |
| 40 | + { |
| 41 | + return restore_error_handler(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Run code with error handling, breaks on first encountered error. |
| 46 | + * @param callable $callback The code to run |
| 47 | + * @param mixed $handling |
| 48 | + * - If null, handler will throw ErrorException |
| 49 | + * - If Throwable $t, throw $t with ErrorException attached as previous |
| 50 | + * - If callable, will invoke callback with ErrorException as argument |
| 51 | + * @param int $levels Error levels to catch, all errors by default |
| 52 | + * @return mixed Return what $callback returns, or what $handling retuns on error |
| 53 | + */ |
| 54 | + public function with(callable $callback, $handling = null, int $levels = E_ALL) |
| 55 | + { |
| 56 | + $error = null; |
| 57 | + try { |
| 58 | + $this->set(null, $levels); |
| 59 | + $result = $callback(); |
| 60 | + } catch (ErrorException $e) { |
| 61 | + $error = $this->handle($handling, $e); |
| 62 | + } |
| 63 | + $this->restore(); |
| 64 | + return $error ?: $result; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Run code with error handling, comletes code before handling errors |
| 69 | + * @param callable $callback The code to run |
| 70 | + * @param mixed $handling |
| 71 | + * - If null, handler will throw ErrorException |
| 72 | + * - If Throwable $t, throw $t with ErrorException attached as previous |
| 73 | + * - If callable, will invoke callback with ErrorException as argument |
| 74 | + * @param int $levels Error levels to catch, all errors by default |
| 75 | + * @return mixed Return what $callback returns, or what $handling retuns on error |
| 76 | + */ |
| 77 | + public function withAll(callable $callback, $handling = null, int $levels = E_ALL) |
| 78 | + { |
| 79 | + $errors = []; |
| 80 | + $this->set(function (ErrorException $e) use (&$errors) { |
| 81 | + $errors[] = $e; |
| 82 | + }, $levels); |
| 83 | + $result = $callback(); |
| 84 | + $error = empty($errors) ? null : $this->handle($handling, $errors, $result); |
| 85 | + $this->restore(); |
| 86 | + return $error ?: $result; |
| 87 | + } |
| 88 | + |
| 89 | + /* ----------------- Private helpers --------------------------------------------- */ |
| 90 | + |
| 91 | + // Get handler function |
| 92 | + private function getHandler($handling) |
| 93 | + { |
| 94 | + return function ($severity, $message, $file, $line) use ($handling) { |
| 95 | + $error = new ErrorException($message, 0, $severity, $file, $line); |
| 96 | + $this->handle($handling, $error); |
| 97 | + }; |
| 98 | + } |
| 99 | + |
| 100 | + // Handle error according to $handlig type |
| 101 | + private function handle($handling, $error, $result = null) |
| 102 | + { |
| 103 | + if (is_callable($handling)) { |
| 104 | + return $handling($error, $result); |
| 105 | + } |
| 106 | + if (is_array($error)) { |
| 107 | + $error = array_shift($error); |
| 108 | + } |
| 109 | + if ($handling instanceof Throwable) { |
| 110 | + try { |
| 111 | + throw $error; |
| 112 | + } finally { |
| 113 | + throw $handling; |
| 114 | + } |
| 115 | + } |
| 116 | + throw $error; |
| 117 | + } |
| 118 | +} |
0 commit comments