forked from nanoninja/docker-nginx-php-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.php
executable file
·213 lines (171 loc) · 4.13 KB
/
Response.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
namespace Acme\Http;
use duncan3dc\Laravel\BladeInstance;
use Kunststube\CSRFP\SignatureGenerator;
use Sunra\PhpSimple\HtmlDomParser;
/**
* Class Response
* @package Acme\Http
*/
class Response {
protected $data;
protected $view;
protected $with;
protected $response_type;
protected $response_code;
protected $flash;
protected $signer;
protected $session;
protected $with_input;
protected $request;
/**
* Constructor
*/
public function __construct(Request $request, SignatureGenerator $signer, BladeInstance $blade, Session $session)
{
$this->request = $request;
$this->blade = $blade;
$this->response_type = 'text/html';
$this->signer = $signer;
$this->with['signer'] = $this->signer;
$this->session = new Session();
$this->with_input = false;
}
/**
* Render a page
*/
public function render()
{
$this->with['_session'] = $this->session;
$html = $this->blade->render($this->view, $this->with);
$this->repopulateForm($html);
$this->renderOutput($html);
}
/**
* @return $this
*/
public function toJson()
{
$this->response_type = 'application/json';
return $this;
}
/**
* @return $this
*/
public function toXml()
{
$this->response_type = 'text/xml';
return $this;
}
/**
* @param $view
* @return $this
*/
public function withView($view)
{
$this->view = $view;
return $this;
}
/**
* @param $name
* @param $value
* @return $this
*/
public function with($name, $value)
{
$this->with[$name] = $value;
return $this;
}
/**
* @param $code
* @return $this
*/
public function withResponseCode($code)
{
$this->response_code = $code;
return $this;
}
/**
* @param $message
* @return $this
*/
public function withError($message)
{
$this->session->put('_error', $message);
return $this;
}
/**
* @param $message
* @return $this
*/
public function withMessage($message)
{
$this->session->put('_message', $message);
return $this;
}
/**
* @param $target
*/
public function redirectTo($target)
{
header("Location: " . $target);
}
/**
*
*/
public function withInput()
{
$this->with_input = true;
}
/**
* @param $html
* @return mixed
*/
private function repopulateForm($html)
{
if ($this->with_input) {
$keys = $this->request->getPost();
$dom = HtmlDomParser::str_get_html($html);
foreach ($keys as $name => $value) {
$elements = $dom->find('#' . $name);
foreach ($elements as $element) {
$tag = $element->tag;
switch ($tag) {
case ("input"):
if (isset($element->value))
$element->value = $value;
break;
case ("textarea"):
$element->innertext = $value;
break;
default:
// nothing
}
}
}
$html = $dom->save();
}
return $html;
}
/**
* @param $payload
*/
private function renderOutput($payload)
{
if ($this->response_code != null) {
switch ($this->response_code) {
case (404):
header("HTTP/1.1 404 Not Found");
break;
default:
// nothing
}
}
header('Content-Type: ' . $this->response_type);
echo $payload;
if ($this->session->has('_message'))
$this->session->forget('_message');
if ($this->session->has('_error'))
$this->session->forget('_error');
}
}