diff --git a/README.md b/README.md index 0231e44..30c4734 100644 --- a/README.md +++ b/README.md @@ -81,40 +81,40 @@ php -S localhost:8000 | Project | Status | Description | | ---------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------ | -| [leaf] | [![Latest Stable Version](https://poser.pugx.org/leafs/leaf/v/stable)](https://packagist.org/packages/leafs/leaf) [![Total Downloads](https://poser.pugx.org/leafs/leaf/downloads)](https://packagist.org/packages/leafs/leaf) | Create websites and APIs quickly | -| [leafmvc] | [![Latest Stable Version](https://poser.pugx.org/leafs/mvc/v/stable)](https://packagist.org/packages/leafs/mvc) [![Total Downloads](https://poser.pugx.org/leafs/mvc/downloads)](https://packagist.org/packages/leafs/mvc) | An MVC wrapper for leaf (for general development) | -| [leafapi] | [![Latest Stable Version](https://poser.pugx.org/leafs/api/v/stable)](https://packagist.org/packages/leafs/api) [![Total Downloads](https://poser.pugx.org/leafs/api/downloads)](https://packagist.org/packages/leafs/api) | An MVC wrapper for leaf geared towards API development | -| [skeleton] | [![Latest Stable Version](https://poser.pugx.org/leafs/skeleton/v/stable)](https://packagist.org/packages/leafs/skeleton) [![Total Downloads](https://poser.pugx.org/leafs/skeleton/downloads)](https://packagist.org/packages/leafs/skeleton) | Leaf boilerplate for rapid development | -| [leaf-ui] | [![Latest Stable Version](https://poser.pugx.org/leafs/ui/v/stable)](https://packagist.org/packages/leafs/ui) [![Total Downloads](https://poser.pugx.org/leafs/ui/downloads)](https://packagist.org/packages/leafs/ui) | A PHP library for building user interfaces | -| [cli] | [![Latest Stable Version](https://poser.pugx.org/leafs/cli/v/stable)](https://packagist.org/packages/leafs/cli) [![Total Downloads](https://poser.pugx.org/leafs/cli/downloads)](https://packagist.org/packages/leafs/cli) | CLI for interacting with your leaf apps | +| [leaf](https://github.com/leafsphp/leaf) | [![Latest Stable Version](https://poser.pugx.org/leafs/leaf/v/stable)](https://packagist.org/packages/leafs/leaf) [![Total Downloads](https://poser.pugx.org/leafs/leaf/downloads)](https://packagist.org/packages/leafs/leaf) | Create websites and APIs quickly | +| [leafmvc](https://github.com/leafsphp/leafmvc) | [![Latest Stable Version](https://poser.pugx.org/leafs/mvc/v/stable)](https://packagist.org/packages/leafs/mvc) [![Total Downloads](https://poser.pugx.org/leafs/mvc/downloads)](https://packagist.org/packages/leafs/mvc) | An MVC wrapper for leaf (for general development) | +| [leafapi](https://github.com/leafsphp/leafapi) | [![Latest Stable Version](https://poser.pugx.org/leafs/api/v/stable)](https://packagist.org/packages/leafs/api) [![Total Downloads](https://poser.pugx.org/leafs/api/downloads)](https://packagist.org/packages/leafs/api) | An MVC wrapper for leaf geared towards API development | +| [skeleton](https://github.com/leafsphp/skeleton) | [![Latest Stable Version](https://poser.pugx.org/leafs/skeleton/v/stable)](https://packagist.org/packages/leafs/skeleton) [![Total Downloads](https://poser.pugx.org/leafs/skeleton/downloads)](https://packagist.org/packages/leafs/skeleton) | Leaf boilerplate for rapid development | +| [leaf-ui](https://github.com/leafsphp/leaf-ui) | [![Latest Stable Version](https://poser.pugx.org/leafs/ui/v/stable)](https://packagist.org/packages/leafs/ui) [![Total Downloads](https://poser.pugx.org/leafs/ui/downloads)](https://packagist.org/packages/leafs/ui) | A PHP library for building user interfaces | +| [cli](https://github.com/leafsphp/cli) | [![Latest Stable Version](https://poser.pugx.org/leafs/cli/v/stable)](https://packagist.org/packages/leafs/cli) [![Total Downloads](https://poser.pugx.org/leafs/cli/downloads)](https://packagist.org/packages/leafs/cli) | CLI for interacting with your leaf apps | ## 🧩 The Leaf Ecosystem (Modules) | Project | Status | Description | | ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------- | -| [aloe] | [![Latest Stable Version](https://poser.pugx.org/leafs/aloe/v/stable)](https://packagist.org/packages/leafs/aloe) [![Total Downloads](https://poser.pugx.org/leafs/aloe/downloads)](https://packagist.org/packages/leafs/aloe) | Smart console helper for leaf mvc, leaf api and skeleton | -| [router] | [![Latest Stable Version](https://poser.pugx.org/leafs/router/v/stable)](https://packagist.org/packages/leafs/router) [![Total Downloads](https://poser.pugx.org/leafs/router/downloads)](https://packagist.org/packages/leafs/router) | Default router for leaf php | -| [experiments] | [![Latest Stable Version](https://poser.pugx.org/leafs/experimental/v/stable)](https://packagist.org/packages/leafs/experimental) [![Total Downloads](https://poser.pugx.org/leafs/experimental/downloads)](https://packagist.org/packages/leafs/experimental) | collection of experimental modules | -| [mail] | [![Latest Stable Version](https://poser.pugx.org/leafs/mail/v/stable)](https://packagist.org/packages/leafs/mail) [![Total Downloads](https://poser.pugx.org/leafs/mail/downloads)](https://packagist.org/packages/leafs/mail) | Mailing made easy with leaf | -| [auth] | [![Latest Stable Version](https://poser.pugx.org/leafs/auth/v/stable)](https://packagist.org/packages/leafs/auth) [![Total Downloads](https://poser.pugx.org/leafs/auth/downloads)](https://packagist.org/packages/leafs/auth) | Simple but powerful authentication system for your apps | -| [form] | [![Latest Stable Version](https://poser.pugx.org/leafs/form/v/stable)](https://packagist.org/packages/leafs/form) [![Total Downloads](https://poser.pugx.org/leafs/form/downloads)](https://packagist.org/packages/leafs/form) | Form processes and validation | -| [password] | [![Latest Stable Version](https://poser.pugx.org/leafs/password/v/stable)](https://packagist.org/packages/leafs/password) [![Total Downloads](https://poser.pugx.org/leafs/password/downloads)](https://packagist.org/packages/leafs/password) | Password encryption/validation/hashing in one box | -| [db-old] | [![Latest Stable Version](https://poser.pugx.org/leafs/db-old/v/stable)](https://packagist.org/packages/leafs/db-old) [![Total Downloads](https://poser.pugx.org/leafs/db-old/downloads)](https://packagist.org/packages/leafs/db-old) | Leaf Db from v1 (still maintained) | -| [db] | [![Latest Stable Version](https://poser.pugx.org/leafs/db/v/stable)](https://packagist.org/packages/leafs/db) [![Total Downloads](https://poser.pugx.org/leafs/db/downloads)](https://packagist.org/packages/leafs/db) | Leaf Db from v2 (actively maintained) | -| [session] | [![Latest Stable Version](https://poser.pugx.org/leafs/session/v/stable)](https://packagist.org/packages/leafs/session) [![Total Downloads](https://poser.pugx.org/leafs/session/downloads)](https://packagist.org/packages/leafs/session) | PHP sessions made simple | -| [cookie] | [![Latest Stable Version](https://poser.pugx.org/leafs/cookie/v/stable)](https://packagist.org/packages/leafs/cookie) [![Total Downloads](https://poser.pugx.org/leafs/cookie/downloads)](https://packagist.org/packages/leafs/cookie) | Cookie management without the tears | -| [logger] | [![Latest Stable Version](https://poser.pugx.org/leafs/logger/v/stable)](https://packagist.org/packages/leafs/logger) [![Total Downloads](https://poser.pugx.org/leafs/logger/downloads)](https://packagist.org/packages/leafs/logger) | leaf logger module | -| [fs] | [![Latest Stable Version](https://poser.pugx.org/leafs/fs/v/stable)](https://packagist.org/packages/leafs/fs) [![Total Downloads](https://poser.pugx.org/leafs/fs/downloads)](https://packagist.org/packages/leafs/fs) | Awesome filesystem operations + file uploads | -| [date] | [![Latest Stable Version](https://poser.pugx.org/leafs/date/v/stable)](https://packagist.org/packages/leafs/date) [![Total Downloads](https://poser.pugx.org/leafs/date/downloads)](https://packagist.org/packages/leafs/date) | PHP dates for humans | -| [bareui] | [![Latest Stable Version](https://poser.pugx.org/leafs/bareui/v/stable)](https://packagist.org/packages/leafs/bareui) [![Total Downloads](https://poser.pugx.org/leafs/bareui/downloads)](https://packagist.org/packages/leafs/bareui) | Dead simple templating engine with no compilation (blazing speed) | -| [blade] | [![Latest Stable Version](https://poser.pugx.org/leafs/blade/v/stable)](https://packagist.org/packages/leafs/blade) [![Total Downloads](https://poser.pugx.org/leafs/blade/downloads)](https://packagist.org/packages/leafs/blade) | Laravel blade templating port for leaf | -| [veins] | [![Latest Stable Version](https://poser.pugx.org/leafs/veins/v/stable)](https://packagist.org/packages/leafs/veins) [![Total Downloads](https://poser.pugx.org/leafs/veins/downloads)](https://packagist.org/packages/leafs/veins) | Leaf veins templating engine | -| [http] | [![Latest Stable Version](https://poser.pugx.org/leafs/http/v/stable)](https://packagist.org/packages/leafs/http) [![Total Downloads](https://poser.pugx.org/leafs/http/downloads)](https://packagist.org/packages/leafs/http) | Http operations made simple (request, response, ...) | -| [anchor] | [![Latest Stable Version](https://poser.pugx.org/leafs/anchor/v/stable)](https://packagist.org/packages/leafs/anchor) [![Total Downloads](https://poser.pugx.org/leafs/anchor/downloads)](https://packagist.org/packages/leafs/anchor) | Basic security tools | -| [mvc-core] | [![Latest Stable Version](https://poser.pugx.org/leafs/mvc-core/v/stable)](https://packagist.org/packages/leafs/mvc-core) [![Total Downloads](https://poser.pugx.org/leafs/mvc-core/downloads)](https://packagist.org/packages/leafs/mvc-core) | Core MVC tools powering our MVC wrappers | -| [aloe] | [![Latest Stable Version](https://poser.pugx.org/leafs/aloe/v/stable)](https://packagist.org/packages/leafs/aloe) [![Total Downloads](https://poser.pugx.org/leafs/aloe/downloads)](https://packagist.org/packages/leafs/aloe) | Overpowered cli for our MVC wrappers | -| [fetch] | [![Latest Stable Version](https://poser.pugx.org/leafs/fetch/v/stable)](https://packagist.org/packages/leafs/fetch) [![Total Downloads](https://poser.pugx.org/leafs/fetch/downloads)](https://packagist.org/packages/leafs/fetch) | HTTP requests made simple | -| [redis] | [![Latest Stable Version](https://poser.pugx.org/leafs/redis/v/stable)](https://packagist.org/packages/leafs/redis) [![Total Downloads](https://poser.pugx.org/leafs/redis/downloads)](https://packagist.org/packages/leafs/redis) | Redis module | +| [aloe](https://github.com/leafsphp/aloe) | [![Latest Stable Version](https://poser.pugx.org/leafs/aloe/v/stable)](https://packagist.org/packages/leafs/aloe) [![Total Downloads](https://poser.pugx.org/leafs/aloe/downloads)](https://packagist.org/packages/leafs/aloe) | Smart console helper for leaf mvc, leaf api and skeleton | +| [router](https://github.com/leafsphp/router) | [![Latest Stable Version](https://poser.pugx.org/leafs/router/v/stable)](https://packagist.org/packages/leafs/router) [![Total Downloads](https://poser.pugx.org/leafs/router/downloads)](https://packagist.org/packages/leafs/router) | Default router for leaf php | +| [experiments](https://github.com/leafsphp/experimental-modules) | [![Latest Stable Version](https://poser.pugx.org/leafs/experimental/v/stable)](https://packagist.org/packages/leafs/experimental) [![Total Downloads](https://poser.pugx.org/leafs/experimental/downloads)](https://packagist.org/packages/leafs/experimental) | collection of experimental modules | +| [mail](https://github.com/leafsphp/mail) | [![Latest Stable Version](https://poser.pugx.org/leafs/mail/v/stable)](https://packagist.org/packages/leafs/mail) [![Total Downloads](https://poser.pugx.org/leafs/mail/downloads)](https://packagist.org/packages/leafs/mail) | Mailing made easy with leaf | +| [auth](https://github.com/leafsphp/auth) | [![Latest Stable Version](https://poser.pugx.org/leafs/auth/v/stable)](https://packagist.org/packages/leafs/auth) [![Total Downloads](https://poser.pugx.org/leafs/auth/downloads)](https://packagist.org/packages/leafs/auth) | Simple but powerful authentication system for your apps | +| [form](https://github.com/leafsphp/form) | [![Latest Stable Version](https://poser.pugx.org/leafs/form/v/stable)](https://packagist.org/packages/leafs/form) [![Total Downloads](https://poser.pugx.org/leafs/form/downloads)](https://packagist.org/packages/leafs/form) | Form processes and validation | +| [password](https://github.com/leafsphp/password) | [![Latest Stable Version](https://poser.pugx.org/leafs/password/v/stable)](https://packagist.org/packages/leafs/password) [![Total Downloads](https://poser.pugx.org/leafs/password/downloads)](https://packagist.org/packages/leafs/password) | Password encryption/validation/hashing in one box | +| [db-old](https://github.com/leafsphp/db-old) | [![Latest Stable Version](https://poser.pugx.org/leafs/db-old/v/stable)](https://packagist.org/packages/leafs/db-old) [![Total Downloads](https://poser.pugx.org/leafs/db-old/downloads)](https://packagist.org/packages/leafs/db-old) | Leaf Db from v1 (still maintained) | +| [db](https://github.com/leafsphp/db) | [![Latest Stable Version](https://poser.pugx.org/leafs/db/v/stable)](https://packagist.org/packages/leafs/db) [![Total Downloads](https://poser.pugx.org/leafs/db/downloads)](https://packagist.org/packages/leafs/db) | Leaf Db from v2 (actively maintained) | +| [session](https://github.com/leafsphp/session) | [![Latest Stable Version](https://poser.pugx.org/leafs/session/v/stable)](https://packagist.org/packages/leafs/session) [![Total Downloads](https://poser.pugx.org/leafs/session/downloads)](https://packagist.org/packages/leafs/session) | PHP sessions made simple | +| [cookie](https://github.com/leafsphp/cookie) | [![Latest Stable Version](https://poser.pugx.org/leafs/cookie/v/stable)](https://packagist.org/packages/leafs/cookie) [![Total Downloads](https://poser.pugx.org/leafs/cookie/downloads)](https://packagist.org/packages/leafs/cookie) | Cookie management without the tears | +| [logger](https://github.com/leafsphp/logger) | [![Latest Stable Version](https://poser.pugx.org/leafs/logger/v/stable)](https://packagist.org/packages/leafs/logger) [![Total Downloads](https://poser.pugx.org/leafs/logger/downloads)](https://packagist.org/packages/leafs/logger) | leaf logger module | +| [fs](https://github.com/leafsphp/fs) | [![Latest Stable Version](https://poser.pugx.org/leafs/fs/v/stable)](https://packagist.org/packages/leafs/fs) [![Total Downloads](https://poser.pugx.org/leafs/fs/downloads)](https://packagist.org/packages/leafs/fs) | Awesome filesystem operations + file uploads | +| [date](https://github.com/leafsphp/date) | [![Latest Stable Version](https://poser.pugx.org/leafs/date/v/stable)](https://packagist.org/packages/leafs/date) [![Total Downloads](https://poser.pugx.org/leafs/date/downloads)](https://packagist.org/packages/leafs/date) | PHP dates for humans | +| [bareui](https://github.com/leafsphp/bareui) | [![Latest Stable Version](https://poser.pugx.org/leafs/bareui/v/stable)](https://packagist.org/packages/leafs/bareui) [![Total Downloads](https://poser.pugx.org/leafs/bareui/downloads)](https://packagist.org/packages/leafs/bareui) | Dead simple templating engine with no compilation (blazing speed) | +| [blade](https://github.com/leafsphp/blade) | [![Latest Stable Version](https://poser.pugx.org/leafs/blade/v/stable)](https://packagist.org/packages/leafs/blade) [![Total Downloads](https://poser.pugx.org/leafs/blade/downloads)](https://packagist.org/packages/leafs/blade) | Laravel blade templating port for leaf | +| [veins](https://github.com/leafsphp/veins) | [![Latest Stable Version](https://poser.pugx.org/leafs/veins/v/stable)](https://packagist.org/packages/leafs/veins) [![Total Downloads](https://poser.pugx.org/leafs/veins/downloads)](https://packagist.org/packages/leafs/veins) | Leaf veins templating engine | +| [http](https://github.com/leafsphp/http) | [![Latest Stable Version](https://poser.pugx.org/leafs/http/v/stable)](https://packagist.org/packages/leafs/http) [![Total Downloads](https://poser.pugx.org/leafs/http/downloads)](https://packagist.org/packages/leafs/http) | Http operations made simple (request, response, ...) | +| [anchor](https://github.com/leafsphp/anchor) | [![Latest Stable Version](https://poser.pugx.org/leafs/anchor/v/stable)](https://packagist.org/packages/leafs/anchor) [![Total Downloads](https://poser.pugx.org/leafs/anchor/downloads)](https://packagist.org/packages/leafs/anchor) | Basic security tools | +| [mvc-core](https://github.com/leafsphp/mvc-core) | [![Latest Stable Version](https://poser.pugx.org/leafs/mvc-core/v/stable)](https://packagist.org/packages/leafs/mvc-core) [![Total Downloads](https://poser.pugx.org/leafs/mvc-core/downloads)](https://packagist.org/packages/leafs/mvc-core) | Core MVC tools powering our MVC wrappers | +| [aloe](https://github.com/leafsphp/aloe) | [![Latest Stable Version](https://poser.pugx.org/leafs/aloe/v/stable)](https://packagist.org/packages/leafs/aloe) [![Total Downloads](https://poser.pugx.org/leafs/aloe/downloads)](https://packagist.org/packages/leafs/aloe) | Overpowered cli for our MVC wrappers | +| [fetch](https://github.com/leafsphp/fetch) | [![Latest Stable Version](https://poser.pugx.org/leafs/fetch/v/stable)](https://packagist.org/packages/leafs/fetch) [![Total Downloads](https://poser.pugx.org/leafs/fetch/downloads)](https://packagist.org/packages/leafs/fetch) | HTTP requests made simple | +| [redis](https://github.com/leafsphp/redis) | [![Latest Stable Version](https://poser.pugx.org/leafs/redis/v/stable)](https://packagist.org/packages/leafs/redis) [![Total Downloads](https://poser.pugx.org/leafs/redis/downloads)](https://packagist.org/packages/leafs/redis) | Redis module | ## 💬 Stay In Touch diff --git a/composer.json b/composer.json index da13f49..acbcc1d 100644 --- a/composer.json +++ b/composer.json @@ -31,9 +31,10 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "leafs/http": "^1.1", - "leafs/router": "^0.1.4", - "leafs/anchor": "^1.1" + "leafs/http": "^1.2", + "leafs/router": "^0.1.5", + "leafs/anchor": "^1.2", + "leafs/exception": "^3.0" }, "require-dev": { "pestphp/pest": "^1.21", @@ -42,5 +43,10 @@ "scripts": { "format": "vendor/bin/php-cs-fixer fix --config=.php_cs.dist.php --allow-risky=yes", "test": "vendor/bin/pest" - } -} \ No newline at end of file + }, + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } + } +} diff --git a/src/App.php b/src/App.php index fd60e1a..fe43189 100755 --- a/src/App.php +++ b/src/App.php @@ -10,7 +10,7 @@ * The easiest way to build simple but powerful apps and APIs quickly. * * @author Michael Darko - * @copyright 2019-2021 Michael Darko + * @copyright 2019-2022 Michael Darko * @link https://leafphp.dev * @license MIT * @package Leaf @@ -70,16 +70,17 @@ public function __construct(array $userSettings = []) protected function setupErrorHandler() { - if ($this->config('debug')) { - $debugConfig = [E_ALL, 1, ['\Leaf\Exception\General', 'handleErrors'], false]; + if ($this->config('debug') === true) { + $debugConfig = [E_ALL, 1]; + $this->errorHandler = (new \Leaf\Exception\Run()); + $this->errorHandler->register(); } else { - $debugConfig = [0, 0, ['\Leaf\Exception\General', 'defaultError'], true]; + $debugConfig = [0, 0]; + $this->setErrorHandler(['\Leaf\Exception\General', 'defaultError'], true); } error_reporting($debugConfig[0]); ini_set('display_errors', (string) $debugConfig[1]); - - $this->setErrorHandler($debugConfig[2], $debugConfig[3]); } /** @@ -91,6 +92,15 @@ public function setErrorHandler($handler, bool $wrapper = true) { $errorHandler = $handler; + if ($this->errorHandler instanceof \Leaf\Exception\Run) { + $this->errorHandler->unregister(); + } + + if ($handler instanceof \Leaf\Exception\Handler\Handler) { + $this->errorHandler = new \Leaf\Exception\Run(); + $this->errorHandler->pushHandler($handler)->register(); + } + if ($wrapper) { $errorHandler = function ($errno, $errstr = '', $errfile = '', $errline = '') use ($handler) { $exception = Exception\General::toException($errno, $errstr, $errfile, $errline); @@ -241,6 +251,7 @@ public function config($name, $value = null) } Config::set($name, $value); + $this->setupErrorHandler(); } /******************************************************************************** @@ -361,35 +372,6 @@ public static function halt($status, $message = '') exit(); } - /** - * Stop - * - * The thrown exception will be caught in application's `call()` method - * and the response will be sent as is to the HTTP client. - * - * @throws \Leaf\Exception\Stop - */ - public function stop() - { - throw new \Leaf\Exception\Stop(); - } - - /** - * Pass - * - * The thrown exception is caught in the application's `call()` method causing - * the router's current iteration to stop and continue to the subsequent route if available. - * If no subsequent matching routes are found, a 404 response will be sent to the client. - * - * @throws \Leaf\Exception\Pass - */ - public function pass() - { - $this->cleanBuffer(); - - throw new \Leaf\Exception\Pass(); - } - /** * Evade CORS errors * diff --git a/src/Exception/General.php b/src/Exception/General.php deleted file mode 100755 index 082cd61..0000000 --- a/src/Exception/General.php +++ /dev/null @@ -1,232 +0,0 @@ -response = new Response(); - $this->handleException($throwable); - } - - /** - * Configure exception handler - */ - public function configure($config) - { - $configuration = array_merge($this->config, $config); - $this->config = $configuration; - } - - /** - * Handles an exception - */ - protected function handleException($throwable) - { - $this->response->throwErr($throwable); - } - - /** - * Convert errors into ErrorException objects - * - * This method catches PHP errors and converts them into \ErrorException objects; - * these \ErrorException objects are then thrown and caught by Leaf's - * built-in or custom error handlers. - * - * @param int $errno The numeric type of the Error - * @param string $errstr The error message - * @param string $errfile The absolute path to the affected file - * @param int $errline The line number of the error in the affected file - * @return bool - * @throws \ErrorException - */ - public static function handleErrors($errno, $errstr = '', $errfile = '', $errline = '') - { - if (!($errno & error_reporting())) { - return; - } - - try { - throw new \ErrorException($errstr, $errno, 0, $errfile, $errline); - } catch (\Throwable $th) { - $app = \Leaf\Config::get("app")["instance"]; - - if ($app && $app->config("log.enabled")) { - $app->logger()->error($th); - } - - exit(static::renderBody($th)); - } - } - - /** - * Returns ErrorException objects from errors - * - * This method catches PHP errors and converts them into \ErrorException objects; - * these \ErrorException objects are then thrown and caught by Leaf's - * built-in or custom error handlers. - * - * @param int $errno The numeric type of the Error - * @param string $errstr The error message - * @param string $errfile The absolute path to the affected file - * @param int $errline The line number of the error in the affected file - * @return void|\ErrorException - */ - public static function toException($errno, $errstr = '', $errfile = '', $errline = '') - { - if (!($errno & error_reporting())) { - return; - } - - try { - throw new \ErrorException($errstr, $errno, 0, $errfile, $errline); - } catch (\Throwable $th) { - return $th; - } - } - - /** - * Render response body - * - * @param array $env - * @param \Exception $exception - * - * @return string - */ - protected static function renderBody($exception) - { - $title = static::$config['ERROR_TITLE'] ?? 'Leaf Application Error'; - $code = $exception->getCode(); - $message = htmlspecialchars($exception->getMessage()); - $file = $exception->getFile(); - $line = $exception->getLine(); - - $trace = str_replace( - ['#', "\n"], - ['
#', '
'], - htmlspecialchars($exception->getTraceAsString()) - ); - $trace = str_replace(['): ', ''], ['): ', ''], $trace); - $body = "

$title

"; - $body .= '

The application could not run because of the following error:

'; - $body .= '

Details

'; - $body .= sprintf('
Type: %s
', get_class($exception)); - - if ($code) { - $body .= "
Code: $code
"; - } - - if ($message) { - $body .= "
Message: $message
"; - } - - if ($file) { - $body .= "
File: $file
"; - } - - if ($line) { - $body .= "
Line: $line
"; - } - - if ($trace) { - $body .= '

Trace

'; - $body .= "
$trace
"; - } - - return static::exceptionMarkup($title, $body); - } - - /** - * Generate diagnostic template markup - * - * This method accepts a title and body content to generate an HTML document layout. - * - * @param string $title The title of the HTML template - * @param string $body The body content of the HTML template - * @return string - */ - protected static function errorMarkup($title, $body) - { - return "$title

$title

$body
"; - } - - /** - * Generate diagnostic template markup - * - * This method accepts a title and body content to generate an HTML document layout. - * - * @param string $title The title of the HTML template - * @param string $body The body content of the HTML template - * @return string - */ - protected static function exceptionMarkup($title, $body) - { - return "$title$body"; - } - - /** - * Default Not Found handler - */ - public static function defaultDown() - { - echo static::errorMarkup( - 'Oops!', - '

App is under maintainance, please check back soon.

' - ); - } - - /** - * Default Not Found handler - */ - public static function default404() - { - echo static::errorMarkup( - '404', - '

The page you are looking for could not be found.

' - ); - } - - /** - * CSRF error - */ - public static function csrf($error = null) - { - echo static::errorMarkup( - 'Invalid request', - "

$error

" ?? '

The page you are looking for has expired.

' - ); - } - - /** - * Default Error handler - */ - public static function defaultError($e = null) - { - if ($e) { - $app = \Leaf\Config::get("app")["instance"]; - - if ($app && $app->config("log.enabled")) { - $app->logger()->error($e); - } - } - - echo self::errorMarkup('Oops!', '

A website error has occurred, our team has been notified.

'); - } -} diff --git a/src/Exception/Pass.php b/src/Exception/Pass.php deleted file mode 100755 index 3c5f4c6..0000000 --- a/src/Exception/Pass.php +++ /dev/null @@ -1,21 +0,0 @@ -config('app.down', false); - $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['REQUEST_URI'] = '/'; - app()->config('mode', 'TEST'); - app()->get('/', function () { - }); - app()->script('TEST', function () { - app()->config('app.down', true); - }); - - app()->run(); - - expect(app()->config('mode'))->toBe('TEST'); - expect(app()->config('app.down'))->toBe(true); -}); + app()->setBasePath('/'); -test('set error handler', function () { - app()->config('app.down', false); - - $_SERVER['REQUEST_METHOD'] = 'GET'; - $_SERVER['REQUEST_URI'] = '/'; + app()->config('test', false); + app()->config('mode', 'TEST'); - // create an error to trigger error handler - app()->get('/', function () { - $app; + app()->set404(function () { }); - app()->setErrorHandler(function () { - app()->config('app.down', true); + app()->script('TEST', function () { + app()->config('test', true); }); app()->run(); - expect(app()->config('app.down'))->toBe(true); + expect(app()->config('mode'))->toBe('TEST'); + expect(app()->config('test'))->toBe(true); }); test('set 404', function () { app()->config('app.down', false); - $_SERVER['REQUEST_METHOD'] = 'GET'; - $_SERVER['REQUEST_URI'] = '/'; + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_SERVER['REQUEST_URI'] = '/home'; app()->set404(function () { app()->config('app.down', true);