Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 3995008

Browse files
committed
Merge branch 'feature/629' into develop
Close #629
2 parents e624529 + 10efc9c commit 3995008

File tree

8 files changed

+49
-6
lines changed

8 files changed

+49
-6
lines changed

CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 3.1.0 - TBD
5+
## 3.1.0 - 2018-07-30
66

77
### Added
88

99
- Nothing.
1010

1111
### Changed
1212

13-
- Nothing.
13+
- [#629](https://github.com/zendframework/zend-expressive/pull/629) changes the constructor of `Zend\Expressive\Middleware\ErrorResponseGenerator`
14+
to accept an additional, optional argument, `$layout`, which defaults to a new
15+
constant value, `ErrorResponseGenerator::LAYOUT_DEFAULT`, or `layout::default`.
16+
`Zend\Expressive\Container\ErrorResponseGeneratorFactory` now also looks for
17+
the configuration value `zend-expressive.error_handler.layout`, and will use
18+
that value to seed the constructor argument. This change makes the
19+
`ErrorResponseGenerator` mirror the `NotFoundHandler`, allowing for a
20+
consistent layout between the two error pages.
1421

1522
### Deprecated
1623

docs/book/v3/features/container/factories.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ When the `config` service is present, the factory can utilize two values:
8181
- `zend-expressive.error_handler.template_error`, a name of an alternate
8282
template to use (instead of the default represented in the
8383
`Zend\Expressive\Middleware\ErrorResponseGenerator::TEMPLATE_DEFAULT`
84-
constant).
84+
constant, which evaluates to `error::error`).
85+
- **Since 3.1.0**: `zend-expressive.error_handler.layout`, a name of an
86+
alternate layout to use (instead of the default represented in the
87+
`Zend\Expressive\Middleware\ErrorResponseGenerator::LAYOUT_DEFAULT` constant,
88+
which evaluates to `layout::default`).
8589

8690
As an example:
8791

@@ -90,6 +94,7 @@ As an example:
9094
'zend-expressive' => [
9195
'error_handler' => [
9296
'template_error' => 'name of error template',
97+
'layout' => 'layout::alternate',
9398
],
9499
],
95100
```
@@ -150,14 +155,21 @@ When the `config` service is present, the factory can utilize two values:
150155

151156
- `zend-expressive.error_handler.template_404`, a name of an alternate
152157
template to use (instead of the default represented in the
153-
`Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant).
158+
`Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant, which
159+
evaluates to `error::404`).
160+
161+
- `zend-expressive.error_handler.layout`, a name of an alternate
162+
template to use (instead of the default represented in the
163+
`Zend\Expressive\Delegate\NotFoundDelegate::TEMPLATE_DEFAULT` constant, which
164+
evaluates to `layout::default`).
154165

155166
As an example:
156167

157168
```php
158169
'zend-expressive' => [
159170
'error_handler' => [
160171
'template_404' => 'name of 404 template',
172+
'layout' => 'layout::alternate',
161173
],
162174
],
163175
```

docs/book/v3/features/error-handling.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ We provide two error response generators for you:
7171
otherwise, a plain text response is generated that notes the request method
7272
and URI.
7373

74+
Since version 3.1.0, it also accepts a layout name, if you want to use one
75+
other than `layout::default`.
76+
7477
- `Zend\Expressive\Middleware\WhoopsErrorResponseGenerator`, which uses
7578
[whoops](http://filp.github.io/whoops/) to present detailed exception
7679
and request information; this implementation is intended for development

src/Container/ErrorResponseGeneratorFactory.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ public function __invoke(ContainerInterface $container) : ErrorResponseGenerator
2424
$template = $config['zend-expressive']['error_handler']['template_error']
2525
?? ErrorResponseGenerator::TEMPLATE_DEFAULT;
2626

27+
$layout = $config['zend-expressive']['error_handler']['layout']
28+
?? ErrorResponseGenerator::LAYOUT_DEFAULT;
29+
2730
$renderer = $container->has(TemplateRendererInterface::class)
2831
? $container->get(TemplateRendererInterface::class)
2932
: null;
3033

31-
return new ErrorResponseGenerator($debug, $renderer, $template);
34+
return new ErrorResponseGenerator($debug, $renderer, $template, $layout);
3235
}
3336
}

src/Middleware/ErrorResponseGenerator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ class ErrorResponseGenerator
2121
use ErrorResponseGeneratorTrait;
2222

2323
public const TEMPLATE_DEFAULT = 'error::error';
24+
public const LAYOUT_DEFAULT = 'layout::default';
2425

2526
public function __construct(
2627
bool $isDevelopmentMode = false,
2728
TemplateRendererInterface $renderer = null,
28-
string $template = self::TEMPLATE_DEFAULT
29+
string $template = self::TEMPLATE_DEFAULT,
30+
string $layout = self::LAYOUT_DEFAULT
2931
) {
3032
$this->debug = $isDevelopmentMode;
3133
$this->renderer = $renderer;
3234
$this->template = $template;
35+
$this->layout = $layout;
3336
}
3437

3538
public function __invoke(
@@ -49,6 +52,7 @@ public function __invoke(
4952
'uri' => (string) $request->getUri(),
5053
'status' => $response->getStatusCode(),
5154
'reason' => $response->getReasonPhrase(),
55+
'layout' => $this->layout,
5256
],
5357
$this->debug,
5458
$response

src/Response/ErrorResponseGeneratorTrait.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ trait ErrorResponseGeneratorTrait
4848
*/
4949
private $template;
5050

51+
/**
52+
* Name of the layout to render.
53+
*
54+
* @var string
55+
*/
56+
private $layout;
57+
5158
private function prepareTemplatedResponse(
5259
Throwable $e,
5360
TemplateRendererInterface $renderer,

test/Container/ErrorResponseGeneratorFactoryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function testNoConfigurationCreatesInstanceWithDefaults()
4242
$this->assertAttributeEquals(false, 'debug', $generator);
4343
$this->assertAttributeEmpty('renderer', $generator);
4444
$this->assertAttributeEquals('error::error', 'template', $generator);
45+
$this->assertAttributeEquals('layout::default', 'layout', $generator);
4546
}
4647

4748
public function testUsesDebugConfigurationToSetDebugFlag()
@@ -56,6 +57,7 @@ public function testUsesDebugConfigurationToSetDebugFlag()
5657
$this->assertAttributeEquals(true, 'debug', $generator);
5758
$this->assertAttributeEmpty('renderer', $generator);
5859
$this->assertAttributeEquals('error::error', 'template', $generator);
60+
$this->assertAttributeEquals('layout::default', 'layout', $generator);
5961
}
6062

6163
public function testUsesConfiguredTemplateRenderToSetGeneratorRenderer()
@@ -70,6 +72,7 @@ public function testUsesConfiguredTemplateRenderToSetGeneratorRenderer()
7072
$this->assertAttributeEquals(false, 'debug', $generator);
7173
$this->assertAttributeSame($this->renderer->reveal(), 'renderer', $generator);
7274
$this->assertAttributeEquals('error::error', 'template', $generator);
75+
$this->assertAttributeEquals('layout::default', 'layout', $generator);
7376
}
7477

7578
public function testUsesTemplateConfigurationToSetTemplate()
@@ -79,6 +82,7 @@ public function testUsesTemplateConfigurationToSetTemplate()
7982
'zend-expressive' => [
8083
'error_handler' => [
8184
'template_error' => 'error::custom',
85+
'layout' => 'layout::custom',
8286
],
8387
],
8488
]);
@@ -90,5 +94,6 @@ public function testUsesTemplateConfigurationToSetTemplate()
9094
$this->assertAttributeEquals(false, 'debug', $generator);
9195
$this->assertAttributeEmpty('renderer', $generator);
9296
$this->assertAttributeEquals('error::custom', 'template', $generator);
97+
$this->assertAttributeEquals('layout::custom', 'layout', $generator);
9398
}
9499
}

test/Middleware/ErrorResponseGeneratorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public function testRendersTemplateWithoutErrorDetailsWhenRendererPresentAndNotI
135135
'uri' => 'https://example.com/foo',
136136
'status' => StatusCode::STATUS_INTERNAL_SERVER_ERROR,
137137
'reason' => 'Internal Server Error',
138+
'layout' => 'layout::default',
138139
])
139140
->willReturn('TEMPLATED CONTENTS');
140141

@@ -198,6 +199,7 @@ public function testRendersTemplateWithErrorDetailsWhenRendererPresentAndInDebug
198199
'status' => StatusCode::STATUS_INTERNAL_SERVER_ERROR,
199200
'reason' => 'Network Connect Timeout Error',
200201
'error' => $error,
202+
'layout' => 'layout::default',
201203
])
202204
->willReturn('TEMPLATED CONTENTS');
203205

0 commit comments

Comments
 (0)