diff --git a/src/Container/ErrorHandlerFactory.php b/src/Container/ErrorHandlerFactory.php new file mode 100644 index 00000000..f2f97385 --- /dev/null +++ b/src/Container/ErrorHandlerFactory.php @@ -0,0 +1,24 @@ +get(TemplatedErrorResponseGenerator::class) + ); + } +} diff --git a/src/Container/NotFoundHandlerFactory.php b/src/Container/NotFoundHandlerFactory.php new file mode 100644 index 00000000..55941aaa --- /dev/null +++ b/src/Container/NotFoundHandlerFactory.php @@ -0,0 +1,33 @@ +has('config') + ? $container->get('config') + : []; + + $template = isset($config['zend-expressive']['error_handler']['template_404']) + ? $config['zend-expressive']['error_handler']['template_404'] + : 'error/404'; + + return new NotFoundHandler( + $container->get(TemplateRendererInterface::class), + new Response(), + $template + ); + } +} diff --git a/src/Container/TemplatedErrorResponseGeneratorFactory.php b/src/Container/TemplatedErrorResponseGeneratorFactory.php new file mode 100644 index 00000000..2f021ddc --- /dev/null +++ b/src/Container/TemplatedErrorResponseGeneratorFactory.php @@ -0,0 +1,31 @@ +has('config') ? $container->get('config') : []; + $debug = isset($config['debug']) ? $config['debug'] : false; + + $template = isset($config['zend-expressive']['error_handler']['template_error']) + ? $config['zend-expressive']['error_handler']['template_error'] + : 'error/error'; + + return new TemplatedErrorResponseGenerator( + $container->get(TemplateRendererInterface::class), + $template, + $debug + ); + } +} diff --git a/src/NotFoundHandler.php b/src/NotFoundHandler.php new file mode 100644 index 00000000..1b876125 --- /dev/null +++ b/src/NotFoundHandler.php @@ -0,0 +1,72 @@ +renderer = $renderer; + $this->responsePrototype = $responsePrototype; + $this->template = $template; + } + + /** + * Proxy to process() + * + * Proxies to process, after first wrapping the `$next` argument using the + * CallableDelegateDecorator. + * + * @param ServerRequestInterface $request + * @param ResponseInterface $response + * @param callable $next + * + * @return ResponseInterface + */ + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) + { + return $this->process($request, new CallableDelegateDecorator($next, $response)); + } + + /** + * Creates and returns a 404 response. + * + * @param ServerRequestInterface $request Ignored. + * @param DelegateInterface $delegate Ignored. + * + * @return ResponseInterface + */ + public function process(ServerRequestInterface $request, DelegateInterface $delegate) + { + $response = $this->responsePrototype->withStatus(404); + $response->getBody()->write( + $this->renderer->render($this->template) + ); + + return $response; + } +} diff --git a/src/TemplatedErrorResponseGenerator.php b/src/TemplatedErrorResponseGenerator.php new file mode 100644 index 00000000..e1fc2317 --- /dev/null +++ b/src/TemplatedErrorResponseGenerator.php @@ -0,0 +1,41 @@ +renderer = $renderer; + $this->isDevelopmentMode = $isDevelopmentMode; + $this->template = $template; + } + + public function __invoke($e, ServerRequestInterface $request, ResponseInterface $response) + { + $response = $response->withStatus(500); + $response->getBody()->write($this->renderer->render('error::error', [ + 'exception' => $e, + 'development_mode' => $this->isDevelopmentMode, + 'status' => $response->getStatusCode(), + 'reason' => $response->getReasonPhrase(), + ])); + + return $response; + } +}