1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Imanghafoori\HeyMan; |
4
|
|
|
|
5
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
6
|
|
|
|
7
|
|
|
class Responder |
8
|
|
|
{ |
9
|
|
|
private $response; |
10
|
|
|
|
11
|
|
|
private $exception; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Create a new redirect response to the given path. |
15
|
|
|
* |
16
|
|
|
* @param string $path |
17
|
|
|
* @param int $status |
18
|
|
|
* @param array $headers |
19
|
|
|
* @param bool|null $secure |
20
|
|
|
*/ |
21
|
3 |
|
public function redirectTo($path, $status = 302, $headers = [], $secure = null) |
22
|
|
|
{ |
23
|
3 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
24
|
3 |
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return a new response from the application. |
29
|
|
|
* |
30
|
|
|
* @param string $content |
31
|
|
|
* @param int $status |
32
|
|
|
* @param array $headers |
33
|
|
|
*/ |
34
|
1 |
|
public function make($content = '', $status = 200, array $headers = []) |
35
|
|
|
{ |
36
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return a new view response from the application. |
41
|
|
|
* |
42
|
|
|
* @param string $view |
43
|
|
|
* @param array $data |
44
|
|
|
* @param int $status |
45
|
|
|
* @param array $headers |
46
|
|
|
*/ |
47
|
1 |
|
public function view($view, $data = [], $status = 200, array $headers = []) |
48
|
|
|
{ |
49
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Return a new JSON response from the application. |
54
|
|
|
* |
55
|
|
|
* @param string|array $data |
56
|
|
|
* @param int $status |
57
|
|
|
* @param array $headers |
58
|
|
|
* @param int $options |
59
|
|
|
*/ |
60
|
2 |
|
public function json($data = [], $status = 200, array $headers = [], $options = 0) |
61
|
|
|
{ |
62
|
2 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
63
|
2 |
|
} |
64
|
|
|
/** |
65
|
|
|
* Return a new JSONP response from the application. |
66
|
|
|
* |
67
|
|
|
* @param string $callback |
68
|
|
|
* @param string|array $data |
69
|
|
|
* @param int $status |
70
|
|
|
* @param array $headers |
71
|
|
|
* @param int $options |
72
|
|
|
*/ |
73
|
1 |
|
public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0) |
74
|
|
|
{ |
75
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return a new streamed response from the application. |
80
|
|
|
* |
81
|
|
|
* @param \Closure $callback |
82
|
|
|
* @param int $status |
83
|
|
|
* @param array $headers |
84
|
|
|
*/ |
85
|
1 |
|
public function stream($callback, $status = 200, array $headers = []) |
86
|
|
|
{ |
87
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
88
|
1 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return a new streamed response as a file download from the application. |
92
|
|
|
* |
93
|
|
|
* @param \Closure $callback |
94
|
|
|
* @param string|null $name |
95
|
|
|
* @param array $headers |
96
|
|
|
* @param string|null $disposition |
97
|
|
|
*/ |
98
|
1 |
|
public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment') |
99
|
|
|
{ |
100
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a new file download response. |
105
|
|
|
* |
106
|
|
|
* @param \SplFileInfo|string $file |
107
|
|
|
* @param string|null $name |
108
|
|
|
* @param array $headers |
109
|
|
|
* @param string|null $disposition |
110
|
|
|
*/ |
111
|
1 |
|
public function download($file, $name = null, array $headers = [], $disposition = 'attachment') |
112
|
|
|
{ |
113
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
114
|
1 |
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Create a new redirect response to a named route. |
118
|
|
|
* |
119
|
|
|
* @param string $route |
120
|
|
|
* @param array $parameters |
121
|
|
|
* @param int $status |
122
|
|
|
* @param array $headers |
123
|
|
|
*/ |
124
|
1 |
|
public function redirectToRoute($route, $parameters = [], $status = 302, $headers = []) |
125
|
|
|
{ |
126
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Create a new redirect response to a controller action. |
131
|
|
|
* |
132
|
|
|
* @param string $action |
133
|
|
|
* @param array $parameters |
134
|
|
|
* @param int $status |
135
|
|
|
* @param array $headers |
136
|
|
|
*/ |
137
|
1 |
|
public function redirectToAction($action, $parameters = [], $status = 302, $headers = []) |
138
|
|
|
{ |
139
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Create a new redirect response, while putting the current URL in the session. |
144
|
|
|
* |
145
|
|
|
* @param string $path |
146
|
|
|
* @param int $status |
147
|
|
|
* @param array $headers |
148
|
|
|
* @param bool|null $secure |
149
|
|
|
*/ |
150
|
1 |
|
public function redirectGuest($path, $status = 302, $headers = [], $secure = null) |
151
|
|
|
{ |
152
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
153
|
1 |
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Create a new redirect response to the previously intended location. |
157
|
|
|
* |
158
|
|
|
* @param string $default |
159
|
|
|
* @param int $status |
160
|
|
|
* @param array $headers |
161
|
|
|
* @param bool|null $secure |
162
|
|
|
*/ |
163
|
1 |
|
public function redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null) |
164
|
|
|
{ |
165
|
1 |
|
$this->response = [__FUNCTION__, func_get_args()]; |
166
|
1 |
|
} |
167
|
|
|
|
168
|
1 |
|
public function weThrowNew($exception, $message= '') |
169
|
|
|
{ |
170
|
1 |
|
$this->exception = new $exception($message); |
171
|
1 |
|
} |
172
|
|
|
|
173
|
2 |
|
public function abort($code, $message = '', array $headers = []) |
174
|
|
|
{ |
175
|
|
|
try { |
176
|
2 |
|
abort($code, $message, $headers); |
177
|
2 |
|
} catch (\Exception $e) { |
178
|
2 |
|
$this->exception = $e; |
179
|
|
|
} |
180
|
2 |
|
} |
181
|
|
|
|
182
|
51 |
|
public function weDenyAccess($msg = '') |
183
|
|
|
{ |
184
|
51 |
|
$this->exception = new AuthorizationException($msg); |
185
|
51 |
|
} |
186
|
|
|
|
187
|
2 |
|
public function afterCalling($callback, array $parameters = []) |
188
|
|
|
{ |
189
|
2 |
|
app()->call($callback, $parameters); |
190
|
1 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
59 |
|
public function __destruct() |
194
|
|
|
{ |
195
|
59 |
|
app(HeyMan::class)->startListening($this->response, $this->exception); |
196
|
59 |
|
} |
197
|
|
|
|
198
|
|
|
public function afterFiringEvent(...$args) |
199
|
|
|
{ |
200
|
|
|
app('events')->dispatch(...$args); |
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
} |