Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Jan 22, 2020
1 parent 6b9352a commit f6d4ee2
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 82 deletions.
Expand Up @@ -13,11 +13,11 @@

namespace Chevere\Components\ExceptionHandler\Documents;

use DateTimeInterface;
use Chevere\Components\ExceptionHandler\Interfaces\DocumentInterface;
use Chevere\Components\ExceptionHandler\Interfaces\ExceptionHandlerInterface;
use Chevere\Components\ExceptionHandler\Interfaces\FormatterInterface;
use Chevere\Components\ExceptionHandler\Trace;
use DateTimeInterface;

abstract class AbstractDocument implements DocumentInterface
{
Expand All @@ -38,9 +38,6 @@ final public function __construct(ExceptionHandlerInterface $exceptionHandler)
$this->template = $this->getTemplate();
}

/**
* @return string[]
*/
final public function sections(): array
{
return $this->sections;
Expand All @@ -51,11 +48,6 @@ protected function prepare(string $value): string
return $value;
}

protected function getLineBreak(): string
{
return "\n\n";
}

abstract public function getTemplate(): array;

abstract public function getFormatter(): FormatterInterface;
Expand All @@ -72,7 +64,7 @@ final public function toString(): string
static::TAG_ID => $this->exceptionHandler->id(),
static::TAG_DATE_TIME_UTC_ATOM => $dateTimeUtc->format(DateTimeInterface::ATOM),
static::TAG_TIMESTAMP => $dateTimeUtc->getTimestamp(),
static::TAG_LOG_FILENAME => $this->getLogFilename(),
static::TAG_LOG_DESTINATION => $this->exceptionHandler->logDestination(),
static::TAG_STACK => $this->getStack(),
static::TAG_PHP_UNAME => php_uname(),
];
Expand All @@ -84,7 +76,7 @@ final public function toString(): string

return $this->prepare(
strtr(
implode($this->getLineBreak(), array_filter($templated)),
implode($this->formatter->getLineBreak(), array_filter($templated)),
$this->tags
)
);
Expand All @@ -98,14 +90,6 @@ private function getExceptionCode(): string
: '';
}

private function getLogFilename(): string
{
return
$this->exceptionHandler->hasLogger()
? '__LOGGER_FILENAME__'
: '/dev/null';
}

private function getStack(): string
{
return
Expand Down
Expand Up @@ -39,7 +39,7 @@ public function getTemplate(): array
return [
static::SECTION_TITLE => $title,
static::SECTION_MESSAGE => $this->colorApplySection('# Message ' . static::TAG_CODE_WRAP) . "\n" . static::TAG_MESSAGE,
static::SECTION_ID => $this->colorApplySection('# Incident ID:' . static::TAG_ID) . "\n" . 'Logged at ' . $this->colorApplyLink(static::TAG_LOG_FILENAME),
static::SECTION_ID => $this->colorApplySection('# Incident ID:' . static::TAG_ID) . "\n" . 'Logged at ' . $this->colorApplyLink(static::TAG_LOG_DESTINATION),
static::SECTION_TIME => $this->colorApplySection('# Time') . "\n" . static::TAG_DATE_TIME_UTC_ATOM . ' [' . static::TAG_TIMESTAMP . ']',
static::SECTION_STACK => $this->colorApplySection('# Stack trace') . "\n" . static::TAG_STACK,
static::SECTION_CLIENT => $this->colorApplySection('# Client') . "\n" . static::TAG_CLIENT_IP . ' ' . static::TAG_CLIENT_USER_AGENT,
Expand Down
Expand Up @@ -48,7 +48,7 @@ public function getTemplate(): array
static::SECTION_TITLE => $this->wrapTitle(static::TAG_TITLE . ' <span>in&nbsp;' . static::TAG_FILE_LINE . '</span>'),
static::SECTION_MESSAGE => $this->wrapSectionTitle('# Message ' . static::TAG_CODE_WRAP) . "\n" . $this->wrapContent(static::TAG_MESSAGE),
static::SECTION_TIME => $this->wrapSectionTitle('# Time') . "\n" . $this->wrapContent(static::TAG_DATE_TIME_UTC_ATOM . ' [' . static::TAG_TIMESTAMP . ']'),
static::SECTION_ID => $this->wrapSectionTitle('# Incident ID:' . static::TAG_ID) . "\n" . $this->wrapContent('Logged at ' . static::TAG_LOG_FILENAME),
static::SECTION_ID => $this->wrapSectionTitle('# Incident ID:' . static::TAG_ID) . "\n" . $this->wrapContent('Logged at ' . static::TAG_LOG_DESTINATION),
static::SECTION_STACK => $this->wrapSectionTitle('# Stack trace') . "\n" . $this->wrapContent(static::TAG_STACK),
static::SECTION_CLIENT => $this->wrapSectionTitle('# Client') . "\n" . $this->wrapContent(static::TAG_CLIENT_IP . ' ' . static::TAG_CLIENT_USER_AGENT),
static::SECTION_REQUEST => $this->wrapSectionTitle('# Request') . "\n" . $this->wrapContent(static::TAG_SERVER_PROTOCOL . ' ' . static::TAG_REQUEST_METHOD . ' ' . static::TAG_URI),
Expand All @@ -61,11 +61,6 @@ public function getTemplate(): array
}
}

protected function getLineBreak(): string
{
return "\n<br>\n";
}

protected function prepare(string $value): string
{
$preDocument = strtr(static::HTML_TEMPLATE, [
Expand All @@ -84,9 +79,7 @@ private function wrapTitle(string $value): string

private function wrapSectionTitle(string $value): string
{
$value = str_replace('# ', $this->wrapHidden('#&nbsp;'), $value);

return '<div class="title">' . $value . '</div>';
return '<div class="title">' . str_replace('# ', $this->wrapHidden('#&nbsp;'), $value) . '</div>';
}

private function wrapHidden(string $value): string
Expand Down
Expand Up @@ -32,7 +32,7 @@ public function getTemplate(): array
static::SECTION_TITLE => static::TAG_TITLE . ' in ' . static::TAG_FILE_LINE,
static::SECTION_MESSAGE => '# Message ' . static::TAG_CODE_WRAP . "\n" . '%message%',
static::SECTION_TIME => '# Time' . "\n" . static::TAG_DATE_TIME_UTC_ATOM . ' [' . static::TAG_TIMESTAMP . ']',
static::SECTION_ID => '# Incident ID:' . static::TAG_ID . "\n" . 'Logged at ' . static::TAG_LOG_FILENAME,
static::SECTION_ID => '# Incident ID:' . static::TAG_ID . "\n" . 'Logged at ' . static::TAG_LOG_DESTINATION,
static::SECTION_STACK => '# Stack trace' . "\n" . static::TAG_STACK,
static::SECTION_CLIENT => '# Client' . "\n" . static::TAG_CLIENT_IP . ' ' . static::TAG_CLIENT_USER_AGENT,
static::SECTION_REQUEST => '# Request' . "\n" . static::TAG_SERVER_PROTOCOL . ' ' . static::TAG_REQUEST_METHOD . ' ' . static::TAG_URI,
Expand Down
Expand Up @@ -31,8 +31,6 @@ final class Exception implements ExceptionInterface

private string $type;

private string $loggerLevel;

private string $message;

private string $file;
Expand All @@ -59,7 +57,7 @@ public function __construct(\Exception $exception)
$this->severity = ExceptionInterface::DEFAULT_ERROR_TYPE;
}
$this->assertSeverity();
$this->loggerLevel = ExceptionInterface::ERROR_LEVELS[$this->severity];
// $this->loggerLevel = ExceptionInterface::ERROR_LEVELS[$this->severity];
$this->type = ExceptionInterface::ERROR_TYPES[$this->severity];
$this->message = $exception->getMessage();
$this->file = $exception->getFile();
Expand Down Expand Up @@ -99,14 +97,6 @@ public function type(): string
return $this->type;
}

/**
* {@inheritdoc}
*/
public function loggerLevel(): string
{
return $this->loggerLevel;
}

/**
* {@inheritdoc}
*/
Expand Down
Expand Up @@ -14,19 +14,13 @@
namespace Chevere\Components\ExceptionHandler;

use BadMethodCallException;
use DateTime;
use DateTimeZone;
use DateTimeInterface;
use Chevere\Components\App\Instances\RuntimeInstance;
use Chevere\Components\ExceptionHandler\Documents\ConsoleDocument;
use Chevere\Components\ExceptionHandler\Formatters\ConsoleFormatter;
use DateTimeImmutable;
use Chevere\Components\ExceptionHandler\Interfaces\ExceptionHandlerInterface;
use Chevere\Components\ExceptionHandler\Interfaces\ExceptionInterface;
use Chevere\Components\Http\Interfaces\RequestInterface;
use Chevere\Components\Message\Message;
use Chevere\Components\Runtime\Interfaces\RuntimeInterface;
use DateTimeImmutable;
use Monolog\Logger;

/**
* The Chevere exception handler.
Expand All @@ -39,17 +33,15 @@ final class ExceptionHandler implements ExceptionHandlerInterface

private string $id;

// private RuntimeInterface $runtime;

private RequestInterface $request;

private Logger $logger;

private string $loggerFilename;

private bool $isDebug = false;

private string $logDestination = '/dev/null';

/**
* Creates a new instance.
*
* @param mixed $args Arguments passed to the error exception (severity, message, file, line; Exception)
*/
public function __construct(\Exception $exception)
Expand All @@ -59,25 +51,33 @@ public function __construct(\Exception $exception)
$this->id = uniqid('', true);
}

public static function function($exception): void
{
}

/**
* {@inheritdoc}
*/
public function dateTimeUtc(): DateTimeInterface
{
return $this->dateTimeUtc;
}

/**
* {@inheritdoc}
*/
public function exception(): ExceptionInterface
{
return $this->exception;
}

/**
* {@inheritdoc}
*/
public function id(): string
{
return $this->id;
}

/**
* {@inheritdoc}
*/
public function withIsDebug(bool $isDebug): ExceptionHandlerInterface
{
$new = clone $this;
Expand All @@ -86,6 +86,9 @@ public function withIsDebug(bool $isDebug): ExceptionHandlerInterface
return $new;
}

/**
* {@inheritdoc}
*/
public function isDebug(): bool
{
return $this->isDebug;
Expand All @@ -102,6 +105,9 @@ public function withRequest(RequestInterface $request): ExceptionHandlerInterfac
return $new;
}

/**
* {@inheritdoc}
*/
public function hasRequest(): bool
{
return isset($this->request);
Expand All @@ -117,24 +123,17 @@ public function request(): RequestInterface
return $this->request;
}

public function withLogger(Logger $logger): ExceptionHandlerInterface
public function withLogDestination(string $logDestination): ExceptionHandlerInterface
{
$new = clone $this;
$new->logger = $logger;
$new->logDestination = $logDestination;

return $new;
}

public function hasLogger(): bool
{
return isset($this->logger);
}

public function logger(): Logger
public function logDestination(): string
{
$this->assertPropertyMethod();

return $this->logger;
return $this->logDestination;
}

private function assertPropertyMethod(): void
Expand Down
Expand Up @@ -59,4 +59,12 @@ public function getHr(): string
{
return '------------------------------------------------------------';
}

/**
* {@inheritdoc}
*/
public function getLineBreak(): string
{
return "\n\n";
}
}
Expand Up @@ -36,4 +36,9 @@ public function getHr(): string
{
return '<div class="hr"><span>------------------------------------------------------------</span></div>';
}

public function getLineBreak(): string
{
return "\n<br>\n";
}
}
Expand Up @@ -31,7 +31,7 @@ interface DocumentInterface
const TAG_FILE_LINE = '%fileLine%';
const TAG_DATE_TIME_UTC_ATOM = '%dateTimeUtcAtom%';
const TAG_TIMESTAMP = '%timestamp%';
const TAG_LOG_FILENAME = '%logFilename%';
const TAG_LOG_DESTINATION = '%logDestination%';
const TAG_STACK = '%stack%';
const TAG_CLIENT_IP = '%clientIp%';
const TAG_CLIENT_USER_AGENT = '%clientUserAgent%';
Expand Down
Expand Up @@ -13,34 +13,67 @@

namespace Chevere\Components\ExceptionHandler\Interfaces;

use Chevere\Components\Http\Interfaces\RequestInterface;
use Chevere\Components\Runtime\Interfaces\RuntimeInterface;
use DateTimeInterface;
use Monolog\Logger;
use Chevere\Components\Http\Interfaces\RequestInterface;

interface ExceptionHandlerInterface
{
public function __construct(\Exception $exception);

/**
* Provides access to the instance DateTime UTC.
*/
public function dateTimeUtc(): DateTimeInterface;

/**
* Provides access to the instance exception.
*/
public function exception(): ExceptionInterface;

/**
* Provides access to the instance id.
*/
public function id(): string;

/**
* Return an instance with the specified debug flag.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified debug flag.
*/
public function withIsDebug(bool $isDebug): ExceptionHandlerInterface;

/**
* Provides access to the instance debug flag.
*/
public function isDebug(): bool;

/**
* Return an instance with the specified RequestInterface.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified RequestInterface.
*/
public function withRequest(RequestInterface $request): ExceptionHandlerInterface;

/**
* Returns a boolean indicating whether the instance has a RequestInterface.
*/
public function hasRequest(): bool;

/**
* Provides access to the RequestInterface instance.
*/
public function request(): RequestInterface;

public function withLogger(Logger $logger): ExceptionHandlerInterface;

public function hasLogger(): bool;

public function logger(): Logger;
/**
* Return an instance with the specified log destination.
*
* This method MUST retain the state of the current instance, and return
* an instance that contains the specified log destination.
*/
public function withLogDestination(string $logDestination): ExceptionHandlerInterface;

/**
* Provides access to the instance log destination.
*/
public function logDestination(): string;
}

0 comments on commit f6d4ee2

Please sign in to comment.