Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add separate parameters for HtmlRenderer settings #137

Merged
merged 1 commit into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

- Enh #125: Add error code & show function arguments (@xepozz)
- Enh #130: Pass exception message instead of rendered exception to logger in `ErrorHandler` (@olegbaturin)
- Enh #133: Extract response generator from `ErrorCatcher` middleware into separate `ThrowableResponseFactory` class (@olegbaturin)
- Enh #133: Extract response generator from `ErrorCatcher` middleware into separate `ThrowableResponseFactory`
class (@olegbaturin)
- Chg #137: Add separate parameters for each of `HtmlRenderer` settings in constructor. Mark `$settings` parameter as
deprecated (@vjik)

## 3.3.0 July 11, 2024

Expand Down
41 changes: 32 additions & 9 deletions src/Renderer/HtmlRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,19 @@ final class HtmlRenderer implements ThrowableRendererInterface
private ?array $vendorPaths = null;

/**
* @param array $settings Settings can have the following keys:
* @param array $settings (deprecated) Settings can have the following keys:
* - template: string, full path of the template file for rendering exceptions without call stack information.
* - verboseTemplate: string, full path of the template file for rendering exceptions with call stack information.
* - maxSourceLines: int, maximum number of source code lines to be displayed. Defaults to 19.
* - maxTraceLines: int, maximum number of trace source code lines to be displayed. Defaults to 13.
* - traceHeaderLine: string, trace header line with placeholders to be be substituted. Defaults to null.
* - traceHeaderLine: string, trace header line with placeholders to be substituted. Defaults to null.
* @param string|null $template The full path of the template file for rendering exceptions without call stack
* information.
* @param string|null $verboseTemplate The full path of the template file for rendering exceptions with call stack
* information.
* @param int|null $maxSourceLines The maximum number of source code lines to be displayed. Defaults to 19.
* @param int|null $maxTraceLines The maximum number of trace source code lines to be displayed. Defaults to 13.
* @param string|null $traceHeaderLine The trace header line with placeholders to be substituted. Defaults to null.
*
* @psalm-param array{
* template?: string,
Expand All @@ -120,17 +127,33 @@ final class HtmlRenderer implements ThrowableRendererInterface
* traceHeaderLine?: string,
* } $settings
*/
public function __construct(array $settings = [])
{
public function __construct(
array $settings = [],
?string $template = null,
?string $verboseTemplate = null,
?int $maxSourceLines = null,
?int $maxTraceLines = null,
?string $traceHeaderLine = null,
) {
$this->markdownParser = new GithubMarkdown();
$this->markdownParser->html5 = true;

$this->defaultTemplatePath = dirname(__DIR__, 2) . '/templates';
$this->template = $settings['template'] ?? $this->defaultTemplatePath . '/production.php';
$this->verboseTemplate = $settings['verboseTemplate'] ?? $this->defaultTemplatePath . '/development.php';
$this->maxSourceLines = $settings['maxSourceLines'] ?? 19;
$this->maxTraceLines = $settings['maxTraceLines'] ?? 13;
$this->traceHeaderLine = $settings['traceHeaderLine'] ?? null;
$this->template = $template
?? $settings['template']
?? $this->defaultTemplatePath . '/production.php';
$this->verboseTemplate = $verboseTemplate
?? $settings['verboseTemplate']
?? $this->defaultTemplatePath . '/development.php';
$this->maxSourceLines = $maxSourceLines
?? $settings['maxSourceLines']
?? 19;
$this->maxTraceLines = $maxTraceLines
?? $settings['maxTraceLines']
?? 13;
$this->traceHeaderLine = $traceHeaderLine
?? $settings['traceHeaderLine']
?? null;
}

public function render(Throwable $t, ServerRequestInterface $request = null): ErrorData
Expand Down
Loading