Skip to content

Commit

Permalink
Jet Debug
Browse files Browse the repository at this point in the history
------------
* Brand new advanced error displayer
  • Loading branch information
mirekmarek committed May 30, 2024
1 parent 46d4cde commit f285d25
Show file tree
Hide file tree
Showing 7 changed files with 574 additions and 124 deletions.
245 changes: 121 additions & 124 deletions application/ErrorHandlers/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,17 @@
use Jet\Debug;
use Jet\Debug_ErrorHandler_Handler;
use Jet\Debug_ErrorHandler_Error;
use PhpToken;

/**
*
*/
class ErrorHandler_Display extends Debug_ErrorHandler_Handler
{
protected array $non_fatal_errors = [];

protected bool $non_fatal_displayer_registered = false;

/**
* @return string
*/
Expand Down Expand Up @@ -51,143 +56,135 @@ public function errorDisplayed(): bool

/**
*
* @param Debug_ErrorHandler_Error $e
* @param Debug_ErrorHandler_Error $error
*
*/
public function display( Debug_ErrorHandler_Error $e ): void
public function display( Debug_ErrorHandler_Error $error ): void
{
if($e->isFatal()) {
if($error->isFatal()) {
while (ob_get_level())
{
ob_end_clean();
}

$handler = $this;

require __DIR__.'/views/fatal-error.phtml';
return;
}

?>
<style>
.dbg-error {
background-color: #c9ffc9;
padding:5px;
border: 1px solid black;
font-family: 'Arial CE', Arial, sans-serif;
}

.dbg-error h2 {
padding:0;
margin:5px;
}

.dbg-error div.error {
padding: 10px;
margin-top: 10px;
margin-bottom: 10px;
border-top: 1px solid black;
border-bottom: 1px solid black;
}

.dbg-error table.error-info {
border-collapse:collapse;
background-color: #c9c9c9;
}

.dbg-error table.error-info td {
padding: 5px;
}

.dbg-error table.backtrace {
border-collapse:collapse;
background-color: #999999;
}

.dbg-error table.backtrace th {
text-align: left;
padding: 5px;
}

.dbg-error table.backtrace .row1 td,
.dbg-error table.backtrace .row2 td {
padding: 5px;
}

.dbg-error table.backtrace .row1 {
background-color:#f0f0f0;
}

.dbg-error table.backtrace .row2 {
background-color:#c9c9c9;
}
</style>
<br/>
<div class="dbg-error">
<h2><?= static::encode( $e->getTxt() ) ?></h2>
<div class="error">
<?= static::encode( $e->getMessage() ) ?>
</div>
<table class="error-info">
<tr>
<td>script:</td>
<td><?= $e->getFile() ?></td>
</tr>
<tr>
<td>line:</td>
<td><?= $e->getLine() ?></td>
</tr>
<tr>
<td>time:</td>
<td><?= $e->getDate() ?> <?= $e->getTime() ?></td>
</tr>
<tr>
<td>URL:</td>
<td><?= static::encode( $e->getRequestURL() ) ?></td>
</tr>
</table>
<br/>

<?php if( $e->getBacktrace() ): ?>

<br/><strong>Debug backtrace:</strong><br/>

<table class="backtrace">
<thead>
<tr>
<th>File</th>
<th>Line</th>
<th>Call</th>
</tr>

</thead>

<tbody>
<?php
$i = 0;
foreach( $e->getBacktrace() as $d ):
$i++;
?>
<tr class="row<?=($i % 2)?1:2?>">
<td><?= $d->getFile() ?></td>
<td><?= $d->getLine() ?></td>
<td><?= self::encode( $d->getCall() ) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>

</div><br/>

<?php
$this->non_fatal_errors[] = $error;

if( !$this->non_fatal_displayer_registered ) {
register_shutdown_function( function() {
$errors = $this->non_fatal_errors;
$handler = $this;

require __DIR__.'/views/warnings.phtml';
} );

$this->non_fatal_displayer_registered = true;
}
}



/**
*
* @param string $html
*
* @return string
* @param string $source
* @param bool $as_lines
* @return string|array
*/
protected static function encode( string $html ): string
public function highlightPhpCode( string $source, bool $as_lines = false): string|array
{
return nl2br( htmlspecialchars( $html, ENT_QUOTES ) );
$source = str_replace("\r\n", "\n", $source);
$source = preg_replace('#(__halt_compiler\s*\(\)\s*;).*#is', '$1', $source);
$source = preg_replace('#/\*sensitive\{\*/.*?/\*}\*/#s', '********', $source);

$result = '';
$prev_token_class = null;

$replace_map = [
'<' => '&lt;',
'>' => '&gt;',
'&' => '&amp;',
"\t" => '&nbsp;&nbsp;&nbsp;&nbsp;'
];

$class_prefix = 'dbg-code-hl-';

$class_map = [
T_COMMENT => $class_prefix.'comment',
T_DOC_COMMENT => $class_prefix.'doc-comment',
T_INLINE_HTML => $class_prefix.'html',

T_OPEN_TAG => $class_prefix.'general',
T_OPEN_TAG_WITH_ECHO => $class_prefix.'general',
T_CLOSE_TAG => $class_prefix.'general',
T_LINE => $class_prefix.'general',
T_FILE => $class_prefix.'general',
T_DIR => $class_prefix.'general',
T_TRAIT_C => $class_prefix.'general',
T_METHOD_C => $class_prefix.'general',
T_FUNC_C => $class_prefix.'general',
T_NS_C => $class_prefix.'general',
T_CLASS_C => $class_prefix.'general',
T_STRING => $class_prefix.'general',
T_NAME_FULLY_QUALIFIED => $class_prefix.'general',
T_NAME_QUALIFIED => $class_prefix.'general',
T_NAME_RELATIVE => $class_prefix.'general',
T_LNUMBER => $class_prefix.'general',
T_DNUMBER => $class_prefix.'general',

T_ENCAPSED_AND_WHITESPACE => $class_prefix.'string',
T_CONSTANT_ENCAPSED_STRING => $class_prefix.'string',

T_VARIABLE => $class_prefix.'variable',

'default' => $class_prefix.'keyword',
];

foreach ( PhpToken::tokenize($source) as $token) {
if($token->id!=T_WHITESPACE) {
$current_token_class = $class_map[$token->id] ?? $class_map['default'];

if ($prev_token_class !== $current_token_class) {
if($prev_token_class !== null) {
$result .= '</span>';
}

$result .= '<span class="'.$current_token_class.'">';

$prev_token_class = $current_token_class;
}
}

$token_text = $token->text;
$token_text = strtr($token_text, $replace_map);

if(isset($current_token_class)) {
$token_text = str_replace("\n", "</span>\n<span class=\"$current_token_class\">", $token_text);
}


$result .= $token_text;
}

if($prev_token_class) {
$result .= '</span>';
}

if($as_lines) {
$_result = explode("\n", $result);
$result = [];

foreach($_result as $line_no=>$line) {
$line_no++;
$result[$line_no] = rtrim($line);
}

return $result;
}

return nl2br($result);
}


}
Loading

0 comments on commit f285d25

Please sign in to comment.