|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Created by PhpStorm. |
| 4 | + * User: dawid |
| 5 | + * Date: 07.07.18 |
| 6 | + * Time: 17:58 |
| 7 | + */ |
| 8 | + |
| 9 | +namespace Engine\Error; |
| 10 | + |
| 11 | + |
| 12 | +use Throwable; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class ShiftError |
| 16 | + * @package Engine\Error |
| 17 | + */ |
| 18 | +class ShiftError extends \Error { |
| 19 | + |
| 20 | + /** |
| 21 | + * ShiftError constructor. |
| 22 | + * @param string $message |
| 23 | + * @param int $code |
| 24 | + * @param Throwable|null $previous |
| 25 | + */ |
| 26 | + public function __construct(string $message = "", int $code = 0, Throwable $previous = null) { |
| 27 | + parent::__construct($message, $code, $previous); |
| 28 | + |
| 29 | + $line = (int)$this->getLine(); |
| 30 | + $highlighted = $this->highlight_file_with_line_numbers($this->getFile(), $line); |
| 31 | + $stackTrace = $this->getBeautyStackTrace(); |
| 32 | + |
| 33 | + echo ' |
| 34 | + <style> |
| 35 | + *{margin: 0;padding: 0;} |
| 36 | + #error-container{width: 100%;float: left;background: #135171;box-sizing: border-box;padding: 5px;color:#fff;font-family: Arial, sans-serif} |
| 37 | + #error-container #message{width: 100%;float: left;} |
| 38 | + #error-container .file-presentation, |
| 39 | + #error-container #stack-trace .trace-item .file-presentation{white-space: pre-wrap;background: white;position: relative} |
| 40 | + #error-container #stack-trace .trace-item .file-presentation{display: none;} |
| 41 | + #error-container .file-presentation #yellow-line, |
| 42 | + #error-container #stack-trace .trace-item .file-presentation #yellow-line{width: 100%;z-index: 1;position: absolute;height: 15px;left: 0;background: rgba(255,255,0,0.36)} |
| 43 | + #error-container .file-presentation code, |
| 44 | + #error-container #stack-trace .trace-item .file-presentation code{z-index: 2;} |
| 45 | + #error-container #stack-trace .trace-item{border-bottom:1px #ffffff solid;padding: 5px 2px} |
| 46 | + #error-container #stack-trace .trace-item:last-child{border:none;} |
| 47 | + #error-container #stack-trace .trace-item .filename{font-weight: bold} |
| 48 | + #error-container #stack-trace .trace-item .filepath{font-size: 12px;line-height: 15px;} |
| 49 | + #error-container #stack-trace .trace-item .filepath .show-hide{cursor: pointer;font-size: 15px;padding: 0 5px;font-weight: bold;} |
| 50 | + #error-container #stack-trace .trace-item .method{font-size: 14px;font-style: italic} |
| 51 | + #error-container #stack-trace .trace-item .method .array{border-bottom: 1px #fff dotted; cursor: pointer;position: relative} |
| 52 | + #error-container #stack-trace .trace-item .method .array .array-content{display: none;position: absolute;bottom: 15px;left: -70px;background: #f3f3f3;color: #000;min-width: 200px;padding: 4px;white-space: pre;border: 1px #a2a2a2 dotted;cursor: text;font-style: normal} |
| 53 | + #error-container #stack-trace .trace-item .method .array:hover .array-content{display: inline} |
| 54 | + </style> |
| 55 | + <div id="error-container"> |
| 56 | + <div id="message">' . $this->getMessage() . '</div> |
| 57 | + <div id="file-info">in <b>' . $this->getFile() . '</b></div> |
| 58 | + <div class="file-presentation">' . $highlighted . '</div> |
| 59 | + <br><br> |
| 60 | + <div id="stack-trace-text">Stack trace:</div> |
| 61 | + <div id="stack-trace">' . $stackTrace . '</div> |
| 62 | + </div> |
| 63 | + <script> |
| 64 | + const buttons = document.getElementsByClassName("show-hide"); |
| 65 | + |
| 66 | + for(const button of buttons) { |
| 67 | + button.addEventListener("click", function(e) { |
| 68 | + const filePresentation = this.parentElement.parentElement.getElementsByClassName("file-presentation"); |
| 69 | +
|
| 70 | + if(!filePresentation.length) { |
| 71 | + return; |
| 72 | + } |
| 73 | +
|
| 74 | + if (filePresentation[0].style.display !== "block") { |
| 75 | + filePresentation[0].style.display = "block"; |
| 76 | + this.innerText = "-"; |
| 77 | + } else { |
| 78 | + this.innerText = "+"; |
| 79 | + filePresentation[0].style.display = "none"; |
| 80 | + } |
| 81 | + }); |
| 82 | + } |
| 83 | + </script>'; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @param string $file |
| 88 | + * @param int $lineWithError |
| 89 | + * @return string |
| 90 | + */ |
| 91 | + private function highlight_file_with_line_numbers(string $file, int $lineWithError = null): string { |
| 92 | + $code = substr(highlight_file($file, true), 36, -15); |
| 93 | + $lines = explode('<br />', $code); |
| 94 | + $lineCount = count($lines); |
| 95 | + $padLength = strlen($lineCount); |
| 96 | + |
| 97 | + $return = ''; |
| 98 | + if ($lineWithError) |
| 99 | + $return .= '<div id="yellow-line" style="top: 75px"></div>'; |
| 100 | + |
| 101 | + $return .= "<code><span style=\"color: #000000\">"; |
| 102 | + |
| 103 | + $start = null; |
| 104 | + $limit = 10; |
| 105 | + if ($lineWithError) { |
| 106 | + $start = $lineWithError - 5; |
| 107 | + } |
| 108 | + |
| 109 | + foreach ($lines as $i => $line) { |
| 110 | + if ($start) { |
| 111 | + if ($start > $i + 1) { |
| 112 | + continue; |
| 113 | + } |
| 114 | + if ($start + $limit < $i + 1) { |
| 115 | + continue; |
| 116 | + } |
| 117 | + } |
| 118 | + $lineNumber = str_pad($i + 1, $padLength, " ", STR_PAD_LEFT); |
| 119 | + $return .= '<span style="color: #999999">' . $lineNumber . ' | </span>' . $line . "\n"; |
| 120 | + } |
| 121 | + |
| 122 | + $return .= "</span></code>"; |
| 123 | + |
| 124 | + |
| 125 | + return $return; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * @return string |
| 130 | + */ |
| 131 | + private function getBeautyStackTrace(): string { |
| 132 | + $return = ''; |
| 133 | + foreach ($this->getTrace() as $info) { |
| 134 | + $infoFile = isset($info['file']) ? $info['file'] : ''; |
| 135 | + $pathAsArray = explode('/', $infoFile); |
| 136 | + $filename = $pathAsArray[count($pathAsArray) - 1]; |
| 137 | + |
| 138 | + $fargs = ''; |
| 139 | + |
| 140 | + foreach ($info['args'] as $arg) { |
| 141 | + $type = gettype($arg); |
| 142 | + |
| 143 | + switch ($type) { |
| 144 | + case 'string': |
| 145 | + if (strlen($arg) === 0) { |
| 146 | + $fargs .= "''"; |
| 147 | + } else { |
| 148 | + $fargs .= "'" . $arg . "'"; |
| 149 | + } |
| 150 | + break; |
| 151 | + case 'array': |
| 152 | + $fargs .= '<span class="array">Array<span class="array-content">' . print_r($arg, true) . '</span></span>'; |
| 153 | + break; |
| 154 | + case 'object': |
| 155 | + $fargs .= '<span class="array">' . get_class($arg) . ' Object<span class="array-content">' . print_r($arg, true) . '</span></span>'; |
| 156 | + break; |
| 157 | + default: |
| 158 | + $fargs .= $arg; |
| 159 | + } |
| 160 | + $fargs .= ', '; |
| 161 | + } |
| 162 | + $fargs = trim($fargs, ', '); |
| 163 | + |
| 164 | + $infoLine = isset($info['line']) ? $info['line'] : ''; |
| 165 | + $filePresentation = strlen($infoFile) && stream_is_local($infoFile) ? $this->highlight_file_with_line_numbers($infoFile, (int)$infoLine) : ''; |
| 166 | + $return .= ' |
| 167 | + <div class="trace-item"> |
| 168 | + <div class="filename"> |
| 169 | + ' . $filename . ':' . $infoLine . ' |
| 170 | + </div> |
| 171 | + <div class="filepath"> |
| 172 | + ' . $info["file"] . ' <span class="show-hide">+</span> |
| 173 | + </div> |
| 174 | + <div class="method"> |
| 175 | + ' . $info["class"] . $info["type"] . $info["function"] . '(' . $fargs . ') |
| 176 | + </div > |
| 177 | + <div class="file-presentation">' . $filePresentation . '</div > |
| 178 | + </div > |
| 179 | + '; |
| 180 | + } |
| 181 | + return $return; |
| 182 | + } |
| 183 | +} |
0 commit comments