diff --git a/composer.json b/composer.json index e9eedcb..99982d3 100644 --- a/composer.json +++ b/composer.json @@ -9,6 +9,7 @@ "require": { "php": "^7.2", "ext-json": "*", + "filp/whoops": "^2.6", "php-di/php-di": "^6.0", "twig/twig": "^3.0" }, diff --git a/composer.lock b/composer.lock index 3f5bdc6..d32a9e1 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,69 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "7da10fc34e3c55211c7a134e7e79f040", + "content-hash": "8dc6d12612bce2ab7a927687ee0aeca5", "packages": [ + { + "name": "filp/whoops", + "version": "2.6.0", + "source": { + "type": "git", + "url": "https://github.com/filp/whoops.git", + "reference": "ecbc8f3ed2cafca3cfca3d5febaae5a9d2899508" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/filp/whoops/zipball/ecbc8f3ed2cafca3cfca3d5febaae5a9d2899508", + "reference": "ecbc8f3ed2cafca3cfca3d5febaae5a9d2899508", + "shasum": "" + }, + "require": { + "php": "^5.5.9 || ^7.0", + "psr/log": "^1.0.1" + }, + "require-dev": { + "mockery/mockery": "^0.9 || ^1.0", + "phpunit/phpunit": "^4.8.35 || ^5.7 || ^6.0", + "symfony/var-dumper": "^2.6 || ^3.0 || ^4.0 || ^5.0" + }, + "suggest": { + "symfony/var-dumper": "Pretty print complex values better with var-dumper available", + "whoops/soap": "Formats errors as SOAP responses" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5-dev" + } + }, + "autoload": { + "psr-4": { + "Whoops\\": "src/Whoops/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Filipe Dobreira", + "homepage": "https://github.com/filp", + "role": "Developer" + } + ], + "description": "php error handling for cool kids", + "homepage": "https://filp.github.io/whoops/", + "keywords": [ + "error", + "exception", + "handling", + "library", + "throwable", + "whoops" + ], + "time": "2019-12-25T10:00:00+00:00" + }, { "name": "jeremeamia/superclosure", "version": "2.4.0", @@ -308,6 +369,53 @@ ], "time": "2017-02-14T16:28:37+00:00" }, + { + "name": "psr/log", + "version": "1.1.2", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "446d54b4cb6bf489fc9d75f55843658e6f25d801" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/446d54b4cb6bf489fc9d75f55843658e6f25d801", + "reference": "446d54b4cb6bf489fc9d75f55843658e6f25d801", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Log\\": "Psr/Log/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "homepage": "https://github.com/php-fig/log", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2019-11-01T11:05:21+00:00" + }, { "name": "symfony/polyfill-ctype", "version": "v1.13.1", diff --git a/src/Runtime/Exceptions/ExceptionHandler.php b/src/Runtime/Exceptions/ExceptionHandler.php index e356537..f3d8f07 100644 --- a/src/Runtime/Exceptions/ExceptionHandler.php +++ b/src/Runtime/Exceptions/ExceptionHandler.php @@ -2,19 +2,54 @@ namespace Runtime\Exceptions; +use Whoops\Handler\JsonResponseHandler; +use Whoops\Handler\PlainTextHandler; +use Whoops\Run; +use Whoops\Handler\PrettyPageHandler; +use Whoops\Util\Misc; + class ExceptionHandler { - public static function make($abstract = null) + /** + * @var Run + */ + private static $handler; + + /** + * @param null $abstract + * @param null $message + */ + public static function make($abstract = null, $message = null) { - if(is_object($abstract) && $abstract instanceof \Exception) { - echo $abstract->getMessage(); - die(); + if(is_string($abstract)) { + $abstract = new $abstract($message); } - echo 'Error: ' . $abstract; + if(is_subclass_of($abstract, 'Exception')) { + self::$handler->handleException($abstract); + } die(); } + /** + * Register Whoops (Error handler) + */ + public static function register() + { + self::$handler = new Run(); + self::$handler->pushHandler(new PrettyPageHandler()); + + if (Misc::isAjaxRequest()){ + self::$handler->pushHandler(new JsonResponseHandler()); + } + + if (Misc::isCommandLine()){ + self::$handler->pushHandler(new PlainTextHandler()); + } + + self::$handler->register(); + } + } diff --git a/src/Runtime/Factory/BootstrapFactory.php b/src/Runtime/Factory/BootstrapFactory.php index 9e751e8..c591747 100644 --- a/src/Runtime/Factory/BootstrapFactory.php +++ b/src/Runtime/Factory/BootstrapFactory.php @@ -16,6 +16,7 @@ class BootstrapFactory { */ public function handle() { + ExceptionHandler::register(); Route::setDefaultNamespace('\App\Http\Controllers'); require_once('../routes/web.php'); @@ -65,7 +66,7 @@ class BootstrapFactory { } } - ExceptionHandler::make(ClassNotFoundException::class); + ExceptionHandler::make(ClassNotFoundException::class, sprintf("Class '%s' not found", $abstract)); return null; }