summaryrefslogtreecommitdiff
path: root/src/Exceptions/Handler.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-11-26 11:07:27 +0100
committerGitHub <noreply@github.com>2017-11-26 11:07:27 +0100
commiteda7f7788ea8012bd8be46405c56a666c11f3fa5 (patch)
tree50cf84d7d07d11bd65b45c2c17f37632f6cd8eff /src/Exceptions/Handler.php
parente54a10b81679bae9d19337617d6c58310d2f7a58 (diff)
parentb9bc03a1bdf146e0b4ef6529ebe814a0ac9c930d (diff)
Merge pull request #364 from MyIgel/master
Refactored error handling, changed tests from MySQL to MariaDB
Diffstat (limited to 'src/Exceptions/Handler.php')
-rw-r--r--src/Exceptions/Handler.php144
1 files changed, 69 insertions, 75 deletions
diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php
index 95bcd132..ee15717a 100644
--- a/src/Exceptions/Handler.php
+++ b/src/Exceptions/Handler.php
@@ -2,6 +2,9 @@
namespace Engelsystem\Exceptions;
+use Engelsystem\Exceptions\Handlers\HandlerInterface;
+use Engelsystem\Http\Request;
+use ErrorException;
use Throwable;
class Handler
@@ -9,34 +12,44 @@ class Handler
/** @var string */
protected $environment;
+ /** @var HandlerInterface[] */
+ protected $handler = [];
+
+ /** @var Request */
+ protected $request;
+
const ENV_PRODUCTION = 'prod';
const ENV_DEVELOPMENT = 'dev';
/**
* Handler constructor.
*
- * @param string $environment production|development
+ * @param string $environment prod|dev
*/
public function __construct($environment = self::ENV_PRODUCTION)
{
$this->environment = $environment;
+ }
+ /**
+ * Activate the error handler
+ */
+ public function register()
+ {
set_error_handler([$this, 'errorHandler']);
set_exception_handler([$this, 'exceptionHandler']);
}
/**
* @param int $number
- * @param string $string
+ * @param string $message
* @param string $file
* @param int $line
- * @param array $context
*/
- public function errorHandler($number, $string, $file, $line, $context)
+ public function errorHandler($number, $message, $file, $line)
{
- $trace = array_reverse(debug_backtrace());
-
- $this->handle('error', $number, $string, $file, $line, $context, $trace);
+ $exception = new ErrorException($message, 0, $number, $file, $line);
+ $this->exceptionHandler($exception);
}
/**
@@ -44,98 +57,79 @@ class Handler
*/
public function exceptionHandler($e)
{
- $this->handle(
- 'exception',
- $e->getCode(),
- get_class($e) . ': ' . $e->getMessage(),
- $e->getFile(),
- $e->getLine(),
- ['exception' => $e]
- );
+ if (!$this->request instanceof Request) {
+ $this->request = new Request();
+ }
+
+ $handler = $this->handler[$this->environment];
+ $handler->report($e);
+ $handler->render($this->request, $e);
+ $this->die();
}
/**
- * @param string $type
- * @param int $number
- * @param string $string
- * @param string $file
- * @param int $line
- * @param array $context
- * @param array $trace
+ * Exit the application
+ *
+ * @codeCoverageIgnore
+ * @param string $message
*/
- protected function handle($type, $number, $string, $file, $line, $context = [], $trace = [])
+ protected function die($message = '')
{
- 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.';
+ echo $message;
die();
}
/**
- * @param array $stackTrace
- * @return array
+ * @return string
*/
- protected function formatStackTrace($stackTrace)
+ public function getEnvironment()
{
- $return = [];
-
- foreach ($stackTrace as $trace) {
- $path = '';
- $line = '';
-
- if (isset($trace['file']) && isset($trace['line'])) {
- $path = $this->stripBasePath($trace['file']);
- $line = $trace['line'];
- }
+ return $this->environment;
+ }
- $functionName = $trace['function'];
+ /**
+ * @param string $environment
+ */
+ public function setEnvironment($environment)
+ {
+ $this->environment = $environment;
+ }
- $return[] = [
- 'file' => $path . ':' . $line,
- $functionName => $trace['args'],
- ];
+ /**
+ * @param string $environment
+ * @return HandlerInterface|HandlerInterface[]
+ */
+ public function getHandler($environment = null)
+ {
+ if (!is_null($environment)) {
+ return $this->handler[$environment];
}
- return $return;
+ return $this->handler;
}
/**
- * @param string $path
- * @return string
+ * @param string $environment
+ * @param HandlerInterface $handler
*/
- protected function stripBasePath($path)
+ public function setHandler($environment, HandlerInterface $handler)
{
- $basePath = realpath(__DIR__ . '/../..') . '/';
- return str_replace($basePath, '', $path);
+ $this->handler[$environment] = $handler;
}
/**
- * @param string $environment
+ * @return Request
*/
- public function setEnvironment($environment)
+ public function getRequest()
{
- $this->environment = $environment;
+ return $this->request;
+ }
+
+ /**
+ * @param Request $request
+ */
+ public function setRequest(Request $request)
+ {
+ $this->request = $request;
}
}