Added ExceptionHandler Interface

This commit is contained in:
Igor Scheller 2017-11-20 17:08:05 +01:00
parent e54a10b816
commit 6eea072376
6 changed files with 176 additions and 112 deletions

View File

@ -2,7 +2,7 @@
use Engelsystem\Application; use Engelsystem\Application;
use Engelsystem\Config\Config; use Engelsystem\Config\Config;
use Engelsystem\Exceptions\Handler as ExceptionHandler; use Engelsystem\Exceptions\BasicHandler as ExceptionHandler;
/** /**
* This file includes all needed functions, connects to the db etc. * This file includes all needed functions, connects to the db etc.

View File

@ -0,0 +1,119 @@
<?php
namespace Engelsystem\Exceptions;
use ErrorException;
use Throwable;
class BasicHandler extends Handler
{
/**
* Activate the error handler
*/
public function register()
{
set_error_handler([$this, 'errorHandler']);
set_exception_handler([$this, 'exceptionHandler']);
}
/**
* @param int $number
* @param string $message
* @param string $file
* @param int $line
*/
public function errorHandler($number, $message, $file, $line)
{
$exception = new ErrorException($message, 0, $number, $file, $line);
$this->exceptionHandler($exception);
}
/**
* @param Throwable $e
*/
public function exceptionHandler($e)
{
$this->handle(
$e->getCode(),
get_class($e) . ': ' . $e->getMessage(),
$e->getFile(),
$e->getLine(),
['exception' => $e]
);
}
/**
* @param int $number
* @param string $string
* @param string $file
* @param int $line
* @param array $context
* @param array $trace
*/
protected function handle($number, $string, $file, $line, $context = [], $trace = [])
{
error_log(sprintf('Exception: Number: %s, String: %s, File: %s:%u, Context: %s',
$number,
$string,
$file,
$line,
json_encode($context)
));
$file = $this->stripBasePath($file);
if ($this->environment == self::ENV_DEVELOPMENT) {
echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;max-height: 90%;overflow-y:auto;">';
echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
var_export([
'string' => $string,
'file' => $file . ':' . $line,
'context' => $context,
'stacktrace' => $this->formatStackTrace($trace),
]);
echo '</pre>';
die();
}
echo 'An <del>un</del>expected error occurred, a team of untrained monkeys has been dispatched to deal with it.';
die();
}
/**
* @param array $stackTrace
* @return array
*/
protected function formatStackTrace($stackTrace)
{
$return = [];
foreach ($stackTrace as $trace) {
$path = '';
$line = '';
if (isset($trace['file']) && isset($trace['line'])) {
$path = $this->stripBasePath($trace['file']);
$line = $trace['line'];
}
$functionName = $trace['function'];
$return[] = [
'file' => $path . ':' . $line,
$functionName => $trace['args'],
];
}
return $return;
}
/**
* @param string $path
* @return string
*/
protected function stripBasePath($path)
{
$basePath = realpath(__DIR__ . '/../..') . '/';
return str_replace($basePath, '', $path);
}
}

View File

@ -3,13 +3,14 @@
namespace Engelsystem\Exceptions; namespace Engelsystem\Exceptions;
use Engelsystem\Container\ServiceProvider; use Engelsystem\Container\ServiceProvider;
use Engelsystem\Exceptions\Handler as ExceptionHandler;
class ExceptionsServiceProvider extends ServiceProvider class ExceptionsServiceProvider extends ServiceProvider
{ {
public function register() public function register()
{ {
$errorHandler = $this->app->make(ExceptionHandler::class); $errorHandler = $this->app->make(BasicHandler::class);
$errorHandler->register();
$this->app->instance('error.handler', $errorHandler); $this->app->instance('error.handler', $errorHandler);
$this->app->bind(Handler::class, 'error.handler');
} }
} }

View File

@ -2,9 +2,7 @@
namespace Engelsystem\Exceptions; namespace Engelsystem\Exceptions;
use Throwable; abstract class Handler
class Handler
{ {
/** @var string */ /** @var string */
protected $environment; protected $environment;
@ -20,115 +18,13 @@ class Handler
public function __construct($environment = self::ENV_PRODUCTION) public function __construct($environment = self::ENV_PRODUCTION)
{ {
$this->environment = $environment; $this->environment = $environment;
set_error_handler([$this, 'errorHandler']);
set_exception_handler([$this, 'exceptionHandler']);
} }
/** /**
* @param int $number * Activate the error handler
* @param string $string
* @param string $file
* @param int $line
* @param array $context
*/ */
public function errorHandler($number, $string, $file, $line, $context) public function register()
{ {
$trace = array_reverse(debug_backtrace());
$this->handle('error', $number, $string, $file, $line, $context, $trace);
}
/**
* @param Throwable $e
*/
public function exceptionHandler($e)
{
$this->handle(
'exception',
$e->getCode(),
get_class($e) . ': ' . $e->getMessage(),
$e->getFile(),
$e->getLine(),
['exception' => $e]
);
}
/**
* @param string $type
* @param int $number
* @param string $string
* @param string $file
* @param int $line
* @param array $context
* @param array $trace
*/
protected function handle($type, $number, $string, $file, $line, $context = [], $trace = [])
{
error_log(sprintf('%s: Number: %s, String: %s, File: %s:%u, Context: %s',
$type,
$number,
$string,
$file,
$line,
json_encode($context)
));
$file = $this->stripBasePath($file);
if ($this->environment == self::ENV_DEVELOPMENT) {
echo '<pre style="background-color:#333;color:#ccc;z-index:1000;position:fixed;bottom:1em;padding:1em;width:97%;max-height: 90%;overflow-y:auto;">';
echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
var_export([
'string' => $string,
'file' => $file . ':' . $line,
'context' => $context,
'stacktrace' => $this->formatStackTrace($trace),
]);
echo '</pre>';
die();
}
echo 'An <del>un</del>expected error occurred, a team of untrained monkeys has been dispatched to deal with it.';
die();
}
/**
* @param array $stackTrace
* @return array
*/
protected function formatStackTrace($stackTrace)
{
$return = [];
foreach ($stackTrace as $trace) {
$path = '';
$line = '';
if (isset($trace['file']) && isset($trace['line'])) {
$path = $this->stripBasePath($trace['file']);
$line = $trace['line'];
}
$functionName = $trace['function'];
$return[] = [
'file' => $path . ':' . $line,
$functionName => $trace['args'],
];
}
return $return;
}
/**
* @param string $path
* @return string
*/
protected function stripBasePath($path)
{
$basePath = realpath(__DIR__ . '/../..') . '/';
return str_replace($basePath, '', $path);
} }
/** /**
@ -138,4 +34,12 @@ class Handler
{ {
$this->environment = $environment; $this->environment = $environment;
} }
/**
* @return string
*/
public function getEnvironment()
{
return $this->environment;
}
} }

View File

@ -2,8 +2,9 @@
namespace Engelsystem\Test\Unit\Exceptions; namespace Engelsystem\Test\Unit\Exceptions;
use Engelsystem\Exceptions\BasicHandler as ExceptionHandler;
use Engelsystem\Exceptions\ExceptionsServiceProvider; use Engelsystem\Exceptions\ExceptionsServiceProvider;
use Engelsystem\Exceptions\Handler as ExceptionHandler; use Engelsystem\Exceptions\Handler;
use Engelsystem\Test\Unit\ServiceProviderTest; use Engelsystem\Test\Unit\ServiceProviderTest;
use PHPUnit_Framework_MockObject_MockObject; use PHPUnit_Framework_MockObject_MockObject;
@ -18,10 +19,11 @@ class ExceptionsServiceProviderTest extends ServiceProviderTest
$exceptionHandler = $this->getMockBuilder(ExceptionHandler::class) $exceptionHandler = $this->getMockBuilder(ExceptionHandler::class)
->getMock(); ->getMock();
$app = $this->getApp(); $app = $this->getApp(['make', 'instance', 'bind']);
$this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler); $this->setExpects($app, 'make', [ExceptionHandler::class], $exceptionHandler);
$this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]); $this->setExpects($app, 'instance', ['error.handler', $exceptionHandler]);
$this->setExpects($app, 'bind', [Handler::class, 'error.handler']);
$serviceProvider = new ExceptionsServiceProvider($app); $serviceProvider = new ExceptionsServiceProvider($app);
$serviceProvider->register(); $serviceProvider->register();

View File

@ -0,0 +1,38 @@
<?php
namespace Engelsystem\Test\Unit\Exceptions;
use Engelsystem\Exceptions\Handler;
use PHPUnit\Framework\TestCase;
use PHPUnit_Framework_MockObject_MockObject as Mock;
class HandlerTest extends TestCase
{
/**
* @covers \Engelsystem\Exceptions\Handler::__construct()
* @covers \Engelsystem\Exceptions\Handler::register()
*/
public function testRegister()
{
/** @var Handler|Mock $handler */
$handler = $this->getMockForAbstractClass(Handler::class);
$this->assertInstanceOf(Handler::class, $handler);
$handler->register();
}
/**
* @covers \Engelsystem\Exceptions\Handler::setEnvironment()
* @covers \Engelsystem\Exceptions\Handler::getEnvironment()
*/
public function testEnvironment()
{
/** @var Handler|Mock $handler */
$handler = $this->getMockForAbstractClass(Handler::class);
$handler->setEnvironment(Handler::ENV_DEVELOPMENT);
$this->assertEquals(Handler::ENV_DEVELOPMENT, $handler->getEnvironment());
$handler->setEnvironment(Handler::ENV_PRODUCTION);
$this->assertEquals(Handler::ENV_PRODUCTION, $handler->getEnvironment());
}
}