From 955004b80a9be2f869ef0fecc1f5c3307739c232 Mon Sep 17 00:00:00 2001 From: Ben Yan Date: Sun, 29 Jul 2018 00:57:59 +0800 Subject: [PATCH 1/2] Added custom layout support in ErrorResponseGenerator --- src/Container/ErrorResponseGeneratorFactory.php | 5 ++++- src/Middleware/ErrorResponseGenerator.php | 6 +++++- src/Response/ErrorResponseGeneratorTrait.php | 7 +++++++ test/Container/ErrorResponseGeneratorFactoryTest.php | 5 +++++ test/Middleware/ErrorResponseGeneratorTest.php | 2 ++ 5 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/Container/ErrorResponseGeneratorFactory.php b/src/Container/ErrorResponseGeneratorFactory.php index 8ac93e49..64b17e43 100644 --- a/src/Container/ErrorResponseGeneratorFactory.php +++ b/src/Container/ErrorResponseGeneratorFactory.php @@ -24,10 +24,13 @@ public function __invoke(ContainerInterface $container) : ErrorResponseGenerator $template = $config['zend-expressive']['error_handler']['template_error'] ?? ErrorResponseGenerator::TEMPLATE_DEFAULT; + $layout = $config['zend-expressive']['error_handler']['layout'] + ?? ErrorResponseGenerator::LAYOUT_DEFAULT; + $renderer = $container->has(TemplateRendererInterface::class) ? $container->get(TemplateRendererInterface::class) : null; - return new ErrorResponseGenerator($debug, $renderer, $template); + return new ErrorResponseGenerator($debug, $renderer, $template, $layout); } } diff --git a/src/Middleware/ErrorResponseGenerator.php b/src/Middleware/ErrorResponseGenerator.php index b14c66c4..d814880b 100644 --- a/src/Middleware/ErrorResponseGenerator.php +++ b/src/Middleware/ErrorResponseGenerator.php @@ -21,15 +21,18 @@ class ErrorResponseGenerator use ErrorResponseGeneratorTrait; public const TEMPLATE_DEFAULT = 'error::error'; + public const LAYOUT_DEFAULT = 'layout::default'; public function __construct( bool $isDevelopmentMode = false, TemplateRendererInterface $renderer = null, - string $template = self::TEMPLATE_DEFAULT + string $template = self::TEMPLATE_DEFAULT, + string $layout = self::LAYOUT_DEFAULT ) { $this->debug = $isDevelopmentMode; $this->renderer = $renderer; $this->template = $template; + $this->layout = $layout; } public function __invoke( @@ -49,6 +52,7 @@ public function __invoke( 'uri' => (string) $request->getUri(), 'status' => $response->getStatusCode(), 'reason' => $response->getReasonPhrase(), + 'layout' => $this->layout, ], $this->debug, $response diff --git a/src/Response/ErrorResponseGeneratorTrait.php b/src/Response/ErrorResponseGeneratorTrait.php index e1450702..9bb9b0e8 100644 --- a/src/Response/ErrorResponseGeneratorTrait.php +++ b/src/Response/ErrorResponseGeneratorTrait.php @@ -48,6 +48,13 @@ trait ErrorResponseGeneratorTrait */ private $template; + /** + * Name of the layout to render. + * + * @var string + */ + private $layout; + private function prepareTemplatedResponse( Throwable $e, TemplateRendererInterface $renderer, diff --git a/test/Container/ErrorResponseGeneratorFactoryTest.php b/test/Container/ErrorResponseGeneratorFactoryTest.php index 9872c615..0533884e 100644 --- a/test/Container/ErrorResponseGeneratorFactoryTest.php +++ b/test/Container/ErrorResponseGeneratorFactoryTest.php @@ -42,6 +42,7 @@ public function testNoConfigurationCreatesInstanceWithDefaults() $this->assertAttributeEquals(false, 'debug', $generator); $this->assertAttributeEmpty('renderer', $generator); $this->assertAttributeEquals('error::error', 'template', $generator); + $this->assertAttributeEquals('layout::default', 'layout', $generator); } public function testUsesDebugConfigurationToSetDebugFlag() @@ -56,6 +57,7 @@ public function testUsesDebugConfigurationToSetDebugFlag() $this->assertAttributeEquals(true, 'debug', $generator); $this->assertAttributeEmpty('renderer', $generator); $this->assertAttributeEquals('error::error', 'template', $generator); + $this->assertAttributeEquals('layout::default', 'layout', $generator); } public function testUsesConfiguredTemplateRenderToSetGeneratorRenderer() @@ -70,6 +72,7 @@ public function testUsesConfiguredTemplateRenderToSetGeneratorRenderer() $this->assertAttributeEquals(false, 'debug', $generator); $this->assertAttributeSame($this->renderer->reveal(), 'renderer', $generator); $this->assertAttributeEquals('error::error', 'template', $generator); + $this->assertAttributeEquals('layout::default', 'layout', $generator); } public function testUsesTemplateConfigurationToSetTemplate() @@ -79,6 +82,7 @@ public function testUsesTemplateConfigurationToSetTemplate() 'zend-expressive' => [ 'error_handler' => [ 'template_error' => 'error::custom', + 'layout' => 'layout::custom', ], ], ]); @@ -90,5 +94,6 @@ public function testUsesTemplateConfigurationToSetTemplate() $this->assertAttributeEquals(false, 'debug', $generator); $this->assertAttributeEmpty('renderer', $generator); $this->assertAttributeEquals('error::custom', 'template', $generator); + $this->assertAttributeEquals('layout::custom', 'layout', $generator); } } diff --git a/test/Middleware/ErrorResponseGeneratorTest.php b/test/Middleware/ErrorResponseGeneratorTest.php index eb8381a4..4f8e7ba9 100644 --- a/test/Middleware/ErrorResponseGeneratorTest.php +++ b/test/Middleware/ErrorResponseGeneratorTest.php @@ -135,6 +135,7 @@ public function testRendersTemplateWithoutErrorDetailsWhenRendererPresentAndNotI 'uri' => 'https://example.com/foo', 'status' => StatusCode::STATUS_INTERNAL_SERVER_ERROR, 'reason' => 'Internal Server Error', + 'layout' => 'layout::default', ]) ->willReturn('TEMPLATED CONTENTS'); @@ -198,6 +199,7 @@ public function testRendersTemplateWithErrorDetailsWhenRendererPresentAndInDebug 'status' => StatusCode::STATUS_INTERNAL_SERVER_ERROR, 'reason' => 'Network Connect Timeout Error', 'error' => $error, + 'layout' => 'layout::default', ]) ->willReturn('TEMPLATED CONTENTS'); From 5baf0e1f34c482794d331c64e91ea0bcf28277a6 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Mon, 30 Jul 2018 16:09:28 -0500 Subject: [PATCH 2/2] Adds documentation for #629 Documents the new `$layout` argument, configuration options, and defaults for the `ErrorResponseGenerator`. Additionally, provides the default values for template and layout-related constants defined by both the `NotFoundHandler` and `ErrorResponseGenerator`. --- docs/book/v3/features/container/factories.md | 16 ++++++++++++++-- docs/book/v3/features/error-handling.md | 3 +++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/book/v3/features/container/factories.md b/docs/book/v3/features/container/factories.md index 085409f3..eec19126 100644 --- a/docs/book/v3/features/container/factories.md +++ b/docs/book/v3/features/container/factories.md @@ -81,7 +81,11 @@ When the `config` service is present, the factory can utilize two values: - `zend-expressive.error_handler.template_error`, a name of an alternate template to use (instead of the default represented in the `Zend\Expressive\Middleware\ErrorResponseGenerator::TEMPLATE_DEFAULT` - constant). + constant, which evaluates to `error::error`). +- **Since 3.1.0**: `zend-expressive.error_handler.layout`, a name of an + alternate layout to use (instead of the default represented in the + `Zend\Expressive\Middleware\ErrorResponseGenerator::LAYOUT_DEFAULT` constant, + which evaluates to `layout::default`). As an example: @@ -90,6 +94,7 @@ As an example: 'zend-expressive' => [ 'error_handler' => [ 'template_error' => 'name of error template', + 'layout' => 'layout::alternate', ], ], ``` @@ -150,7 +155,13 @@ When the `config` service is present, the factory can utilize two values: - `zend-expressive.error_handler.template_404`, a name of an alternate template to use (instead of the default represented in the - `Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant). + `Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant, which + evaluates to `error::404`). + +- `zend-expressive.error_handler.layout`, a name of an alternate + template to use (instead of the default represented in the + `Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant, which + evaluates to `layout::default`). As an example: @@ -158,6 +169,7 @@ As an example: 'zend-expressive' => [ 'error_handler' => [ 'template_404' => 'name of 404 template', + 'layout' => 'layout::alternate', ], ], ``` diff --git a/docs/book/v3/features/error-handling.md b/docs/book/v3/features/error-handling.md index 4df39715..0b58c8c6 100644 --- a/docs/book/v3/features/error-handling.md +++ b/docs/book/v3/features/error-handling.md @@ -71,6 +71,9 @@ We provide two error response generators for you: otherwise, a plain text response is generated that notes the request method and URI. + Since version 3.1.0, it also accepts a layout name, if you want to use one + other than `layout::default`. + - `Zend\Expressive\Middleware\WhoopsErrorResponseGenerator`, which uses [whoops](http://filp.github.io/whoops/) to present detailed exception and request information; this implementation is intended for development