summaryrefslogtreecommitdiff
path: root/src/Exceptions/Handlers/Whoops.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/Handlers/Whoops.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/Handlers/Whoops.php')
-rw-r--r--src/Exceptions/Handlers/Whoops.php85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/Exceptions/Handlers/Whoops.php b/src/Exceptions/Handlers/Whoops.php
new file mode 100644
index 00000000..807f5eb0
--- /dev/null
+++ b/src/Exceptions/Handlers/Whoops.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Engelsystem\Exceptions\Handlers;
+
+use Engelsystem\Application;
+use Engelsystem\Container\Container;
+use Engelsystem\Http\Request;
+use Throwable;
+use Whoops\Handler\JsonResponseHandler;
+use Whoops\Handler\PrettyPageHandler;
+use Whoops\Run as WhoopsRunner;
+
+class Whoops extends Legacy implements HandlerInterface
+{
+ /** @var Application */
+ protected $app;
+
+ public function __construct(Container $app)
+ {
+ $this->app = $app;
+ }
+
+ /**
+ * @param Request $request
+ * @param Throwable $e
+ */
+ public function render($request, Throwable $e)
+ {
+ $whoops = $this->app->make(WhoopsRunner::class);
+ $handler = $this->getPrettyPageHandler($e);
+ $whoops->pushHandler($handler);
+
+ if ($request->isXmlHttpRequest()) {
+ $handler = $this->getJsonResponseHandler();
+ $whoops->pushHandler($handler);
+ }
+
+ echo $whoops->handleException($e);
+ }
+
+ /**
+ * @param Throwable $e
+ * @return PrettyPageHandler
+ */
+ protected function getPrettyPageHandler(Throwable $e)
+ {
+ $handler = $this->app->make(PrettyPageHandler::class);
+
+ $handler->setPageTitle('Just another ' . get_class($e) . ' to fix :(');
+ $handler->setApplicationPaths([realpath(__DIR__ . '/../..')]);
+
+ $data = $this->getData();
+ $handler->addDataTable('Application', $data);
+
+ return $handler;
+ }
+
+ /**
+ * @return JsonResponseHandler
+ */
+ protected function getJsonResponseHandler()
+ {
+ $handler = $this->app->make(JsonResponseHandler::class);
+ $handler->setJsonApi(true);
+ $handler->addTraceToOutput(true);
+
+ return $handler;
+ }
+
+ /**
+ * Aggregate application data
+ *
+ * @return array
+ */
+ protected function getData()
+ {
+ global $user;
+
+ $data = [];
+ $data['user'] = $user;
+ $data['Booted'] = $this->app->isBooted();
+
+ return $data;
+ }
+}