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

Commit

Permalink
Merge branch 'feature/511' into develop
Browse files Browse the repository at this point in the history
Close #511
Fixes #360
  • Loading branch information
weierophinney committed Oct 9, 2017
2 parents 91daf13 + 7d5a6d7 commit 7c0dec8
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ All notable changes to this project will be documented in this file, in reverse
This change is an internal implementation detail only, and will not affect
existing implementations or extensions.

- [#511](https://github.com/zendframework/zend-expressive/pull/511) modifies
the `NotFoundDelegate` to accept an optional `$layout` argument to its
constructor; the value defaults to `layout::default` if not provided. That
value will be passed for the `layout` template variable when the delegate
renders a template, allowing zend-view users (and potentially other template
systems) to customize the layout template used for reporting errors.

You may provide the template via the configuration
`zend-expressive.error_handler.layout`.

### Deprecated

- Nothing.
Expand Down
5 changes: 4 additions & 1 deletion src/Container/NotFoundDelegateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ public function __invoke(ContainerInterface $container)
$template = isset($config['zend-expressive']['error_handler']['template_404'])
? $config['zend-expressive']['error_handler']['template_404']
: NotFoundDelegate::TEMPLATE_DEFAULT;
$layout = isset($config['zend-expressive']['error_handler']['layout'])
? $config['zend-expressive']['error_handler']['layout']
: NotFoundDelegate::LAYOUT_DEFAULT;

return new NotFoundDelegate(new Response(), $renderer, $template);
return new NotFoundDelegate(new Response(), $renderer, $template, $layout);
}
}
17 changes: 13 additions & 4 deletions src/Delegate/NotFoundDelegate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class NotFoundDelegate implements DelegateInterface
{
const TEMPLATE_DEFAULT = 'error::404';
const LAYOUT_DEFAULT = 'layout::default';

/**
* @var TemplateRendererInterface
Expand All @@ -35,19 +36,27 @@ class NotFoundDelegate implements DelegateInterface
*/
private $template;

/**
* @var string
*/
private $layout;

/**
* @param ResponseInterface $responsePrototype
* @param TemplateRendererInterface $renderer
* @param string $template
* @param string $layout
*/
public function __construct(
ResponseInterface $responsePrototype,
TemplateRendererInterface $renderer = null,
$template = self::TEMPLATE_DEFAULT
$template = self::TEMPLATE_DEFAULT,
$layout = self::LAYOUT_DEFAULT
) {
$this->responsePrototype = $responsePrototype;
$this->renderer = $renderer;
$this->template = $template;
$this->renderer = $renderer;
$this->template = $template;
$this->layout = $layout;
}

/**
Expand Down Expand Up @@ -95,7 +104,7 @@ private function generateTemplatedResponse(ServerRequestInterface $request)
{
$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
$response->getBody()->write(
$this->renderer->render($this->template, ['request' => $request])
$this->renderer->render($this->template, ['request' => $request, 'layout' => $this->layout])
);

return $response;
Expand Down
6 changes: 6 additions & 0 deletions test/Container/NotFoundDelegateFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public function testFactoryUsesConfigured404TemplateWhenPresent()
$config = [
'zend-expressive' => [
'error_handler' => [
'layout' => 'layout::error',
'template_404' => 'foo::bar',
],
],
Expand All @@ -64,6 +65,11 @@ public function testFactoryUsesConfigured404TemplateWhenPresent()
$factory = new NotFoundDelegateFactory();

$delegate = $factory($this->container->reveal());
$this->assertAttributeEquals(
$config['zend-expressive']['error_handler']['layout'],
'layout',
$delegate
);
$this->assertAttributeEquals(
$config['zend-expressive']['error_handler']['template_404'],
'template',
Expand Down
13 changes: 11 additions & 2 deletions test/Delegate/NotFoundDelegateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ public function testConstructorCanAcceptRendererAndTemplate()
{
$renderer = $this->prophesize(TemplateRendererInterface::class)->reveal();
$template = 'foo::bar';
$layout = 'layout::error';

$delegate = new NotFoundDelegate($this->response->reveal(), $renderer, $template);
$delegate = new NotFoundDelegate($this->response->reveal(), $renderer, $template, $layout);

$this->assertInstanceOf(NotFoundDelegate::class, $delegate);
$this->assertAttributeSame($renderer, 'renderer', $delegate);
$this->assertAttributeEquals($template, 'template', $delegate);
$this->assertAttributeEquals($layout, 'layout', $delegate);
}

public function testRendersDefault404ResponseWhenNoRendererPresent()
Expand All @@ -68,7 +70,14 @@ public function testUsesRendererToGenerateResponseContentsWhenPresent()
$request = $this->prophesize(ServerRequestInterface::class)->reveal();

$renderer = $this->prophesize(TemplateRendererInterface::class);
$renderer->render(NotFoundDelegate::TEMPLATE_DEFAULT, ['request' => $request])
$renderer
->render(
NotFoundDelegate::TEMPLATE_DEFAULT,
[
'request' => $request,
'layout' => NotFoundDelegate::LAYOUT_DEFAULT,
]
)
->willReturn('CONTENT');

$stream = $this->prophesize(StreamInterface::class);
Expand Down

0 comments on commit 7c0dec8

Please sign in to comment.