[ErrorHandler] Allow override of the default non-debug template
This commit is contained in:
parent
e6e1ca38c0
commit
6e1d16b44d
@ -1,6 +1,11 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
5.2.0
|
||||
-----
|
||||
|
||||
* added the ability to set `HtmlErrorRenderer::$template` to a custom template to render when not in debug mode.
|
||||
|
||||
5.1.0
|
||||
-----
|
||||
|
||||
|
@ -39,6 +39,8 @@ class HtmlErrorRenderer implements ErrorRendererInterface
|
||||
private $outputBuffer;
|
||||
private $logger;
|
||||
|
||||
private static $template = 'views/error.html.php';
|
||||
|
||||
/**
|
||||
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
|
||||
* @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
|
||||
@ -134,7 +136,7 @@ class HtmlErrorRenderer implements ErrorRendererInterface
|
||||
$statusCode = $this->escape($exception->getStatusCode());
|
||||
|
||||
if (!$debug) {
|
||||
return $this->include('views/error.html.php', [
|
||||
return $this->include(self::$template, [
|
||||
'statusText' => $statusText,
|
||||
'statusCode' => $statusCode,
|
||||
]);
|
||||
@ -347,8 +349,19 @@ class HtmlErrorRenderer implements ErrorRendererInterface
|
||||
{
|
||||
extract($context, EXTR_SKIP);
|
||||
ob_start();
|
||||
include __DIR__.'/../Resources/'.$name;
|
||||
|
||||
include file_exists($name) ? $name : __DIR__.'/../Resources/'.$name;
|
||||
|
||||
return trim(ob_get_clean());
|
||||
}
|
||||
|
||||
/**
|
||||
* Allows overriding the default non-debug template.
|
||||
*
|
||||
* @param string $template path to the custom template file to render
|
||||
*/
|
||||
public static function setTemplate(string $template): void
|
||||
{
|
||||
self::$template = $template;
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,9 @@ Debug::enable();
|
||||
//ErrorHandler::register();
|
||||
//DebugClassLoader::enable();
|
||||
|
||||
// If you want a custom generic template when debug is not enabled
|
||||
// HtmlErrorRenderer::setTemplate('/path/to/custom/error.html.php');
|
||||
|
||||
$data = ErrorHandler::call(static function () use ($filename, $datetimeFormat) {
|
||||
// if any code executed inside this anonymous function fails, a PHP exception
|
||||
// will be thrown, even if the code uses the '@' PHP silence operator
|
||||
|
Reference in New Issue
Block a user