11<?php
22/**
33 * @see https://github.com/zendframework/zend-expressive for the canonical source repository
4- * @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. (https://www.zend.com)
4+ * @copyright Copyright (c) 2016-2018 Zend Technologies USA Inc. (https://www.zend.com)
55 * @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
66 */
77
88declare (strict_types=1 );
99
1010namespace Zend \Expressive \Middleware ;
1111
12+ use Fig \Http \Message \StatusCodeInterface ;
1213use Psr \Http \Message \ResponseInterface ;
1314use Psr \Http \Message \ServerRequestInterface ;
1415use Psr \Http \Server \MiddlewareInterface ;
1516use Psr \Http \Server \RequestHandlerInterface ;
16- use Zend \Expressive \Handler \ NotFoundHandler ;
17+ use Zend \Expressive \Template \ TemplateRendererInterface ;
1718
1819class NotFoundMiddleware implements MiddlewareInterface
1920{
21+ const TEMPLATE_DEFAULT = 'error::404 ' ;
22+ const LAYOUT_DEFAULT = 'layout::default ' ;
23+
2024 /**
21- * @var NotFoundHandler
25+ * @var TemplateRendererInterface
2226 */
23- private $ internalHandler ;
27+ private $ renderer ;
2428
2529 /**
26- * @param NotFoundHandler $internalHandler
30+ * This duplicates the property in StratigilityNotFoundHandler, but is done
31+ * to ensure that we have access to the value in the methods we override.
32+ *
33+ * @var ResponseInterface
2734 */
28- public function __construct (NotFoundHandler $ internalHandler )
29- {
30- $ this ->internalHandler = $ internalHandler ;
35+ protected $ responsePrototype ;
36+
37+ /**
38+ * @var string
39+ */
40+ private $ template ;
41+
42+ /**
43+ * @var string
44+ */
45+ private $ layout ;
46+
47+ public function __construct (
48+ ResponseInterface $ responsePrototype ,
49+ TemplateRendererInterface $ renderer = null ,
50+ string $ template = self ::TEMPLATE_DEFAULT ,
51+ string $ layout = self ::LAYOUT_DEFAULT
52+ ) {
53+ $ this ->responsePrototype = $ responsePrototype ;
54+ $ this ->renderer = $ renderer ;
55+ $ this ->template = $ template ;
56+ $ this ->layout = $ layout ;
3157 }
3258
3359 /**
@@ -38,6 +64,41 @@ public function __construct(NotFoundHandler $internalHandler)
3864 */
3965 public function process (ServerRequestInterface $ request , RequestHandlerInterface $ handler ) : ResponseInterface
4066 {
41- return $ this ->internalHandler ->handle ($ request );
67+ if (! $ this ->renderer ) {
68+ return $ this ->generatePlainTextResponse ($ request );
69+ }
70+
71+ return $ this ->generateTemplatedResponse ($ request );
72+ }
73+
74+ /**
75+ * Generates a plain text response indicating the request method and URI.
76+ */
77+ private function generatePlainTextResponse (ServerRequestInterface $ request ) : ResponseInterface
78+ {
79+ $ response = $ this ->responsePrototype ->withStatus (StatusCodeInterface::STATUS_NOT_FOUND );
80+ $ response ->getBody ()
81+ ->write (sprintf (
82+ 'Cannot %s %s ' ,
83+ $ request ->getMethod (),
84+ (string ) $ request ->getUri ()
85+ ));
86+
87+ return $ response ;
88+ }
89+
90+ /**
91+ * Generates a response using a template.
92+ *
93+ * Template will receive the current request via the "request" variable.
94+ */
95+ private function generateTemplatedResponse (ServerRequestInterface $ request ) : ResponseInterface
96+ {
97+ $ response = $ this ->responsePrototype ->withStatus (StatusCodeInterface::STATUS_NOT_FOUND );
98+ $ response ->getBody ()->write (
99+ $ this ->renderer ->render ($ this ->template , ['request ' => $ request , 'layout ' => $ this ->layout ])
100+ );
101+
102+ return $ response ;
42103 }
43104}
0 commit comments