-
Notifications
You must be signed in to change notification settings - Fork 195
Added custom layout support in NotFoundDelegate #511
Changes from all commits
995c4b0
20242a7
f85291d
fc38974
697e597
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
class NotFoundDelegate implements DelegateInterface | ||
{ | ||
const TEMPLATE_DEFAULT = 'error::404'; | ||
const LAYOUT_DEFAULT = 'layout::error'; | ||
|
||
/** | ||
* @var TemplateRendererInterface | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -58,7 +67,7 @@ public function __construct( | |
*/ | ||
public function process(ServerRequestInterface $request) | ||
{ | ||
if (! $this->renderer) { | ||
if (!$this->renderer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert this change please. CS is now failing:
|
||
return $this->generatePlainTextResponse($request); | ||
} | ||
|
||
|
@@ -78,7 +87,7 @@ private function generatePlainTextResponse(ServerRequestInterface $request) | |
->write(sprintf( | ||
'Cannot %s %s', | ||
$request->getMethod(), | ||
(string) $request->getUri() | ||
(string)$request->getUri() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Revert this change please. We don;t have currently sniff to check it, so all seems to be fine, but for consistency we should have here single space. |
||
)); | ||
return $response; | ||
} | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add trailing comma after last element in the array |
||
])->willReturn('CONTENT'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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 alayout::default
. As such, I think that should likely be the default in this situation, to allow for backwards compatibility with existing applications.