summaryrefslogtreecommitdiff
path: root/src/Exceptions/Handler.php
diff options
context:
space:
mode:
authormsquare <msquare@notrademark.de>2017-06-20 16:50:21 +0200
committermsquare <msquare@notrademark.de>2017-06-20 16:50:21 +0200
commit56814fa2fdf58b4013f4d57c5ea87619c7122957 (patch)
tree640945769b7e9626cdf43162c786147f5c962029 /src/Exceptions/Handler.php
parenta5fc5bd0979e8de1fce8a8addd351a6e7bd6aeb8 (diff)
parentcd30017b97afc3c7001fbb9eb14b54dbb980b7b6 (diff)
Merge branch 'pr/316' into feature-igel-rewrite
Diffstat (limited to 'src/Exceptions/Handler.php')
-rw-r--r--src/Exceptions/Handler.php97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php
new file mode 100644
index 00000000..0532a7d8
--- /dev/null
+++ b/src/Exceptions/Handler.php
@@ -0,0 +1,97 @@
+<?php
+
+namespace Engelsystem\Exceptions;
+
+use Exception;
+
+class Handler
+{
+ /** @var string */
+ protected $environment;
+
+ const ENV_PRODUCTION = 'prod';
+ const ENV_DEVELOPMENT = 'dev';
+
+ /**
+ * Handler constructor.
+ *
+ * @param string $environment production|development
+ */
+ public function __construct($environment = self::ENV_PRODUCTION)
+ {
+ $this->environment = $environment;
+
+ set_error_handler([$this, 'errorHandler']);
+ set_exception_handler([$this, 'exceptionHandler']);
+ }
+
+ /**
+ * @param int $number
+ * @param string $string
+ * @param string $file
+ * @param int $line
+ * @param array $context
+ */
+ public function errorHandler($number, $string, $file, $line, $context)
+ {
+ $this->handle('error', $number, $string, $file, $line, $context);
+ }
+
+ /**
+ * @param Exception $e
+ */
+ public function exceptionHandler(Exception $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
+ */
+ protected function handle($type, $number, $string, $file, $line, $context = [])
+ {
+ error_log(sprintf('%s: Number: %s, String: %s, File: %s:%u, Context: %s',
+ $type,
+ $number,
+ $string,
+ $file,
+ $line,
+ json_encode($context)
+ ));
+
+ 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%;overflow-y:auto;">';
+ echo sprintf('%s: (%s)' . PHP_EOL, ucfirst($type), $number);
+ var_export([
+ 'string' => $string,
+ 'file' => $file . ':' . $line,
+ 'context' => ($this->environment == self::ENV_DEVELOPMENT ? $context : null),
+ ]);
+ 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 string $environment
+ */
+ public function setEnvironment($environment)
+ {
+ $this->environment = $environment;
+ }
+}