| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Core; |
| 6: | |
| 7: | use Atk4\Core\ExceptionRenderer\RendererAbstract; |
| 8: | use Atk4\Core\Translator\ITranslatorAdapter; |
| 9: | use PHPUnit\Framework\SelfDescribing; |
| 10: | |
| 11: | if (!interface_exists(SelfDescribing::class)) { |
| 12: | eval('namespace PHPUnit\Framework; interface SelfDescribing { public function toString(): string; }'); |
| 13: | } |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | class Exception extends \Exception implements SelfDescribing |
| 19: | { |
| 20: | use WarnDynamicPropertyTrait; |
| 21: | |
| 22: | |
| 23: | protected $customExceptionTitle = 'Critical Error'; |
| 24: | |
| 25: | |
| 26: | private $params = []; |
| 27: | |
| 28: | |
| 29: | private $solutions = []; |
| 30: | |
| 31: | |
| 32: | private $translator; |
| 33: | |
| 34: | public function __construct(string $message = '', int $code = 0, \Throwable $previous = null) |
| 35: | { |
| 36: | parent::__construct($message, $code, $previous); |
| 37: | |
| 38: | |
| 39: | $trace = debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT); |
| 40: | for ($i = 0; $i < count($trace); ++$i) { |
| 41: | $frame = $trace[$i]; |
| 42: | if (isset($frame['object']) && $frame['object'] === $this && $frame['function'] === '__construct') { |
| 43: | array_shift($trace); |
| 44: | } |
| 45: | } |
| 46: | $traceReflectionProperty = new \ReflectionProperty(parent::class, 'trace'); |
| 47: | $traceReflectionProperty->setAccessible(true); |
| 48: | $traceReflectionProperty->setValue($this, $trace); |
| 49: | } |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function setMessage(string $message): self |
| 58: | { |
| 59: | $this->message = $message; |
| 60: | |
| 61: | return $this; |
| 62: | } |
| 63: | |
| 64: | #[\Override] |
| 65: | public function toString(): string |
| 66: | { |
| 67: | $res = static::class . ': ' . $this->getMessage() . "\n"; |
| 68: | foreach ($this->getParams() as $param => $value) { |
| 69: | $valueStr = RendererAbstract::toSafeString($value, true); |
| 70: | $res .= ' ' . $param . ': ' . str_replace("\n", "\n" . ' ', $valueStr) . "\n"; |
| 71: | } |
| 72: | |
| 73: | return $res; |
| 74: | } |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | public function getColorfulText(): string |
| 88: | { |
| 89: | return (string) new ExceptionRenderer\Console($this, $this->translator); |
| 90: | } |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | public function getHtml(): string |
| 103: | { |
| 104: | return (string) new ExceptionRenderer\Html($this, $this->translator); |
| 105: | } |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | public function getJson(): string |
| 111: | { |
| 112: | return (string) new ExceptionRenderer\Json($this, $this->translator); |
| 113: | } |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | public function getParams(): array |
| 121: | { |
| 122: | return $this->params; |
| 123: | } |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | public function addMoreInfo(string $param, $value): self |
| 131: | { |
| 132: | $this->params[$param] = $value; |
| 133: | |
| 134: | return $this; |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | public function addSolution(string $solution): self |
| 141: | { |
| 142: | $this->solutions[] = $solution; |
| 143: | |
| 144: | return $this; |
| 145: | } |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | public function getSolutions(): array |
| 151: | { |
| 152: | return $this->solutions; |
| 153: | } |
| 154: | |
| 155: | public function getCustomExceptionTitle(): string |
| 156: | { |
| 157: | return $this->customExceptionTitle; |
| 158: | } |
| 159: | |
| 160: | |
| 161: | |
| 162: | |
| 163: | |
| 164: | |
| 165: | public function setTranslatorAdapter(ITranslatorAdapter $translator = null): self |
| 166: | { |
| 167: | $this->translator = $translator; |
| 168: | |
| 169: | return $this; |
| 170: | } |
| 171: | } |
| 172: | |