|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alkemann\h2l\response; |
|
4
|
|
|
|
|
5
|
|
|
use alkemann\h2l\Environment; |
|
6
|
|
|
use alkemann\h2l\exceptions\InvalidUrl; |
|
7
|
|
|
use alkemann\h2l\Log; |
|
8
|
|
|
use alkemann\h2l\Message; |
|
9
|
|
|
use alkemann\h2l\Request; |
|
10
|
|
|
use alkemann\h2l\Response; |
|
11
|
|
|
use alkemann\h2l\util\Http; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Error |
|
15
|
|
|
* |
|
16
|
|
|
* @package alkemann\h2l |
|
17
|
|
|
*/ |
|
18
|
|
|
class Error extends Response |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected string $content_type = 'text/html'; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var int |
|
26
|
|
|
*/ |
|
27
|
|
|
protected int $code = 500; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array<mixed> |
|
30
|
|
|
*/ |
|
31
|
|
|
protected array $data = []; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var null|Request |
|
34
|
|
|
*/ |
|
35
|
|
|
protected ?Request $request = null; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected array $config = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor |
|
43
|
|
|
* |
|
44
|
|
|
* @param array<mixed> $data |
|
45
|
|
|
* @param array<string, mixed> $config |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct(array $data = [], array $config = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->data = $data; |
|
50
|
|
|
foreach (['content_type', 'code', 'request'] as $key) { |
|
51
|
|
|
if (isset($config[$key])) { |
|
52
|
|
|
$this->{$key} = $config[$key]; |
|
53
|
|
|
unset($config[$key]); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->request) { |
|
58
|
|
|
$this->content_type = $this->request->acceptType(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->config = $config + [ |
|
62
|
|
|
'page_class' => Page::class |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
$this->message = (new Message()) |
|
66
|
|
|
->withCode($this->code) |
|
67
|
|
|
->withHeader('Content-Type', $this->content_type) |
|
68
|
|
|
; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns the `Request` of this response |
|
73
|
|
|
* |
|
74
|
|
|
* @return null|Request |
|
75
|
|
|
*/ |
|
76
|
|
|
public function request(): ?Request |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->request; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Use the Page class configured to render the response |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function render(): string |
|
87
|
|
|
{ |
|
88
|
|
|
$response = ''; |
|
89
|
|
|
$page_class = $this->config['page_class']; |
|
90
|
|
|
try { |
|
91
|
|
|
$page_config = $this->config + [ |
|
92
|
|
|
'code' => $this->code, |
|
93
|
|
|
'template' => $this->code == 404 ? '404' : 'error', |
|
94
|
|
|
'content_type' => $this->content_type, |
|
95
|
|
|
'request' => $this->request |
|
96
|
|
|
]; |
|
97
|
|
|
$data = $this->data + ['code' => $this->code]; |
|
98
|
|
|
/** |
|
99
|
|
|
* @var Page $page |
|
100
|
|
|
*/ |
|
101
|
|
|
$page = new $page_class($data, $page_config); |
|
102
|
|
|
$page->isValid(); |
|
103
|
|
|
|
|
104
|
|
|
return $page->render(); |
|
105
|
|
|
} catch (InvalidUrl $e) { |
|
106
|
|
|
Log::debug("No error page made at " . $e->getMessage()); |
|
107
|
|
|
if (Environment::get('debug')) { |
|
108
|
|
|
$response = "No error page made at " . $e->getMessage(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
$this->setHeaders(); |
|
112
|
|
|
return $response; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|