This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathErrorResponseGenerator.php
64 lines (55 loc) · 2.04 KB
/
ErrorResponseGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/**
* @see https://github.com/zendframework/zend-expressive for the canonical source repository
* @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. (https://www.zend.com)
* @license https://github.com/zendframework/zend-expressive/blob/master/LICENSE.md New BSD License
*/
declare(strict_types=1);
namespace Zend\Expressive\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
use Zend\Expressive\Response\ErrorResponseGeneratorTrait;
use Zend\Expressive\Template\TemplateRendererInterface;
use Zend\Stratigility\Utils;
class ErrorResponseGenerator
{
use ErrorResponseGeneratorTrait;
public const TEMPLATE_DEFAULT = 'error::error';
public const LAYOUT_DEFAULT = 'layout::default';
public function __construct(
bool $isDevelopmentMode = false,
TemplateRendererInterface $renderer = null,
string $template = self::TEMPLATE_DEFAULT,
string $layout = self::LAYOUT_DEFAULT
) {
$this->debug = $isDevelopmentMode;
$this->renderer = $renderer;
$this->template = $template;
$this->layout = $layout;
}
public function __invoke(
Throwable $e,
ServerRequestInterface $request,
ResponseInterface $response
) : ResponseInterface {
$response = $response->withStatus(Utils::getStatusCode($e, $response));
if ($this->renderer) {
return $this->prepareTemplatedResponse(
$e,
$this->renderer,
[
'response' => $response,
'request' => $request,
'uri' => (string) $request->getUri(),
'status' => $response->getStatusCode(),
'reason' => $response->getReasonPhrase(),
'layout' => $this->layout,
],
$this->debug,
$response
);
}
return $this->prepareDefaultResponse($e, $this->debug, $response);
}
}