summaryrefslogtreecommitdiff
path: root/src/Exceptions
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2017-11-20 17:08:05 +0100
committerIgor Scheller <igor.scheller@igorshp.de>2017-11-25 11:27:38 +0100
commit6eea072376cc9fd1034342a0e1d2173681268138 (patch)
treea7331868cb29fad083f93fe6f8e31e918da25bdd /src/Exceptions
parente54a10b81679bae9d19337617d6c58310d2f7a58 (diff)
Added ExceptionHandler Interface
Diffstat (limited to 'src/Exceptions')
-rw-r--r--src/Exceptions/BasicHandler.php119
-rw-r--r--src/Exceptions/ExceptionsServiceProvider.php5
-rw-r--r--src/Exceptions/Handler.php112
3 files changed, 130 insertions, 106 deletions
diff --git a/src/Exceptions/BasicHandler.php b/src/Exceptions/BasicHandler.php
new file mode 100644
index 00000000..2ba960a2
--- /dev/null
+++ b/src/Exceptions/BasicHandler.php
@@ -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);
+ }
+}
diff --git a/src/Exceptions/ExceptionsServiceProvider.php b/src/Exceptions/ExceptionsServiceProvider.php
index 7755e1e7..8eeccf61 100644
--- a/src/Exceptions/ExceptionsServiceProvider.php
+++ b/src/Exceptions/ExceptionsServiceProvider.php
@@ -3,13 +3,14 @@
namespace Engelsystem\Exceptions;
use Engelsystem\Container\ServiceProvider;
-use Engelsystem\Exceptions\Handler as ExceptionHandler;
class ExceptionsServiceProvider extends ServiceProvider
{
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->bind(Handler::class, 'error.handler');
}
}
diff --git a/src/Exceptions/Handler.php b/src/Exceptions/Handler.php
index 95bcd132..cdf94e32 100644
--- a/src/Exceptions/Handler.php
+++ b/src/Exceptions/Handler.php
@@ -2,9 +2,7 @@
namespace Engelsystem\Exceptions;
-use Throwable;
-
-class Handler
+abstract class Handler
{
/** @var string */
protected $environment;
@@ -20,122 +18,28 @@ class Handler
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)
- {
- $trace = array_reverse(debug_backtrace());
-
- $this->handle('error', $number, $string, $file, $line, $context, $trace);
}
/**
- * @param Throwable $e
+ * Activate the error handler
*/
- public function exceptionHandler($e)
+ public function register()
{
- $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
+ * @param string $environment
*/
- protected function formatStackTrace($stackTrace)
+ public function setEnvironment($environment)
{
- $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;
+ $this->environment = $environment;
}
/**
- * @param string $path
* @return string
*/
- protected function stripBasePath($path)
+ public function getEnvironment()
{
- $basePath = realpath(__DIR__ . '/../..') . '/';
- return str_replace($basePath, '', $path);
- }
-
- /**
- * @param string $environment
- */
- public function setEnvironment($environment)
- {
- $this->environment = $environment;
+ return $this->environment;
}
}