PHP Classes

File: src/XrThrowableParser.php

Recommend this page to a friend!
  Classes of Rodolfo Berrios Arce  >  XR PHP Debugger Online  >  src/XrThrowableParser.php  >  Download  
File: src/XrThrowableParser.php
Role: Class source
Content type: text/plain
Description: Class source
Class: XR PHP Debugger Online
Debug PHP code using a Web interface
Author: By
Last change:
Date: 3 months ago
Size: 3,364 bytes
 

Contents

Class file image Download
<?php

/*
 * This file is part of Chevere.
 *
 * (c) Rodolfo Berrios <rodolfo@chevere.org>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

namespace
Chevere\Xr;

use
Chevere\ThrowableHandler\Formats\ThrowableHandlerHtmlFormat;
use
Chevere\ThrowableHandler\Interfaces\ThrowableHandlerFormatInterface;
use
Chevere\ThrowableHandler\Interfaces\ThrowableReadInterface;
use
Chevere\ThrowableHandler\ThrowableRead;
use
Chevere\Trace\TraceDocument;
use
Throwable;

class
XrThrowableParser
{
    private
string $topic = '';

    private
string $body = '';

    private
string $emote = '??Throwable';

    private
ThrowableHandlerFormatInterface $format;

    private
int $index = 0;

    public const
OPEN_TEMPLATE = '<div class="throwable">';

    public const
CLOSE_TEMPLATE = '</div>';

    public const
ITEM_TEMPLATE = <<<HTML
<div class="throwable-item">
            <h2 class="throwable-title">%title%</h2>
            <div class="throwable-code">%code%</div>
            <div class="throwable-message">%message%</div>
            %extra%
            <div class="throwable-backtrace backtrace">%trace%</div>
        </div>
HTML;

    public function
__construct(
        private
Throwable $throwable,
        private
string $extra = '',
    ) {
       
$this->throwableRead = new ThrowableRead($throwable);
       
$this->format = new ThrowableHandlerHtmlFormat();
       
$this->topic = basename(
           
str_replace(
               
'\\',
               
DIRECTORY_SEPARATOR,
               
$this->throwableRead->className()
            )
        );
       
$this->appendBodyLine(static::OPEN_TEMPLATE);
        do {
           
$this->index++;
        } while (
$throwable = $this->parse($throwable));
       
$this->appendBodyLine(static::CLOSE_TEMPLATE);
    }

    public function
body(): string
   
{
        return
$this->body;
    }

    public function
topic(): string
   
{
        return
$this->topic;
    }

    public function
emote(): string
   
{
        return
$this->emote;
    }

    public function
throwableRead(): ThrowableReadInterface
   
{
        return
$this->throwableRead;
    }

    private function
appendBodyLine(string $body): void
   
{
       
$this->body .= $body . "\n";
    }

    private function
parse(Throwable $throwable): ?Throwable
   
{
        if (
$this->index === 1) {
           
$read = $this->throwableRead;
           
$trace = $this->throwableRead->trace();
        } else {
           
$read = new ThrowableRead($throwable);
           
$trace = [
                [
                   
'function' => '{main}',
                   
'file' => $read->file(),
                   
'line' => $read->line(),
                ]
            ];
        }
       
$traceDocument = new TraceDocument($trace, $this->format);
       
$translate = [
               
'%title%' => $read->className(),
               
'%code%' => $read->code(),
               
'%message%' => $read->message(),
               
'%extra%' => $this->index === 1
                   
? $this->extra
                   
: '',
               
'%trace%' => $traceDocument->__toString(),
            ];
       
$this->appendBodyLine(strtr(static::ITEM_TEMPLATE, $translate));

        return
$throwable->getPrevious();
    }
}

For more information send a message to info at phpclasses dot org.