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

Added custom layout support in NotFoundDelegate #511

Merged
merged 5 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from 4 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 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);
}
}
18 changes: 16 additions & 2 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::error';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main problem I have with this is that we do not at this time define a layout::error template in the skeleton application, only a layout::default. As such, I think that should likely be the default in this situation, to allow for backwards compatibility with existing applications.


/**
* @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->layout = $layout;
}

/**
Expand Down Expand Up @@ -93,9 +102,14 @@ private function generatePlainTextResponse(ServerRequestInterface $request)
*/
private function generateTemplatedResponse(ServerRequestInterface $request)
{
$templateData = [
'request' => $request,
'layout' => $this->layout,
];

$response = $this->responsePrototype->withStatus(StatusCodeInterface::STATUS_NOT_FOUND);
$response->getBody()->write(
$this->renderer->render($this->template, ['request' => $request])
$this->renderer->render($this->template, $templateData)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add a new variable?

$this->renderer->render($this->template, [
    'request'  => $request,
    'layout'  => $this->layout,
]);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will rework this point to make the parameter optional - or do you have a better idea? Another option would be that in the factory other optional parameters can be passed to the renderer no matter what...

);

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
10 changes: 7 additions & 3 deletions test/Delegate/NotFoundDelegateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ public function testConstructorDoesNotRequireARenderer()
public function testConstructorCanAcceptRendererAndTemplate()
{
$renderer = $this->prophesize(TemplateRendererInterface::class)->reveal();
$layout = 'layout::error';
$template = 'foo::bar';

$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($layout, 'layout', $delegate);
$this->assertAttributeEquals($template, 'template', $delegate);
}

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

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add trailing comma after last element in the array

])->willReturn('CONTENT');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last line should have one less indent:

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


$stream = $this->prophesize(StreamInterface::class);
$stream->write('CONTENT')->shouldBeCalled();
Expand Down