Skip to content

Commit 70ebf7e

Browse files
committed
feature #37492 [ErrorHandler] Allow override of the default non-debug template (PhilETaylor)
This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- [ErrorHandler] Allow override of the default non-debug template | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes - a very tiny one | Deprecations? | no | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Add new feature to allow the default hardcoded non-debug template to be overridden when `symfony/error-handler`used as a component in other projects. Currently, when used as a component, its not possible (that I could see) to override the standard default template of `views/error.html.php` therefore the look and feel, and text is hard coded. There is much written on how to customise the error pages when using the full symfony stack, but not when using individual `symfony/error-handler` component on its own. This PR is related to the use of `symfony/error-handler` as a component in other projects - specifically the Joomla Content Management System, where, in non debug mode, a generic template **needs to be translatable, and customisable** even though most of the time when the error occurs will bt at boot time and not much available to it. joomla/joomla-cms#29968 This PR allows the use of code such as ```php <?php require 'vendor/autoload.php'; use Symfony\Component\ErrorHandler\ErrorHandler; use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; ErrorHandler::register(); HtmlErrorRenderer::setTemplate('/path/to/custom/mine.php'); throw new Exception('myException is this', 500); ``` and then for further customisation of the "look and feel" to be done in `mine.php` Due to the unique way the error handler component is init'ed, the use of static calls has been employed rather than any DI approach, I hope this is ok. Commits ------- 6e1d16b [ErrorHandler] Allow override of the default non-debug template
2 parents 104a8f8 + 6e1d16b commit 70ebf7e

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

src/Symfony/Component/ErrorHandler/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* added the ability to set `HtmlErrorRenderer::$template` to a custom template to render when not in debug mode.
8+
49
5.1.0
510
-----
611

src/Symfony/Component/ErrorHandler/ErrorRenderer/HtmlErrorRenderer.php

+15-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class HtmlErrorRenderer implements ErrorRendererInterface
3939
private $outputBuffer;
4040
private $logger;
4141

42+
private static $template = 'views/error.html.php';
43+
4244
/**
4345
* @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
4446
* @param bool|callable $outputBuffer The output buffer as a string or a callable that should return it
@@ -134,7 +136,7 @@ private function renderException(FlattenException $exception, string $debugTempl
134136
$statusCode = $this->escape($exception->getStatusCode());
135137

136138
if (!$debug) {
137-
return $this->include('views/error.html.php', [
139+
return $this->include(self::$template, [
138140
'statusText' => $statusText,
139141
'statusCode' => $statusCode,
140142
]);
@@ -347,8 +349,19 @@ private function include(string $name, array $context = []): string
347349
{
348350
extract($context, EXTR_SKIP);
349351
ob_start();
350-
include __DIR__.'/../Resources/'.$name;
352+
353+
include file_exists($name) ? $name : __DIR__.'/../Resources/'.$name;
351354

352355
return trim(ob_get_clean());
353356
}
357+
358+
/**
359+
* Allows overriding the default non-debug template.
360+
*
361+
* @param string $template path to the custom template file to render
362+
*/
363+
public static function setTemplate(string $template): void
364+
{
365+
self::$template = $template;
366+
}
354367
}

src/Symfony/Component/ErrorHandler/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Debug::enable();
2121
//ErrorHandler::register();
2222
//DebugClassLoader::enable();
2323

24+
// If you want a custom generic template when debug is not enabled
25+
// HtmlErrorRenderer::setTemplate('/path/to/custom/error.html.php');
26+
2427
$data = ErrorHandler::call(static function () use ($filename, $datetimeFormat) {
2528
// if any code executed inside this anonymous function fails, a PHP exception
2629
// will be thrown, even if the code uses the '@' PHP silence operator

0 commit comments

Comments
 (0)