addRoutingMiddleware(); self::$app->add(new ThrottleMiddleware()); /** * Add Error Handling Middleware * * @param bool $displayErrorDetails -> Should be set to false in production * @param bool $logErrors -> Parameter is passed to the default ErrorHandler * @param bool $logErrorDetails -> Display error details in error log * which can be replaced by a callable of your choice. * Note: This middleware should be added last. It will not handle any exceptions/errors * for middleware added after it. */ $errorMiddleware = self::$app->addErrorMiddleware( env('displayError', false), env('logErrors', false), env('logErrorDetails', false) ); $errorHandler = $errorMiddleware->getDefaultErrorHandler(); $errorHandler->registerErrorRenderer('text/html', CustomHtmlErrorRenderer::class); } private static function loadEnv() { try { self::$env = Dotenv::createImmutable(__DIR__ . '/..')->load(); } catch (InvalidPathException $e) { // no env file present } } public static function app() { return self::$app; } public static function env(string $key, $default = null) { $value = self::$env[$key] ?? $default; if ($value === false) { return $default; } switch (strtolower($value)) { case '(true)': $value = true; break; case 'false': case '(false)': $value = false; break; case 'empty': case '(empty)': $value = ''; break; case 'null': case '(null)': $value = null; break; } return $value; } public static function run() { self::build(); self::$app->run(); } }