summaryrefslogtreecommitdiff
path: root/src/Exceptions/Handler.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Exceptions/Handler.php')
-rw-r--r--src/Exceptions/Handler.php100
1 files changed, 95 insertions, 5 deletions
diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php
index cdf94e32..ee15717a 100644
--- a/src/Exceptions/Handler.php
+++ b/src/Exceptions/Handler.php
@@ -2,18 +2,29 @@
namespace Engelsystem\Exceptions;
-abstract class Handler
+use Engelsystem\Exceptions\Handlers\HandlerInterface;
+use Engelsystem\Http\Request;
+use ErrorException;
+use Throwable;
+
+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)
{
@@ -25,14 +36,47 @@ abstract class Handler
*/
public function register()
{
+ set_error_handler([$this, 'errorHandler']);
+ set_exception_handler([$this, 'exceptionHandler']);
}
/**
- * @param string $environment
+ * @param int $number
+ * @param string $message
+ * @param string $file
+ * @param int $line
*/
- public function setEnvironment($environment)
+ public function errorHandler($number, $message, $file, $line)
{
- $this->environment = $environment;
+ $exception = new ErrorException($message, 0, $number, $file, $line);
+ $this->exceptionHandler($exception);
+ }
+
+ /**
+ * @param Throwable $e
+ */
+ public function exceptionHandler($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();
+ }
+
+ /**
+ * Exit the application
+ *
+ * @codeCoverageIgnore
+ * @param string $message
+ */
+ protected function die($message = '')
+ {
+ echo $message;
+ die();
}
/**
@@ -42,4 +86,50 @@ abstract class Handler
{
return $this->environment;
}
+
+ /**
+ * @param string $environment
+ */
+ public function setEnvironment($environment)
+ {
+ $this->environment = $environment;
+ }
+
+ /**
+ * @param string $environment
+ * @return HandlerInterface|HandlerInterface[]
+ */
+ public function getHandler($environment = null)
+ {
+ if (!is_null($environment)) {
+ return $this->handler[$environment];
+ }
+
+ return $this->handler;
+ }
+
+ /**
+ * @param string $environment
+ * @param HandlerInterface $handler
+ */
+ public function setHandler($environment, HandlerInterface $handler)
+ {
+ $this->handler[$environment] = $handler;
+ }
+
+ /**
+ * @return Request
+ */
+ public function getRequest()
+ {
+ return $this->request;
+ }
+
+ /**
+ * @param Request $request
+ */
+ public function setRequest(Request $request)
+ {
+ $this->request = $request;
+ }
}